01: package org.gjt.sp.jedit.bsh.collection;
02:
03: import java.util.Iterator;
04: import java.util.Collection;
05:
06: //import java.util.Map;
07:
08: /**
09: * This is the implementation of:
10: * BshIterator - a dynamically loaded extension that supports the collections
11: * API supported by JDK1.2 and greater.
12: *
13: * @author Daniel Leuck
14: * @author Pat Niemeyer
15: */
16: public class CollectionIterator implements
17: org.gjt.sp.jedit.bsh.BshIterator {
18: private Iterator iterator;
19:
20: /**
21: * Construct a basic CollectionIterator
22: *
23: * @param The object over which we are iterating
24: *
25: * @throws java.lang.IllegalArgumentException If the argument is not a
26: * supported (i.e. iterable) type.
27: *
28: * @throws java.lang.NullPointerException If the argument is null
29: */
30: public CollectionIterator(Object iterateOverMe) {
31: iterator = createIterator(iterateOverMe);
32: }
33:
34: /**
35: * Create an iterator over the given object
36: *
37: * @param iterateOverMe Object of type Iterator, Collection, or types
38: * supported by CollectionManager.BasicBshIterator
39: *
40: * @return an Iterator
41: *
42: * @throws java.lang.IllegalArgumentException If the argument is not a
43: * supported (i.e. iterable) type.
44: *
45: * @throws java.lang.NullPointerException If the argument is null
46: */
47: protected Iterator createIterator(Object iterateOverMe) {
48: if (iterateOverMe == null)
49: throw new NullPointerException(
50: "Object arguments passed to "
51: + "the CollectionIterator constructor cannot be null.");
52:
53: if (iterateOverMe instanceof Iterator)
54: return (Iterator) iterateOverMe;
55:
56: if (iterateOverMe instanceof Collection)
57: return ((Collection) iterateOverMe).iterator();
58:
59: /*
60: Should we be able to iterate over maps?
61: if (iterateOverMe instanceof Map)
62: return ((Map)iterateOverMe).entrySet().iterator();
63: */
64:
65: throw new IllegalArgumentException(
66: "Cannot enumerate object of type "
67: + iterateOverMe.getClass());
68: }
69:
70: /**
71: * Fetch the next object in the iteration
72: *
73: * @return The next object
74: */
75: public Object next() {
76: return iterator.next();
77: }
78:
79: /**
80: * Returns true if and only if there are more objects available
81: * via the <code>next()</code> method
82: *
83: * @return The next object
84: */
85: public boolean hasNext() {
86: return iterator.hasNext();
87: }
88: }
|