Class Lookahead<T>

  • Type Parameters:
    T - the type of the elements kept in the stream
    Direct Known Subclasses:
    LookaheadReader

    public abstract class Lookahead<T>
    extends java.lang.Object
    Abstract data structure for providing streams of items with a lookahead.

    Items provided by subclasses of this are processed one after each other. However, using next() or next(int) one can peek at upcoming items without consuming the current one.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected T endOfInputIndicator
      Once the end of the underlying input was reached, an end of input indicator is created and constantly returned for all calls of current and next.
      protected boolean endReached
      Determines if the end of the underlying data source has been reached.
      protected java.util.List<T> itemBuffer
      Internal buffer containing items which where already created due to lookaheads.
    • Constructor Summary

      Constructors 
      Constructor Description
      Lookahead()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T consume()
      Removes and returns the current item from the stream.
      void consume​(int numberOfItems)
      Consumes (removes) numberOfItems at once.
      T current()
      Returns the item the stream is currently pointing at.
      protected abstract T endOfInput()
      Creates the end of input indicator item.
      protected abstract T fetch()
      Fetches the next item from the stream.
      T next()
      Returns the next item after the current one in the stream.
      T next​(int offset)
      Returns the next n-th item in the stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • itemBuffer

        protected java.util.List<T> itemBuffer
        Internal buffer containing items which where already created due to lookaheads.
      • endReached

        protected boolean endReached
        Determines if the end of the underlying data source has been reached.
      • endOfInputIndicator

        protected T endOfInputIndicator
        Once the end of the underlying input was reached, an end of input indicator is created and constantly returned for all calls of current and next.
    • Constructor Detail

      • Lookahead

        public Lookahead()
    • Method Detail

      • next

        @NotNull
        public T next()
               throws ParseException
        Returns the next item after the current one in the stream.

        This method does not change the internal state. Therefore it can be called several times and will always return the same result.

        Returns:
        The next item in the stream. This will be the current item, after a call to consume()you
        Throws:
        ParseException - If there is an exception during parsing.
      • current

        @NotNull
        public T current()
                  throws ParseException
        Returns the item the stream is currently pointing at.

        This method does not change the internal state. Therefore it can be called several times and will always return the same result.

        Returns:
        the item the stream is currently pointing at.
        Throws:
        ParseException - If there is an exception during parsing.
      • next

        @NotNull
        public T next​(int offset)
               throws ParseException
        Returns the next n-th item in the stream.

        Calling this method with 0 as parameter, will return the current item. Calling it with 1 will return the same item as a call to next().

        This method does not change the internal state. Therefore it can be called several times and will always return the same result.

        Parameters:
        offset - the number of items to skip
        Returns:
        the n-th item in the stream
        Throws:
        ParseException - If there is an exception during parsing.
      • consume

        @NotNull
        public T consume()
                  throws ParseException
        Removes and returns the current item from the stream.

        After this method was called, all calls to current() will then return the item, which was previously returned by next()

        Returns:
        the item which is being removed from the stream
        Throws:
        ParseException - If there is an exception during parsing.
      • consume

        public void consume​(int numberOfItems)
                     throws ParseException
        Consumes (removes) numberOfItems at once.

        Removes the given number of items from the stream.

        Parameters:
        numberOfItems - the number of items to remove
        Throws:
        ParseException - If there is an exception during parsing.
      • endOfInput

        @NotNull
        protected abstract T endOfInput()
        Creates the end of input indicator item.

        This method will be only called once, as the indicator is cached.

        Returns:
        a special item which marks the end of the input
      • fetch

        @Nullable
        protected abstract T fetch()
                            throws ParseException
        Fetches the next item from the stream.
        Returns:
        the next item in the stream or null to indicate that the end was reached
        Throws:
        ParseException - If there is an exception during parsing.