Class Lazy.LazyIterator<T>

  • Type Parameters:
    T - the type of the elements
    All Implemented Interfaces:
    Iterator<T>
    Enclosing class:
    Lazy

    private static class Lazy.LazyIterator<T>
    extends Object
    implements Iterator<T>
    A lazy iterator using a supplier function for generating the elements lazily.
    • Field Detail

      • supplier

        private final Supplier<Optional<T>> supplier
        The supplier function; supplies an optional value with an empty optional if no more values exist.
      • optNext

        private Optional<T> optNext
        An optional with the next value; empty if no value exists; null if a next value has to be generated
    • Constructor Detail

      • LazyIterator

        private LazyIterator​(Supplier<Optional<T>> supplier)
        Private constructor.
        Parameters:
        supplier - the supplier function
    • Method Detail

      • hasNext

        public boolean hasNext()
        Tests if a next value exists. A next value exists if the optional value optNext is not empty. Tests the optNext value is null and then uses the supplier function to create a new optional value.
        Specified by:
        hasNext in interface Iterator<T>
        Returns:
        true if there is a next value
      • next

        public T next()
        Returns the next value, if exists. Throws a NoSuchElementException if no more element exists.
        Specified by:
        next in interface Iterator<T>
        Returns:
        the next value