|
Holger's Java API |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.antelmann.util.AbstractIterator<E>
public abstract class AbstractIterator<E>
makes it very easy to implement both, Enumeration and Iterator.
The only abstract method is getNextElement().
Additionally, this class also implements Iterable for most code flexibility.
This method is to supply the next object until it throws an exception,
which marks that the last object was reached.
By default, returning null also marks the end of the iteration;
in consequence, the next() never returns null in this case.
You can change this behavior to allow an enumeration to return null as a
valid element by calling setAllowNull(true) and you can also
call setPropagateException(boolean) to have an exception thrown
instead of having it mark the end of an iteration.
In addition, this class supplies convenient static methods to convert
an Enumeration to an Iterator and vice versa.
| Constructor Summary | |
|---|---|
protected |
AbstractIterator()
|
protected |
AbstractIterator(boolean allowNull)
|
| Method Summary | ||
|---|---|---|
boolean |
allowsNull()
if false (the default), next() never returns null
(as null marks the end of the iteration). |
|
static
|
asIterable(Iterator<T> i)
|
|
void |
close()
closes this iterator by calling stopIteration(). |
|
static
|
concat(Enumeration<X> e1,
Enumeration<Y> e2)
|
|
static
|
concat(Iterator<X> i1,
Iterator<Y> i2)
|
|
static
|
convert(Enumeration<T> e,
PatternExtractor<? super T,N> pe)
|
|
static
|
convert(Iterator<T> i,
PatternExtractor<? super T,N> pe)
|
|
static int |
count(Iterator<?> i)
|
|
E |
currentElement()
allows to access the current element over and over again without advancing the cursor. |
|
static
|
emptyEnumeration()
|
|
static
|
emptyIterable()
|
|
static
|
emptyIterator()
|
|
static
|
enumerate(Iterator<? extends F> i)
converts an Iterator |
|
static
|
filter(Enumeration<F> e,
Filter<? super F> filter)
|
|
static
|
filter(Iterator<F> i,
Filter<? super F> filter)
remove() is not supported on the returned iterator |
|
List<E> |
getAll()
|
|
long |
getCount()
returns the number of elements that have been returned through next so far |
|
Exception |
getEndCondition()
indicates the cause for this iterator to end |
|
Filter<? super E> |
getFilter()
|
|
protected abstract E |
getNextElement()
if no more Element is available, this method is to return null or throw any Exception. |
|
boolean |
hasMoreElements()
|
|
boolean |
hasNext()
|
|
static
|
iterate(Enumeration<? extends F> e)
converts an Enumeration into an Iterator |
|
static
|
iterate(T[] array)
|
|
Iterator<E> |
iterator()
|
|
static
|
list(Enumeration<F> e)
|
|
static
|
list(Iterable<F> i)
|
|
static
|
list(Iterator<E> it,
int maxElements)
if maxElements is negative, the entire iteration is returned |
|
static
|
list(Iterator<F> i)
|
|
static
|
monitor(Enumeration<T> e,
Monitor monitor)
|
|
static
|
monitor(Iterator<T> i,
Monitor monitor)
increments the monitor upon calling next() and returns false upon hasNext() if the monitor is disabled |
|
E |
next()
|
|
E |
nextElement()
|
|
static
|
process(Enumeration<T> e,
Processor<T,R> p)
allows to wrap a given Enumeration so that each enumerated object is processed (possibly even substituted) by the given Processor |
|
static
|
process(Iterator<T> i,
Processor<T,R> p)
allows to wrap a given Iterator so that each iterated object is processed (possibly even substituted) by the given Processor |
|
boolean |
propagatesException()
if true, an exception thrown by getNextElement() will be thrown as an
IterationException back to the caller of any method of the Iterator |
|
void |
remove()
throws UnsupportedOperationException (unless overridden, of course). |
|
static
|
reverseEnumeration(Enumeration<F> e)
reverses the given Enumeration; requires to fully load the given Enumeration into memory |
|
void |
setAllowNull(boolean flag)
allows to set whether null is an allowed value for next()
or if marks the end of an iteration. |
|
void |
setFilter(Filter<? super E> filter)
if a non-null filter is set, only those elements that pass the filter will be returned by next(). |
|
void |
setPropagateException(boolean flag)
allows to have Exceptions thrown during getNextElement() propagated as IterationExceptions back to the caller |
|
static
|
sortEnumeration(Enumeration<? extends F> e)
sorts the given Enumeration by the natural order of its elements; requires to fully load the given Enumeration into memory |
|
static
|
sortEnumeration(Enumeration<F> e,
Comparator<F> c)
sorts the given Enumeration by the order imposed by the given comparator; requires to fully load the given Enumeration into memory |
|
protected void |
stopIteration()
allows to stop an iteration gracefully even when it allows null and propagates Exceptions. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
protected AbstractIterator()
protected AbstractIterator(boolean allowNull)
setAllowNull(boolean)| Method Detail |
|---|
public Iterator<E> iterator()
iterator in interface Iterable<E>public void setFilter(Filter<? super E> filter)
next().
public Filter<? super E> getFilter()
public boolean allowsNull()
next() never returns null
(as null marks the end of the iteration).
setAllowNull(boolean)public void setAllowNull(boolean flag)
next()
or if marks the end of an iteration.
If set to true, the iteration only ends if getNextElement()
throws an exception.
public boolean propagatesException()
getNextElement() will be thrown as an
IterationException back to the caller of any method of the Iterator>or
Enumeration interface.
If false (the default behavior), this iterator will simply assume that
there are no more elements and otherwise ignore the Exception.
IterationExceptionpublic void setPropagateException(boolean flag)
IterationException
protected abstract E getNextElement()
throws Exception
next().
Exception
public void close()
throws IOException
stopIteration().
You can use this method to clean up any resources there may be needed for this iterator.
Note, however, that this method is not called automatically;
so if you need this method to be called after the iteration is through (or only partially used),
you have to call this method yourself (or you overwrite this implementation accordingly).
Also, when overriding this method, you should call super.close().
close in interface CloseableIOExceptionprotected void stopIteration()
public E next()
next in interface Iterator<E>public long getCount()
public E currentElement()
throws IllegalStateException
IllegalStateException - if this method is called before next()
has been called the first time, it returns null.public E nextElement()
nextElement in interface Enumeration<E>public boolean hasMoreElements()
hasMoreElements in interface Enumeration<E>public boolean hasNext()
hasNext in interface Iterator<E>public List<E> getAll()
public Exception getEndCondition()
throws IllegalStateException
IllegalStateException - if hasNext() is truepublic void remove()
remove in interface Iterator<E>public static <T> Iterator<T> iterate(T[] array)
public static <F> Iterator<F> iterate(Enumeration<? extends F> e)
public static <F> Enumeration<F> enumerate(Iterator<? extends F> i)
public static <F extends Comparable<? super F>> Enumeration<F> sortEnumeration(Enumeration<? extends F> e)
public static <F> Enumeration<F> sortEnumeration(Enumeration<F> e,
Comparator<F> c)
public static <F> Enumeration<F> reverseEnumeration(Enumeration<F> e)
public static <E> ArrayList<E> list(Iterator<E> it,
int maxElements)
public static <F> List<F> list(Iterable<F> i)
public static <F> ArrayList<F> list(Iterator<F> i)
public static <F> ArrayList<F> list(Enumeration<F> e)
Collections.list(Enumeration)
public static <F> AbstractIterator<F> filter(Enumeration<F> e,
Filter<? super F> filter)
public static <F> AbstractIterator<F> filter(Iterator<F> i,
Filter<? super F> filter)
public static <T> Enumeration<T> monitor(Enumeration<T> e,
Monitor monitor)
public static <T> Iterator<T> monitor(Iterator<T> i,
Monitor monitor)
public static int count(Iterator<?> i)
public static <T,R extends RuntimeException> Enumeration<T> process(Enumeration<T> e,
Processor<T,R> p)
public static <T,R extends RuntimeException> Iterator<T> process(Iterator<T> i,
Processor<T,R> p)
public static <T,N> Enumeration<N> convert(Enumeration<T> e,
PatternExtractor<? super T,N> pe)
public static <T,N> Iterator<N> convert(Iterator<T> i,
PatternExtractor<? super T,N> pe)
public static <T,X extends T,Y extends T> Enumeration<T> concat(Enumeration<X> e1,
Enumeration<Y> e2)
public static <T,X extends T,Y extends T> Iterator<T> concat(Iterator<X> i1,
Iterator<Y> i2)
public static <T> Enumeration<T> emptyEnumeration()
public static <T> Iterator<T> emptyIterator()
public static <T> Iterable<T> emptyIterable()
public static <T> Iterable<T> asIterable(Iterator<T> i)
|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||