001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.NamedIterator
004: * Version: 1.0
005: *
006: * Date: 2002-10-27
007: *
008: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
009: *
010: * This is licensed under the GNU General Public License (GPL)
011: * and comes with NO WARRANTY. See file license.txt for details.
012: *
013: * Author: Jens Gulden
014: * Email: beautyj@jensgulden.de
015: */
016:
017: package de.gulden.util.javasource;
018:
019: import java.lang.Class;
020: import java.io.*;
021: import java.util.*;
022:
023: /**
024: * Tool class providing a mechanism to access elements from a set of named elements
025: * either in sequence or by name.
026: *
027: * @author Jens Gulden
028: * @version 1.0
029: */
030: public class NamedIterator implements Serializable {
031:
032: // ------------------------------------------------------------------------
033: // --- fields ---
034: // ------------------------------------------------------------------------
035: /**
036: * The data.
037: */
038: public Vector data;
039:
040: /**
041: * The hash.
042: */
043: protected Hashtable hash;
044:
045: /**
046: * The pos.
047: */
048: protected int pos;
049:
050: /**
051: * The readonly.
052: */
053: protected boolean readonly = false;
054:
055: // ------------------------------------------------------------------------
056: // --- constructors ---
057: // ------------------------------------------------------------------------
058: /**
059: * Creates a new instance of NamedIterator.
060: */
061: public NamedIterator() {
062: data = new Vector();
063: hash = new Hashtable();
064: reset();
065: }
066:
067: /**
068: * Create new NeamedIterator that gets populated with the value of Vector
069: * <code>v</code>. The reference to <code>v</code> is kept, so changes to
070: * to the iterator will have effect on the original Vector.
071: */
072: public NamedIterator(Vector v) {
073: data = v; // same _pointer_: changes will have effect in original Vector
074: hash = new Hashtable();
075: for (Enumeration e = v.elements(); e.hasMoreElements();) {
076: Named n = (Named) e.nextElement();
077: hash.put(n.getName(), n);
078: }
079: reset();
080: }
081:
082: /**
083: * Special constructor called from Class.
084: */
085: NamedIterator(Vector v, Object type, int modifierMask) {
086: // Using type 'Object' instead of 'java.lang.Class' for parameter type
087: // is a workaround to avoid having to generate fully qualified class names
088: // when applying BeatyJ to this code. (Otherwise, would confuse with class
089: // 'Class' in this package.)
090: this ();
091: for (Enumeration e = v.elements(); e.hasMoreElements();) {
092: Member m = (Member) e.nextElement();
093: int mod = m.getModifier();
094: if (((modifierMask & mod) != 0)
095: && (m.getClass()
096: .isAssignableFrom((java.lang.Class) type))) {
097: data.addElement(m);
098: }
099: }
100: reset();
101: lockReadonly(); // read-only in this mode
102: }
103:
104: /**
105: * Creates a new instance of NamedIterator.
106: */
107: private NamedIterator(boolean dummy) {
108: // dummy constructor for cloning
109: }
110:
111: // ------------------------------------------------------------------------
112: // --- methods ---
113: // ------------------------------------------------------------------------
114: public Object clone() {
115: NamedIterator c = new NamedIterator(true);
116: synchronized (this ) {
117: c.data = (Vector) data.clone();
118: c.hash = (Hashtable) hash.clone();
119: c.pos = pos;
120: c.readonly = readonly;
121: }
122: return c;
123: }
124:
125: /**
126: * Get next element.
127: *
128: * @return The next element, or null if <code>hasMore()==false</code>.
129: */
130: public Named next() {
131: if (hasMore()) {
132: return (Named) data.elementAt(pos++);
133: } else {
134: return null;
135: }
136: }
137:
138: /**
139: * Tests whether there are more elements that can beretrieved using <code>next()</code>.
140: */
141: public boolean hasMore() {
142: return (pos < data.size());
143: }
144:
145: /**
146: * Get an element by name.
147: */
148: public Named find(String n) {
149: return (Named) hash.get(n);
150: }
151:
152: /**
153: * Sets the iterator back to element position 0.
154: */
155: public void reset() {
156: pos = 0;
157: }
158:
159: /**
160: * Tests whether elements can be added to or removed from this iterator.
161: *
162: * @see #addHere
163: * @see #add
164: * @see #remove
165: * @see #removeAll
166: */
167: public boolean isReadOnly() {
168: return readonly;
169: }
170:
171: /**
172: * Adds an element at the current position.
173: */
174: public void addHere(Named n) {
175: if (!readonly) {
176: synchronized (this ) {
177: data.insertElementAt(n, pos);
178: hash.put(n.getName(), n);
179: }
180: }
181: }
182:
183: /**
184: * Adds an element at the end of the iterator-list.
185: */
186: public void add(Named n) {
187: if (!readonly) {
188: synchronized (this ) {
189: data.addElement(n);
190: hash.put(n.getName(), n);
191: }
192: }
193: }
194:
195: /**
196: * Removes an element from the iterator-list.
197: */
198: public void remove(Named n) {
199: if (!readonly) {
200: synchronized (this ) {
201: data.addElement(n);
202: hash.put(n.getName(), n);
203: }
204: }
205: }
206:
207: /**
208: * Removes all elements from the iterator-list.
209: */
210: public void removeAll() {
211: if (!readonly) {
212: synchronized (this ) {
213: data.removeAllElements();
214: hash.clear();
215: }
216: }
217: }
218:
219: /**
220: * Adds all elements of the NamedIterator to this.
221: */
222: public void add(NamedIterator it) {
223: it.reset();
224: synchronized (this ) {
225: while (it.hasMore()) {
226: data.addElement(it.next());
227: }
228: }
229: }
230:
231: /**
232: * Returns the number of elements in the iterator-list.
233: */
234: public int size() {
235: return data.size();
236: }
237:
238: /**
239: * Set this iterator to read-only mode.
240: */
241: void lockReadonly() {
242: readonly = true;
243: }
244:
245: /**
246: * Gets the orignal Vector that stores the iterator-list.
247: */
248: Vector getVector() {
249: return (Vector) data.clone();
250: }
251:
252: } // end NamedIterator
|