KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > NamedIterator


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.NamedIterator
4  * Version: 1.0
5  *
6  * Date: 2002-10-27
7  *
8  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
9  *
10  * This is licensed under the GNU General Public License (GPL)
11  * and comes with NO WARRANTY. See file license.txt for details.
12  *
13  * Author: Jens Gulden
14  * Email: beautyj@jensgulden.de
15  */

16
17 package de.gulden.util.javasource;
18
19 import java.lang.Class JavaDoc;
20 import java.io.*;
21 import java.util.*;
22
23 /**
24  * Tool class providing a mechanism to access elements from a set of named elements
25  * either in sequence or by name.
26  *
27  * @author Jens Gulden
28  * @version 1.0
29  */

30 public class NamedIterator implements Serializable {
31
32     // ------------------------------------------------------------------------
33
// --- fields ---
34
// ------------------------------------------------------------------------
35
/**
36      * The data.
37      */

38     public Vector data;
39
40     /**
41      * The hash.
42      */

43     protected Hashtable hash;
44
45     /**
46      * The pos.
47      */

48     protected int pos;
49
50     /**
51      * The readonly.
52      */

53     protected boolean readonly=false;
54
55
56     // ------------------------------------------------------------------------
57
// --- constructors ---
58
// ------------------------------------------------------------------------
59
/**
60      * Creates a new instance of NamedIterator.
61      */

62     public NamedIterator() {
63         data=new Vector();
64         hash=new Hashtable();
65         reset();
66     }
67
68     /**
69      * Create new NeamedIterator that gets populated with the value of Vector
70      * <code>v</code>. The reference to <code>v</code> is kept, so changes to
71      * to the iterator will have effect on the original Vector.
72      */

73     public NamedIterator(Vector v) {
74         data=v; // same _pointer_: changes will have effect in original Vector
75
hash=new Hashtable();
76         for (Enumeration e=v.elements();e.hasMoreElements();) {
77             Named n=(Named)e.nextElement();
78             hash.put(n.getName(),n);
79         }
80         reset();
81     }
82
83     /**
84      * Special constructor called from Class.
85      */

86     NamedIterator(Vector v, Object JavaDoc type, int modifierMask) {
87         // Using type 'Object' instead of 'java.lang.Class' for parameter type
88
// is a workaround to avoid having to generate fully qualified class names
89
// when applying BeatyJ to this code. (Otherwise, would confuse with class
90
// 'Class' in this package.)
91
this();
92         for (Enumeration e=v.elements();e.hasMoreElements();) {
93             Member m=(Member)e.nextElement();
94             int mod=m.getModifier();
95             if (
96             ((modifierMask&mod)!=0)
97             &&(m.getClass().isAssignableFrom((java.lang.Class JavaDoc)type))
98             ) {
99                 data.addElement(m);
100             }
101         }
102         reset();
103         lockReadonly(); // read-only in this mode
104
}
105
106     /**
107      * Creates a new instance of NamedIterator.
108      */

109     private NamedIterator(boolean dummy) {
110         // dummy constructor for cloning
111
}
112
113
114     // ------------------------------------------------------------------------
115
// --- methods ---
116
// ------------------------------------------------------------------------
117
public Object JavaDoc clone() {
118         NamedIterator c=new NamedIterator(true);
119         synchronized (this) {
120             c.data=(Vector)data.clone();
121             c.hash=(Hashtable)hash.clone();
122             c.pos=pos;
123             c.readonly=readonly;
124         }
125         return c;
126     }
127
128     /**
129      * Get next element.
130      *
131      * @return The next element, or null if <code>hasMore()==false</code>.
132      */

133     public Named next() {
134         if (hasMore()) {
135             return (Named)data.elementAt(pos++);
136         }
137         else {
138             return null;
139         }
140     }
141
142     /**
143      * Tests whether there are more elements that can beretrieved using <code>next()</code>.
144      */

145     public boolean hasMore() {
146         return (pos<data.size());
147     }
148
149     /**
150      * Get an element by name.
151      */

152     public Named find(String JavaDoc n) {
153         return (Named)hash.get(n);
154     }
155
156     /**
157      * Sets the iterator back to element position 0.
158      */

159     public void reset() {
160         pos=0;
161     }
162
163     /**
164      * Tests whether elements can be added to or removed from this iterator.
165      *
166      * @see #addHere
167      * @see #add
168      * @see #remove
169      * @see #removeAll
170      */

171     public boolean isReadOnly() {
172         return readonly;
173     }
174
175     /**
176      * Adds an element at the current position.
177      */

178     public void addHere(Named n) {
179         if (!readonly) {
180             synchronized (this) {
181                 data.insertElementAt(n,pos);
182                 hash.put(n.getName(),n);
183             }
184         }
185     }
186
187     /**
188      * Adds an element at the end of the iterator-list.
189      */

190     public void add(Named n) {
191         if (!readonly) {
192             synchronized (this) {
193                 data.addElement(n);
194                 hash.put(n.getName(),n);
195             }
196         }
197     }
198
199     /**
200      * Removes an element from the iterator-list.
201      */

202     public void remove(Named n) {
203         if (!readonly) {
204             synchronized (this) {
205                 data.addElement(n);
206                 hash.put(n.getName(),n);
207             }
208         }
209     }
210
211     /**
212      * Removes all elements from the iterator-list.
213      */

214     public void removeAll() {
215         if (!readonly) {
216             synchronized (this) {
217                 data.removeAllElements();
218                 hash.clear();
219             }
220         }
221     }
222
223     /**
224      * Adds all elements of the NamedIterator to this.
225      */

226     public void add(NamedIterator it) {
227         it.reset();
228         synchronized (this) {
229             while (it.hasMore()) {
230                 data.addElement(it.next());
231             }
232         }
233     }
234
235     /**
236      * Returns the number of elements in the iterator-list.
237      */

238     public int size() {
239         return data.size();
240     }
241
242     /**
243      * Set this iterator to read-only mode.
244      */

245     void lockReadonly() {
246         readonly=true;
247     }
248
249     /**
250      * Gets the orignal Vector that stores the iterator-list.
251      */

252     Vector getVector() {
253         return (Vector)data.clone();
254     }
255
256 } // end NamedIterator
257
Popular Tags