Class _Private_RecyclingStack<T>

  • Type Parameters:
    T - the type of elements stored.

    public final class _Private_RecyclingStack<T>
    extends java.lang.Object
    A stack whose elements are recycled. This can be useful when the stack needs to grow and shrink frequently and has a predictable maximum depth.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isEmpty()  
      T peek()  
      T pop()
      Pops an element from the stack, retaining a reference to the element so that it can be reused the next time the stack grows to the element's depth.
      T push()
      Pushes an element onto the top of the stack, instantiating a new element only if the stack has not previously grown to the new depth.
      int size()  
      • Methods inherited from class java.lang.Object

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

      • _Private_RecyclingStack

        public _Private_RecyclingStack​(int initialCapacity,
                                       _Private_RecyclingStack.ElementFactory<T> elementFactory)
        Parameters:
        initialCapacity - the initial capacity of the underlying collection.
        elementFactory - the factory used to create a new element on push() when the stack has not previously grown to the new depth.
    • Method Detail

      • push

        public T push()
        Pushes an element onto the top of the stack, instantiating a new element only if the stack has not previously grown to the new depth.
        Returns:
        the element at the top of the stack after the push. This element must be initialized by the caller.
      • peek

        public T peek()
        Returns:
        the element at the top of the stack, or null if the stack is empty.
      • pop

        public T pop()
        Pops an element from the stack, retaining a reference to the element so that it can be reused the next time the stack grows to the element's depth.
        Returns:
        the element that was at the top of the stack before the pop, or null if the stack was empty.
      • isEmpty

        public boolean isEmpty()
        Returns:
        true if the stack is empty; otherwise, false.
      • size

        public int size()
        Returns:
        the number of elements on the stack.