Class FixedStack<N>

java.lang.Object
com.conversantmedia.util.collection.FixedStack<N>
All Implemented Interfaces:
Stack<N>

public class FixedStack<N> extends Object implements Stack<N>
A very high performance stack to replace java.util.Stack. This stack wraps around rather than checking for bounds. The java version of Stack is based on Vector and completely outdated. The performance of java.util.Stack is poor at best. This version is a small fast fixed size stack. There is no bounds checking so it should only be used when the stack size is known in advance. This object is not thread safe.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
     
    private final int
     
    private final N[]
     
    private int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    FixedStack(int size)
    construct a new stack of given capacity
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clear the stack
    boolean
    Linear search the stack for contains - not an efficient operation
    boolean
     
    peek at the top of the stack
    pop()
    pop the next element off the stack
    boolean
    push(N n)
    add an element to the stack
    int
    how much available space in the stack
    int
    Return the size of the stack

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • size

      private final int size
    • mask

      private final int mask
    • stack

      private final N[] stack
    • stackTop

      private int stackTop
  • Constructor Details

    • FixedStack

      public FixedStack(int size)
      construct a new stack of given capacity
      Parameters:
      size - - the stack size
  • Method Details

    • push

      public boolean push(N n)
      add an element to the stack
      Specified by:
      push in interface Stack<N>
      Parameters:
      n - - the element to add
      Returns:
      boolean - true if the operation succeeded
    • contains

      public boolean contains(N n)
      Description copied from interface: Stack
      Linear search the stack for contains - not an efficient operation
      Specified by:
      contains in interface Stack<N>
      Parameters:
      n - - Object to test for containment
      Returns:
      boolean - true if n is contained somewhere in the stack
    • peek

      public N peek()
      peek at the top of the stack
      Specified by:
      peek in interface Stack<N>
      Returns:
      N - the object at the top of the stack
    • pop

      public N pop()
      pop the next element off the stack
      Specified by:
      pop in interface Stack<N>
      Returns:
      N - The object on the top of the stack
    • size

      public int size()
      Return the size of the stack
      Specified by:
      size in interface Stack<N>
      Returns:
      int - number of elements in the stack
    • remainingCapacity

      public int remainingCapacity()
      how much available space in the stack
      Specified by:
      remainingCapacity in interface Stack<N>
      Returns:
      int - the number of empty slots available in the stack
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Stack<N>
      Returns:
      boolean - true if stack is currently empty
    • clear

      public void clear()
      clear the stack
      Specified by:
      clear in interface Stack<N>