KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > OrderedRepository


1 /*
2   Copyright (C) 2001 Renaud Pawlak
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.util.Vector JavaDoc;
21 import org.objectweb.jac.util.ExtArrays;
22
23 /**
24  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
25  */

26
27 /**
28  * This class defines a repository that provides order on the
29  * registered objects.
30  *
31  * <p>The order corresponds to the order the objects where registered
32  * into the repository. */

33
34 public class OrderedRepository extends Repository {
35
36     /**
37      * Get the sole repository instance for this class. Creates it if
38      * it does not exist yet.
39      *
40      * <p>NOTE: this method MUST be defined by all subclasses.
41      */

42     public static Repository get() {
43         if (repository == null)
44             repository = new OrderedRepository();
45         return repository;
46     }
47    
48     /**
49      * Store the sole instance of repository.
50      *
51      * <p>NOTE: this field MUST be defined by all subclasses.
52      *
53      * @see #get()
54      */

55     protected static Repository repository = null;
56
57     /**
58      * Vector for the ordered objects. */

59     public Vector JavaDoc orderedObjects = new Vector JavaDoc();
60
61     /**
62      * Vector for the ordered names. */

63     public Vector JavaDoc orderedNames = new Vector JavaDoc();
64
65     /**
66      * Register a new object into the repository.
67      *
68      * @param logicalName the key that allows to find the object
69      * @param object the object to register
70      * @return true if the object registered, false if already
71      * registered
72      *
73      * @see #unregister(String)
74      */

75     public boolean register(String JavaDoc logicalName, Object JavaDoc object) {
76         int index = 0;
77         index = orderedNames.indexOf(logicalName);
78         if (index != -1) {
79             orderedNames.remove(index);
80             orderedObjects.remove(index);
81         }
82         orderedObjects.add(object);
83         orderedNames.add(logicalName);
84         super.register(logicalName, object);
85         return true;
86     }
87
88     /**
89      * Unregister a new JacObject into the repository.
90      *
91      * @param logicalName the key that allows to find the object
92      *
93      * @see #register(String,Object)
94      */

95     public void unregister(String JavaDoc logicalName) {
96         int index = 0;
97         index = orderedNames.indexOf(logicalName);
98         if (index == -1) {
99             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 JavaDoc[] 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 JavaDoc[] getNames() {
133         return (String JavaDoc[])orderedNames.toArray(ExtArrays.emptyStringArray);
134     }
135
136     public String JavaDoc getPrintableString() {
137         String JavaDoc 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
147
Popular Tags