/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- package bsh.collection;
- import java.util.Iterator;
- import java.util.Collection;
- import java.util.Enumeration;
- //import java.util.Map;
- import java.lang.reflect.Array;
- /**
- * This is the implementation of:
- * BshIterator - a dynamically loaded extension that supports the collections
- * API supported by JDK1.2 and greater.
- *
- * @author Daniel Leuck
- * @author Pat Niemeyer
- */
- public class CollectionIterator implements bsh.BshIterator
- {
- private Iterator iterator;
- /**
- * Construct a basic CollectionIterator
- *
- * @param The object over which we are iterating
- *
- * @throws java.lang.IllegalArgumentException If the argument is not a
- * supported (i.e. iterable) type.
- *
- * @throws java.lang.NullPointerException If the argument is null
- */
- public CollectionIterator(Object iterateOverMe) {
- iterator = createIterator(iterateOverMe);
- }
- /**
- * Create an iterator over the given object
- *
- * @param iterateOverMe Object of type Iterator, Collection, or types
- * supported by CollectionManager.BasicBshIterator
- *
- * @return an Iterator
- *
- * @throws java.lang.IllegalArgumentException If the argument is not a
- * supported (i.e. iterable) type.
- *
- * @throws java.lang.NullPointerException If the argument is null
- */
- protected Iterator createIterator(Object iterateOverMe)
- {
- if (iterateOverMe==null)
- throw new NullPointerException("Object arguments passed to " +
- "the CollectionIterator constructor cannot be null.");
- if (iterateOverMe instanceof Iterator)
- return (Iterator)iterateOverMe;
- if (iterateOverMe instanceof Collection)
- return ((Collection)iterateOverMe).iterator();
- /*
- Should we be able to iterate over maps?
- if (iterateOverMe instanceof Map)
- return ((Map)iterateOverMe).entrySet().iterator();
- */
- throw new IllegalArgumentException(
- "Cannot enumerate object of type "+iterateOverMe.getClass());
- }
- /**
- * Fetch the next object in the iteration
- *
- * @return The next object
- */
- public Object next() {
- return iterator.next();
- }
-
- /**
- * Returns true if and only if there are more objects available
- * via the <code>next()</code> method
- *
- * @return The next object
- */
- public boolean hasNext() {
- return iterator.hasNext();
- }
- }