Interface SmartIterator<E extends @NonNull Object>

Type Parameters:
E - The element type.
All Superinterfaces:
Iterator<E>
All Known Subinterfaces:
SmartIterator.Detachable<E>, StringSplitter.Async.Iterator, StringSplitter.Iterator
All Known Implementing Classes:
SmartIterator.ListIterator, SmartIterator.Wrapper

public interface SmartIterator<E extends @NonNull Object> extends Iterator<E>
An iterator with extra capabilities. Note that it does not support null elements, and is not thread-safe by default.
Since:
1.0
Version:
1.0
  • Field Details

    • NO_MORE_ELEMENTS_ERROR

      static final String NO_MORE_ELEMENTS_ERROR
      Exception message for when there are no more elements.
      See Also:
  • Method Details

    • empty

      static <E extends @NonNull Object> SmartIterator.Detachable<E> empty()
      Returns an empty iterator.
      Type Parameters:
      E - The element type.
      Returns:
      An empty iterator.
    • peek

      @Pure @Nullable E peek()
      Retrives the element that will be returned by the next call to Iterator.next(), without advancing the iterator.
      Returns:
      The next element, or null if there are no elements remaining.
    • toSpliterator

      @SideEffectFree default Spliterator<E> toSpliterator()
      Returns a spliterator that iterates over the remaining elements to be traversed by this iterator.

      Note that this interface makes no promises as to whether it is safe to continue using this iterator while using the returned spliterator; it is left as an implementation detail.

      Returns:
      The spliterator.
      Implementation Requirements:
      The default implementation creates a spliterator backed by this iterator; as such, their state is interdependent, and continuing to use this iterator results in undefined behavior.
    • toStream

      @SideEffectFree default Stream<E> toStream()
      Returns a stream that contains the remaining elements to be traversed by this iterator.

      Note that this interface makes no promises as to whether it is safe to continue using this iterator while using the returned stream; it is left as an implementation detail.

      Returns:
      The stream.
      Implementation Requirements:
      The default implementation creates a stream backed by this iterator; as such, their state is interdependent, and continuing to use this iterator results in undefined behavior.
    • toFlux

      @SideEffectFree default Flux<E> toFlux()
      Returns a flux that issues the remaining elements to be traversed by this iterator.

      Note that this interface makes no promises as to whether it is safe to continue using this iterator while using the returned flux; it is left as an implementation detail.

      Returns:
      The flux.
      Implementation Requirements:
      The default implementation creates a stream backed by this iterator; as such, their state is interdependent, and continuing to use this iterator results in undefined behavior.
    • hasNext

      @EnsuresNonNullIf(expression="peek()", result=true) default boolean hasNext()
      Specified by:
      hasNext in interface Iterator<E extends @NonNull Object>
    • from

      @SideEffectFree static <E extends @NonNull Object> SmartIterator<E> from(Iterator<E> backing)
      Creates a smart iterator backed by the given iterator.
      Type Parameters:
      E - The element type.
      Parameters:
      backing - The backing iterator.
      Returns:
      The smart iterator.
    • from

      @SideEffectFree static <E extends @NonNull Object> SmartIterator.Detachable<E> from(List<E> list)
      Creates a smart iterator that iterates over the contents of the given list.
      Type Parameters:
      E - The element type.
      Parameters:
      list - The list to iterate over.
      Returns:
      The smart iterator.