Package org.midheaven.collections
Interface Enumerator<T>
- Type Parameters:
T- type of the elements in the Enumerator
- All Known Implementing Classes:
PipeEnumerator
public interface Enumerator<T>
Defines the contract for Enumerator. An Enumerator has the same purpose as an
Iterator
but works differently, facilitating logics. The Enumerator merges the Iterator.hasNext() and Iterator.next()
methods in a single moveNext(). If the operations is successful the element can be read with moveNext().
Otherwise, trying to read moveNext() will throw a IllegalStateException.
Also, the Enumerator has a length() attribute that can be infinite, finite or unknown.-
Method Summary
Modifier and TypeMethodDescriptioncurrent()The selected item.static <T> Enumerator<T> empty()Returns an empty instance.static <T> Enumerator<T> fromIterator(Iterator<T> iterator) Creates an Enumerator from anIteratorthat can be infinite.static <T> Enumerator<T> fromIterator(Iterator<T> iterator, Int size) Creates an Enumerator from anIteratorthat can be infinite, but limites it to a fixed size.length()TheLengthof theEnumerator.booleanmoveNext()Move the enumerator to the next item.static <T> Enumerator<T> single(T value) Creates an Enumerator with just one single valueReturns anIteratorcorresponding with thisEnumerator
-
Method Details
-
empty
Returns an empty instance.- Returns:
- the result of empty
-
fromIterator
Creates an Enumerator from anIteratorthat can be infinite.- Parameters:
iterator- theIterator- Returns:
- the resulting Enumerator
-
fromIterator
Creates an Enumerator from anIteratorthat can be infinite, but limites it to a fixed size.- Parameters:
iterator- theIterator- Returns:
- the resulting Enumerator
-
single
Creates an Enumerator with just one single value- Parameters:
value- the value- Returns:
- the resulting Enumerator
-
moveNext
boolean moveNext()Move the enumerator to the next item. All enumerator are create positioned before the first item.- Returns:
- true if the move was successful, and
current()is safe to call.
-
current
T current()The selected item. If this method is called before calling moveNext, or no current element exists, anIllegalStateExceptionis thrown.- Returns:
- the current element
- Throws:
IllegalStateException- if called before callingmoveNext
-
length
Length length()- Returns:
- The
Lengthof theEnumerator
-
toIterator
Returns anIteratorcorresponding with thisEnumerator- Returns:
- the resulting
Iterator
-