Class ImmutableStack<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public class ImmutableStack<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    An immutable stack of objects. The push(Object) and pop() operations create new stacks.
    • Constructor Detail

      • ImmutableStack

        protected ImmutableStack​(T[] entries)
    • Method Detail

      • getMaxSize

        public static int getMaxSize()
      • emptyStack

        public static final <T> ImmutableStack<T> emptyStack()
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • makeInternalArray

        protected T[] makeInternalArray​(int size)
      • peek

        public T peek()
        Returns:
        the element on the top of the stack
        Throws:
        java.util.EmptyStackException - if stack is empty
      • pop

        public ImmutableStack<T> pop()
        Throws:
        java.util.EmptyStackException - if stack is empty
      • isEmpty

        public boolean isEmpty()
      • size

        public int size()
      • get

        public T get​(int i)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • contains

        public boolean contains​(T entry)
      • topMatches

        public boolean topMatches​(ImmutableStack<T> other)
                           throws java.lang.IllegalArgumentException
        Returns:
        true iff other.size() = k, k <= this.size(), and the top k elements of this equal other
        Throws:
        java.lang.IllegalArgumentException - if other == null
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • replaceTop

        public ImmutableStack<T> replaceTop​(T t)
        return a new stack with the top replaced with t
        Throws:
        java.util.EmptyStackException