001 /* 002 Copyright (C) 2001 Renaud Pawlak 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.util; 019 020 import java.util.Vector; 021 import org.objectweb.jac.util.ExtArrays; 022 023 /** 024 * @author <a href="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a> 025 */ 026 027 /** 028 * This class defines a repository that provides order on the 029 * registered objects. 030 * 031 * <p>The order corresponds to the order the objects where registered 032 * into the repository. */ 033 034 public class OrderedRepository extends Repository { 035 036 /** 037 * Get the sole repository instance for this class. Creates it if 038 * it does not exist yet. 039 * 040 * <p>NOTE: this method MUST be defined by all subclasses. 041 */ 042 public static Repository get() { 043 if (repository == null) 044 repository = new OrderedRepository(); 045 return repository; 046 } 047 048 /** 049 * Store the sole instance of repository. 050 * 051 * <p>NOTE: this field MUST be defined by all subclasses. 052 * 053 * @see #get() 054 */ 055 protected static Repository repository = null; 056 057 /** 058 * Vector for the ordered objects. */ 059 public Vector orderedObjects = new Vector(); 060 061 /** 062 * Vector for the ordered names. */ 063 public Vector orderedNames = new Vector(); 064 065 /** 066 * Register a new object into the repository. 067 * 068 * @param logicalName the key that allows to find the object 069 * @param object the object to register 070 * @return true if the object registered, false if already 071 * registered 072 * 073 * @see #unregister(String) 074 */ 075 public boolean register(String logicalName, Object object) { 076 int index = 0; 077 index = orderedNames.indexOf(logicalName); 078 if (index != -1) { 079 orderedNames.remove(index); 080 orderedObjects.remove(index); 081 } 082 orderedObjects.add(object); 083 orderedNames.add(logicalName); 084 super.register(logicalName, object); 085 return true; 086 } 087 088 /** 089 * Unregister a new JacObject into the repository. 090 * 091 * @param logicalName the key that allows to find the object 092 * 093 * @see #register(String,Object) 094 */ 095 public void unregister(String logicalName) { 096 int index = 0; 097 index = orderedNames.indexOf(logicalName); 098 if (index == -1) { 099 return; 100 } 101 orderedNames.remove(index); 102 orderedObjects.remove(index); 103 super.unregister(logicalName); 104 } 105 106 /** 107 * Return all the ordered registered objects as an array. 108 * 109 * <p>Reverse operation is <code>getNames()</code>. 110 * 111 * @return the registered objects in this repository 112 * 113 * @see #register(String,Object) 114 * @see #getNames() 115 */ 116 public Object[] getObjects() { 117 return orderedObjects.toArray(); 118 } 119 120 /** 121 * Return the ordered names of the registered objects as an array. 122 * 123 * <p>The given order is the registering order of the objects. 124 * 125 * <p>Reverse operation is <code>getObjects()</code>. 126 * 127 * @return the registered object names in this repository 128 * 129 * @see #register(String,Object) 130 * @see #getObjects() 131 */ 132 public String[] getNames() { 133 return (String[])orderedNames.toArray(ExtArrays.emptyStringArray); 134 } 135 136 public String getPrintableString() { 137 String s=""; 138 for (int i=0; i<orderedNames.size(); i++) { 139 s = s+" - "+orderedNames.get(i)+" : "+orderedObjects.get(i)+"\n"; 140 } 141 return s; 142 } 143 144 } 145 146