PageRenderTime 65ms CodeModel.GetById 62ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/collection/CollectionIterator.java

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