Class HeadTailAccumulator<T>


  • final class HeadTailAccumulator<T>
    extends java.lang.Object
    Accumulates the values in a stream or iterable, keeping the first and last elements and discarding everything in between.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Queue<T> head
      The first elements seen.
      private java.util.Queue<T> tail
      The elements seen most recently, excluding anything already on head.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void add​(T element)
      Adds an element to the accumulator, possibly displacing an older element.
      (package private) java.util.stream.Stream<T> stream()
      Converts the accumulated elements into a stream.
      • Methods inherited from class java.lang.Object

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

      • head

        private final java.util.Queue<T> head
        The first elements seen.
      • tail

        private final java.util.Queue<T> tail
        The elements seen most recently, excluding anything already on head.
    • Constructor Detail

      • HeadTailAccumulator

        HeadTailAccumulator​(int headCapacity,
                            int tailCapacity)
        Creates a new HeadTailAccumulator.
        Parameters:
        headCapacity - the maximum number of elements to retain from the start of the stream
        tailCapacity - the maximum number of elements to retain from the end of the stream.
        Throws:
        java.lang.IllegalArgumentException - if either argument is negative
    • Method Detail

      • add

        void add​(T element)
        Adds an element to the accumulator, possibly displacing an older element.
        Parameters:
        element - the element to add (may be null)
      • stream

        java.util.stream.Stream<T> stream()
        Converts the accumulated elements into a stream.
        Returns:
        the head and tail concatenated