Interface PStack<E>

Type Parameters:
E - the type of elements maintained by this stack
All Superinterfaces:
Iterable<E>

public interface PStack<E> extends Iterable<E>
Persistent (functional) Stack.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    anyMatch(Predicate<E> predicate)
    Test given predicate on elements and return true if any of elements matches the predicate
    boolean
     
    static <E> PStack<E>
    of()
    Returns a persistent stack containing zero elements.
    static <E> PStack<E>
    of(E e1)
    Returns a persistent stack containing one element.
    static <E> PStack<E>
    of(E e1, E e2)
    Returns a persistent stack containing two elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3)
    Returns a persistent stack containing three elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4)
    Returns a persistent stack containing four elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4, E e5)
    Returns a persistent stack containing five elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4, E e5, E e6)
    Returns a persistent stack containing six elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
    Returns a persistent stack containing seven elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
    Returns a persistent stack containing eight elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
    Returns a persistent stack containing nine elements, pushed left to right.
    static <E> PStack<E>
    of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
    Returns a persistent stack containing ten elements, pushed left to right.
     
    peek(int i)
     
    pop()
     
    push(E e)
     
    int
    Naive implementation has O(n) time complexity, where n is number of elements.
    default Stream<E>
     
     

    Methods inherited from interface java.lang.Iterable

    forEach, iterator, spliterator
  • Method Details

    • push

      PStack<E> push(E e)
      Returns:
      new stack with added element
    • peek

      E peek()
      Returns:
      element at the top of this stack
      Throws:
      IllegalStateException - if this stack is empty.
    • peek

      E peek(int i)
      Parameters:
      i - - index of element to be returned, 0 means top of the stack
      Returns:
      i-th element from top of the stack
      Throws:
      IllegalStateException - if stack has less than i elements
    • pop

      PStack<E> pop()
      Returns:
      new stack with removed element
      Throws:
      IllegalStateException - if this stack is empty.
    • isEmpty

      boolean isEmpty()
      Returns:
      true if this stack contains no elements
    • anyMatch

      boolean anyMatch(Predicate<E> predicate)
      Test given predicate on elements and return true if any of elements matches the predicate
      Parameters:
      predicate - predicate to be tested
      Returns:
      true if any of the stack elements satisfies the predicate
    • size

      int size()
      Naive implementation has O(n) time complexity, where n is number of elements. More clever implementation could take advantage of PStack's immutability
      Returns:
      number of elements in the stack
    • toString

      String toString()
      Overrides:
      toString in class Object
      Returns:
      a string representation of this stack
    • stream

      default Stream<E> stream()
      Returns:
      stream of the stack's elements, starting at the top of the stack
    • of

      static <E> PStack<E> of()
      Returns a persistent stack containing zero elements.
      Type Parameters:
      E - the PStack's element type
      Returns:
      an empty PStack
    • of

      static <E> PStack<E> of(E e1)
      Returns a persistent stack containing one element.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the single element
      Returns:
      a PStack containing the specified element
    • of

      static <E> PStack<E> of(E e1, E e2)
      Returns a persistent stack containing two elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3)
      Returns a persistent stack containing three elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4)
      Returns a persistent stack containing four elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4, E e5)
      Returns a persistent stack containing five elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      e5 - the fifth element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
      Returns a persistent stack containing six elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      e5 - the fifth element
      e6 - the sixth element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
      Returns a persistent stack containing seven elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      e5 - the fifth element
      e6 - the sixth element
      e7 - the seventh element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
      Returns a persistent stack containing eight elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      e5 - the fifth element
      e6 - the sixth element
      e7 - the seventh element
      e8 - the eighth element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
      Returns a persistent stack containing nine elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      e5 - the fifth element
      e6 - the sixth element
      e7 - the seventh element
      e8 - the eighth element
      e9 - the ninth element
      Returns:
      a PStack containing the specified elements
    • of

      static <E> PStack<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
      Returns a persistent stack containing ten elements, pushed left to right.
      Type Parameters:
      E - the PStack's element type
      Parameters:
      e1 - the first element
      e2 - the second element
      e3 - the third element
      e4 - the fourth element
      e5 - the fifth element
      e6 - the sixth element
      e7 - the seventh element
      e8 - the eighth element
      e9 - the ninth element
      e10 - the tenth element
      Returns:
      a PStack containing the specified elements