1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.toolkit.util;
20
21 import java.util.*;
22
23 /*** Static help class that provides a number of tools for working with
24 * iterators such as making the unmodifiable or wraping arrays.
25 *
26 * @author Dennis Zikovic
27 * @version 1.00
28 *
29 *//*
30 *
31 * WHEN WHO WHY & WHAT
32 * -----------------------------------------------------------------------------
33 * 2002-08-23 Dennis Zikovic Creation
34 */
35 public final class Iterators
36 {
37
38 public static ListIterator EMPTY_ITERATOR = new ArrayIterator(null);
39
40
41 public static ListIterator unmodifiableIterator(ListIterator it)
42 {
43 return new UnmodifiableListIterator(it);
44 }
45
46 public static Iterator unmodifiableIterator(Iterator it)
47 {
48 return new UnmodifiableIterator(it);
49 }
50
51 public static Iterator iterate(Enumeration enum)
52 {
53 if(enum==null)
54 return EMPTY_ITERATOR;
55 else
56 return new EnumerationIterator(enum);
57 }
58
59 public static ListIterator iterate(Object[] array)
60 {
61 return new ArrayIterator(array);
62 }
63
64 public static ListIterator iterate(Object obj)
65 {
66 return new ArrayIterator(new Object[] {obj});
67 }
68
69 public static ListIterator iterate(Object obj1, Object obj2)
70 {
71 return new ArrayIterator(new Object[] {obj1, obj2});
72 }
73
74
75
76 public static class EnumerationIterator implements Iterator, Enumeration
77 {
78
79 protected Enumeration mEnumeration;
80
81
82 public EnumerationIterator(Enumeration enum)
83 {
84 mEnumeration = enum;
85 }
86
87
88 public boolean hasNext()
89 {
90 return mEnumeration.hasMoreElements();
91 }
92
93 public Object next()
94 {
95 return mEnumeration.nextElement();
96 }
97
98 public void remove()
99 {
100 throw new java.lang.UnsupportedOperationException();
101 }
102
103
104 public boolean hasMoreElements()
105 {
106 return mEnumeration.hasMoreElements();
107 }
108
109 public Object nextElement()
110 {
111 return mEnumeration.nextElement();
112 }
113 }
114
115 public static class ArrayIterator implements ListIterator, Enumeration
116 {
117
118 protected Object[] mObjectArray;
119 protected int mArrayIndex;
120
121
122 public ArrayIterator(Object[] objectArray)
123 {
124 mObjectArray = objectArray;
125 mArrayIndex = -1;
126 }
127
128
129 public void add(Object o)
130 {
131 throw new java.lang.UnsupportedOperationException();
132 }
133
134 public boolean hasNext()
135 {
136 return mObjectArray!=null && mArrayIndex<mObjectArray.length-1;
137 }
138
139 public boolean hasPrevious()
140 {
141 return mArrayIndex>0;
142 }
143
144 public Object next()
145 {
146 return mObjectArray[++mArrayIndex];
147 }
148
149 public int nextIndex()
150 {
151 return mArrayIndex+1;
152 }
153
154 public Object previous()
155 {
156 return mObjectArray[--mArrayIndex];
157 }
158
159 public int previousIndex()
160 {
161 return mArrayIndex-1;
162 }
163
164 public void remove()
165 {
166 throw new java.lang.UnsupportedOperationException();
167 }
168
169 public void set(Object o)
170 {
171 mObjectArray[mArrayIndex] = o;
172 }
173
174
175 public boolean hasMoreElements()
176 {
177 return this.hasNext();
178 }
179
180 public Object nextElement()
181 {
182 return this.next();
183 }
184 }
185
186 public static class UnmodifiableIterator implements Iterator, Enumeration
187 {
188
189 protected Iterator mIterator;
190
191
192 public UnmodifiableIterator(Iterator iterator)
193 {
194 mIterator = iterator;
195 }
196
197
198 public boolean hasNext()
199 {
200 return mIterator.hasNext();
201 }
202
203 public Object next()
204 {
205 return mIterator.next();
206 }
207
208 public void remove()
209 {
210 throw new java.lang.UnsupportedOperationException();
211 }
212
213
214 public boolean hasMoreElements()
215 {
216 return this.hasNext();
217 }
218
219 public Object nextElement()
220 {
221 return this.next();
222 }
223 }
224
225 public static class UnmodifiableListIterator implements ListIterator, Enumeration
226 {
227
228 protected ListIterator mIterator;
229
230
231 public UnmodifiableListIterator(ListIterator iterator)
232 {
233 mIterator = iterator;
234 }
235
236
237 public void add(Object o)
238 {
239 throw new java.lang.UnsupportedOperationException();
240 }
241
242 public boolean hasNext()
243 {
244 return mIterator.hasNext();
245 }
246
247 public boolean hasPrevious()
248 {
249 return mIterator.hasPrevious();
250 }
251
252 public Object next()
253 {
254 return mIterator.next();
255 }
256
257 public int nextIndex()
258 {
259 return mIterator.nextIndex();
260 }
261
262 public Object previous()
263 {
264 return mIterator.previous();
265 }
266
267 public int previousIndex()
268 {
269 return mIterator.previousIndex();
270 }
271
272 public void remove()
273 {
274 throw new java.lang.UnsupportedOperationException();
275 }
276
277 public void set(Object o)
278 {
279 throw new java.lang.UnsupportedOperationException();
280 }
281
282
283 public boolean hasMoreElements()
284 {
285 return this.hasNext();
286 }
287
288 public Object nextElement()
289 {
290 return this.next();
291 }
292 }
293
294 public abstract static class WrapIterator implements Iterator, Enumeration
295 {
296
297 protected Iterator mIterator;
298
299
300 public WrapIterator(Iterator iterator)
301 {
302 mIterator = iterator;
303 }
304
305
306 public boolean hasNext()
307 {
308 return mIterator.hasNext();
309 }
310
311 public Object next()
312 {
313 return mIterator.next();
314 }
315
316 public void remove()
317 {
318 mIterator.remove();
319 }
320
321
322 public boolean hasMoreElements()
323 {
324 return this.hasNext();
325 }
326
327 public Object nextElement()
328 {
329 return this.next();
330 }
331 }
332
333 public abstract static class FilterIterator implements Iterator, Enumeration
334 {
335
336 protected Iterator mIterator;
337 protected Object mNextObject;
338
339
340 public FilterIterator(Iterator iterator)
341 {
342 mIterator = iterator;
343 mNextObject = nextAccepted();
344 }
345
346 public FilterIterator(Collection collection)
347 {
348 this(collection.iterator());
349 }
350
351 protected FilterIterator()
352 {
353 }
354
355
356 public boolean hasNext()
357 {
358 return mNextObject!=null;
359 }
360
361 public Object next()
362 {
363 Object currentObject = mNextObject;
364 mNextObject = nextAccepted();
365 return currentObject;
366 }
367
368 public void remove()
369 {
370 throw new java.lang.UnsupportedOperationException();
371 }
372
373
374 public boolean hasMoreElements()
375 {
376 return this.hasNext();
377 }
378
379 public Object nextElement()
380 {
381 return this.next();
382 }
383
384
385
386 /*** Abstract method that should return true if the provided object
387 * should be included in the itteration.
388 */
389 protected abstract boolean accept(Object object);
390
391
392 protected Object nextAccepted()
393 {
394 if(!mIterator.hasNext())
395 return null;
396
397 Object nextObject = mIterator.next();
398 while(!this.accept(nextObject) && mIterator.hasNext())
399 nextObject = mIterator.next();
400 return nextObject;
401 }
402 }
403 }