Class ConsPStack<E>

  • Type Parameters:
    E -
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, PCollection<E>, PSequence<E>, PStack<E>

    public final class ConsPStack<E>
    extends AbstractUnmodifiableSequentialList<E>
    implements PStack<E>, java.io.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:
    Serialized Form
    • Field Detail

      • EMPTY

        private static final ConsPStack<java.lang.Object> EMPTY
      • first

        private final E first
      • size

        private final int size
    • Constructor Detail

      • ConsPStack

        private ConsPStack()
      • ConsPStack

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

      • 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​(java.util.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​(java.util.Iterator<? extends E> i)
      • size

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

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

        public ConsPStack<E> subList​(int start,
                                     int end)
        Specified by:
        subList in interface java.util.List<E>
        Specified by:
        subList in interface PSequence<E>
        Specified by:
        subList in interface PStack<E>
        Overrides:
        subList in class java.util.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>
        Returns:
        a collection which contains e and all of the elements of this
      • plusAll

        public ConsPStack<E> plusAll​(java.util.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>
        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>
        Returns:
        a sequence consisting of the elements of this with e inserted at index i.
      • plusAll

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

        public ConsPStack<E> minus​(java.lang.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>
        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>
        Returns:
        a sequence consisting of the elements of this with the element at index i removed.
      • with

        public ConsPStack<E> with​(int i,
                                  E e)
        Specified by:
        with in interface PSequence<E>
        Specified by:
        with in interface PStack<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>
        Returns:
        subList(start,this.size())