home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / stack.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  4.4 KB  |  209 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. import java.util.Enumeration;
  7.  
  8. /**
  9.  * A Stack is an adapter that allows you to use any Sequence as a
  10.  * first-in, last-out data structure. By default, a Stack uses an
  11.  * Array.
  12.  * <p>
  13.  * @see jgl.Sequence
  14.  * @see jgl.Array
  15.  * @see jgl.examples.StackExamples
  16.  * @version 1.1
  17.  * @author ObjectSpace, Inc.
  18.  */
  19.  
  20. public class Stack implements Container
  21.   {
  22.   Sequence mySequence;
  23.  
  24.   /**
  25.    * Construct myself to be an empty Stack.
  26.    * Use an Array for my underlying implementation.
  27.    */
  28.   public Stack()
  29.     {
  30.     mySequence = new Array();
  31.     }
  32.  
  33.   /**
  34.    * Construct myself with a specified Sequence as my underlying implementation.
  35.    * @param sequence The empty Sequence to be used for my implementation.
  36.    */
  37.   public Stack( Sequence sequence )
  38.     {
  39.     mySequence = sequence;
  40.     }
  41.  
  42.   /**
  43.    * Construct myself to be a shallow copy of a specified Stack.
  44.    * @param stack The Stack to be copied.
  45.    */
  46.   public Stack( Stack stack )
  47.     {
  48.     mySequence = (Sequence) stack.mySequence.clone();
  49.     }
  50.  
  51.   /**
  52.    * Become a shallow copy of a specified Stack.
  53.    * A shallow copy is made of the Stack's underlying sequence.
  54.    * @param stack The Stack to be copied.
  55.    */
  56.   public void copy( Stack stack )
  57.     {
  58.     if( this != stack )
  59.       mySequence = (Sequence) stack.mySequence.clone();
  60.     }
  61.  
  62.   /**
  63.    * Return a shallow copy of myself.
  64.    */
  65.   public Object clone()
  66.     {
  67.     return new Stack( (Sequence) mySequence.clone() );
  68.     }
  69.  
  70.   /**
  71.    * Return a string that describes me.
  72.    */
  73.   public String toString()
  74.     {
  75.     return "Stack( " + mySequence.toString() + " )";
  76.     }
  77.  
  78.   /**
  79.    * Return true if object is a Stack whose underlying sequence is equal to mine.
  80.    * @param object Any object.
  81.    */
  82.   public boolean equals( Object object )
  83.     {
  84.     return object instanceof Stack && equals( (Stack) object );
  85.     }
  86.  
  87.   /**
  88.    * Return true if a specified Stack's sequence is equal to mine.
  89.    * @param stack The Stack to compare myself against.
  90.    */
  91.   public boolean equals( Stack stack )
  92.     {
  93.     return mySequence.equals( stack.mySequence );
  94.     }
  95.  
  96.   /**
  97.    * Return my hash code for support of hashing containers
  98.    */
  99.   public int hashCode()
  100.     {                             
  101.     return Hashing.orderedHash( this );
  102.     }
  103.   
  104.   /**
  105.    * Return true if I contain no objects.
  106.    */
  107.   public boolean isEmpty()
  108.     {
  109.     return mySequence.isEmpty();
  110.     }
  111.  
  112.   /**
  113.    * Return the number of objects that I contain.
  114.    */
  115.   public int size()
  116.     {
  117.     return mySequence.size();
  118.     }
  119.  
  120.   /**
  121.    * Return the maximum number of objects that I can contain.
  122.    */
  123.   public int maxSize()
  124.     {
  125.     return mySequence.maxSize();
  126.     }
  127.  
  128.   /**
  129.    * Return the last object that was pushed onto me.
  130.    * @exception jgl.InvalidOperationException if the Stack is empty.
  131.    */
  132.   public Object top()
  133.     {
  134.     return mySequence.back();
  135.     }
  136.  
  137.   /** 
  138.    * Push an object.  Return null as add's always work for Stacks
  139.    * @param object The object to push.
  140.    */
  141.   public Object add( Object object )
  142.     {
  143.     mySequence.pushBack( object );
  144.     return null;
  145.     }
  146.  
  147.   /**
  148.    * Push an object.
  149.    * @param object The object to push.
  150.    */
  151.   public void push( Object object )
  152.     {
  153.     mySequence.pushBack( object );
  154.     }
  155.  
  156.   /**
  157.    * Pop the last object that was pushed onto me.
  158.    * @exception jgl.InvalidOperationException if the Stack is empty.
  159.    */    
  160.   public Object pop()
  161.     {
  162.     return mySequence.popBack();
  163.     }
  164.  
  165.   /**
  166.    * Remove all of my objects.
  167.    */
  168.   public void clear()
  169.     {
  170.     mySequence.clear();
  171.     }
  172.  
  173.   /**
  174.    * Return an Enumeration of my components.
  175.    */
  176.   public Enumeration elements()
  177.     {
  178.     return mySequence.elements();
  179.     }
  180.  
  181.   /**
  182.    * Return an iterator positioned at my first item.
  183.    */
  184.   public ForwardIterator start()
  185.     {
  186.     return mySequence.start();
  187.     }
  188.  
  189.   /**
  190.    * Return an iterator positioned immediately afer my last item.
  191.    */
  192.   public ForwardIterator finish()
  193.     {
  194.     return mySequence.finish();
  195.     }
  196.  
  197.   /**
  198.    * Swap my contents with another Stack.
  199.    * @param stack The Stack that I will swap my contents with.
  200.    */
  201.   public void swap( Stack stack )
  202.     {
  203.     Sequence tmp_sequence = mySequence;
  204.     mySequence = stack.mySequence;
  205.     stack.mySequence = tmp_sequence;
  206.     }
  207.   }
  208.  
  209.