Class ConsPStack<E>

Type Parameters:
E -
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>, PCollection<E>, PSequence<E>, PStack<E>

public final class ConsPStack<E> extends AbstractUnmodifiableSequentialList<E> implements PStack<E>, Serializable
A simple persistent stack.

This implementation is thread-safe (assuming Java's AbstractSequentialList is thread-safe), although its iterators may not be.

See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • EMPTY

      private static final ConsPStack<Object> EMPTY
    • first

      private final E first
    • rest

      private final ConsPStack<E> rest
    • size

      private final int size
  • Constructor Details

    • ConsPStack

      private ConsPStack()
    • ConsPStack

      private ConsPStack(E first, ConsPStack<E> rest)
  • Method Details

    • empty

      public static <E> ConsPStack<E> empty()
      Type Parameters:
      E -
      Returns:
      an empty stack
    • singleton

      public static <E> ConsPStack<E> singleton(E e)
      Type Parameters:
      E -
      Parameters:
      e -
      Returns:
      empty().plus(e)
    • from

      public static <E> ConsPStack<E> from(Collection<? extends E> list)
      Type Parameters:
      E -
      Parameters:
      list -
      Returns:
      a stack consisting of the elements of list in the order of list.iterator()
    • from

      private static <E> ConsPStack<E> from(Iterator<? extends E> i)
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class AbstractCollection<E>
    • listIterator

      public ListIterator<E> listIterator(int index)
      Specified by:
      listIterator in interface List<E>
      Specified by:
      listIterator in class AbstractSequentialList<E>
    • subList

      public ConsPStack<E> subList(int start, int end)
      Specified by:
      subList in interface List<E>
      Specified by:
      subList in interface PSequence<E>
      Specified by:
      subList in interface PStack<E>
      Overrides:
      subList in class AbstractList<E>
    • plus

      public ConsPStack<E> plus(E e)
      Description copied from interface: PStack
      Returns a stack consisting of the elements of this with e prepended.
      Specified by:
      plus in interface PCollection<E>
      Specified by:
      plus in interface PSequence<E>
      Specified by:
      plus in interface PStack<E>
      Parameters:
      e -
      Returns:
      a collection which contains e and all of the elements of this
    • plusAll

      public ConsPStack<E> plusAll(Collection<? extends E> list)
      Description copied from interface: PStack
      Returns a stack consisting of the elements of this with list prepended in reverse.
      Specified by:
      plusAll in interface PCollection<E>
      Specified by:
      plusAll in interface PSequence<E>
      Specified by:
      plusAll in interface PStack<E>
      Parameters:
      list -
      Returns:
      a collection which contains all of the elements of list and this
    • plus

      public ConsPStack<E> plus(int i, E e)
      Specified by:
      plus in interface PSequence<E>
      Specified by:
      plus in interface PStack<E>
      Parameters:
      i -
      e -
      Returns:
      a sequence consisting of the elements of this with e inserted at index i.
    • plusAll

      public ConsPStack<E> plusAll(int i, Collection<? extends E> list)
      Specified by:
      plusAll in interface PSequence<E>
      Specified by:
      plusAll in interface PStack<E>
      Parameters:
      i -
      list -
      Returns:
      a sequence consisting of the elements of this with list inserted at index i.
    • minus

      public ConsPStack<E> minus(Object e)
      Description copied from interface: PSequence
      Returns a sequence consisting of the elements of this without the first occurrence of e.
      Specified by:
      minus in interface PCollection<E>
      Specified by:
      minus in interface PSequence<E>
      Specified by:
      minus in interface PStack<E>
      Parameters:
      e -
      Returns:
      this with a single instance of e removed, if e is in this
    • minus

      public ConsPStack<E> minus(int i)
      Specified by:
      minus in interface PSequence<E>
      Specified by:
      minus in interface PStack<E>
      Parameters:
      i -
      Returns:
      a sequence consisting of the elements of this with the element at index i removed.
    • minusAll

      public ConsPStack<E> minusAll(Collection<?> list)
      Specified by:
      minusAll in interface PCollection<E>
      Specified by:
      minusAll in interface PSequence<E>
      Specified by:
      minusAll in interface PStack<E>
      Parameters:
      list -
      Returns:
      this with all elements of list completely removed
    • with

      public ConsPStack<E> with(int i, E e)
      Specified by:
      with in interface PSequence<E>
      Specified by:
      with in interface PStack<E>
      Parameters:
      i -
      e -
      Returns:
      a sequence consisting of the elements of this with e replacing the element at index i.
    • subList

      public ConsPStack<E> subList(int start)
      Specified by:
      subList in interface PStack<E>
      Parameters:
      start -
      Returns:
      subList(start,this.size())