PageRenderTime 150ms CodeModel.GetById 109ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/collection/CollectionIterator.java

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