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 Details

    • empty

      static <T> Enumerator<T> empty()
      Returns an empty instance.
      Returns:
      the result of empty
    • fromIterator

      static <T> Enumerator<T> fromIterator(Iterator<T> iterator)
      Creates an Enumerator from an Iterator that can be infinite.
      Parameters:
      iterator - the Iterator
      Returns:
      the resulting Enumerator
    • fromIterator

      static <T> Enumerator<T> fromIterator(Iterator<T> iterator, Int size)
      Creates an Enumerator from an Iterator that can be infinite, but limites it to a fixed size.
      Parameters:
      iterator - the Iterator
      Returns:
      the resulting Enumerator
    • single

      static <T> Enumerator<T> single(T value)
      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, an IllegalStateException is thrown.
      Returns:
      the current element
      Throws:
      IllegalStateException - if called before calling moveNext
    • length

      Length length()
      The Length of the Enumerator. An Enumerators length can be infinite.
      Returns:
      The Length of the Enumerator
    • toIterator

      default Iterator<T> toIterator()
      Returns an Iterator corresponding with this Enumerator
      Returns:
      the resulting Iterator