home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / Stack.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  5.6 KB  |  240 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.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 COM.objectspace.jgl.Sequence
  14.  * @see COM.objectspace.jgl.Array
  15.  * @see COM.objectspace.jgl.examples.StackExamples
  16.  * @version 2.0.2
  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.     synchronized( sequence )
  40.       {
  41.       mySequence = (Sequence)sequence.clone();
  42.       }
  43.     }
  44.  
  45.   /**
  46.    * Construct myself to be a shallow copy of a specified Stack.
  47.    * @param stack The Stack to be copied.
  48.    */
  49.   public Stack( Stack stack )
  50.     {
  51.     synchronized( stack )
  52.       {
  53.       mySequence = (Sequence)stack.mySequence.clone();
  54.       }
  55.     }
  56.  
  57.   /**
  58.    * Become a shallow copy of a specified Stack.
  59.    * A shallow copy is made of the Stack's underlying sequence.
  60.    * @param stack The Stack to be copied.
  61.    */
  62.   public synchronized void copy( Stack stack )
  63.     {
  64.     synchronized( stack )
  65.       {
  66.       if ( this != stack )
  67.         mySequence = (Sequence)stack.mySequence.clone();
  68.       }
  69.     }
  70.  
  71.   /**
  72.    * Return a shallow copy of myself.
  73.    */
  74.   public synchronized Object clone()
  75.     {
  76.     return new Stack( (Sequence)mySequence.clone() );
  77.     }
  78.  
  79.   /**
  80.    * Return a string that describes me.
  81.    */
  82.   public synchronized String toString()
  83.     {
  84.     return "Stack( " + mySequence.toString() + " )";
  85.     }
  86.  
  87.   /**
  88.    * Return true if object is a Stack whose underlying sequence is equal to mine.
  89.    * @param object Any object.
  90.    */
  91.   public boolean equals( Object object )
  92.     {
  93.     return object instanceof Stack && equals( (Stack)object );
  94.     }
  95.  
  96.   /**
  97.    * Return true if a specified Stack's sequence is equal to mine.
  98.    * @param stack The Stack to compare myself against.
  99.    */
  100.   public synchronized boolean equals( Stack stack )
  101.     {
  102.     return mySequence.equals( stack.mySequence );
  103.     }
  104.  
  105.   /**
  106.    * Return my hash code for support of hashing containers
  107.    */
  108.   public synchronized int hashCode()
  109.     {
  110.     return mySequence.hashCode();
  111.     }
  112.  
  113.   /**
  114.    * Return true if I contain no objects.
  115.    */
  116.   public boolean isEmpty()
  117.     {
  118.     return mySequence.isEmpty();
  119.     }
  120.  
  121.   /**
  122.    * Return the number of objects that I contain.
  123.    */
  124.   public int size()
  125.     {
  126.     return mySequence.size();
  127.     }
  128.  
  129.   /**
  130.    * Return the maximum number of objects that I can contain.
  131.    */
  132.   public int maxSize()
  133.     {
  134.     return mySequence.maxSize();
  135.     }
  136.  
  137.   /**
  138.    * Return the last object that was pushed onto me.
  139.    * @exception COM.objectspace.jgl.InvalidOperationException if the Stack is empty.
  140.    */
  141.   public synchronized Object top()
  142.     {
  143.     return mySequence.back();
  144.     }
  145.  
  146.   /**
  147.    * Push an object.  Return null as add's always work for Stacks
  148.    * @param object The object to push.
  149.    */
  150.   public synchronized Object add( Object object )
  151.     {
  152.     mySequence.pushBack( object );
  153.     return null;
  154.     }
  155.  
  156.   /**
  157.    * Push an object.
  158.    * @param object The object to push.
  159.    */
  160.   public void push( Object object )
  161.     {
  162.     add( object );
  163.     }
  164.  
  165.   /**
  166.    * Pop the last object that was pushed onto me.
  167.    * @exception COM.objectspace.jgl.InvalidOperationException if the Stack is empty.
  168.    */
  169.   public synchronized Object pop()
  170.     {
  171.     return mySequence.popBack();
  172.     }
  173.  
  174.   /**
  175.    * Remove all of my objects.
  176.    */
  177.   public synchronized void clear()
  178.     {
  179.     mySequence.clear();
  180.     }
  181.  
  182.   /**
  183.    * Return an Enumeration of my components.
  184.    */
  185.   public synchronized Enumeration elements()
  186.     {
  187.     return mySequence.elements();
  188.     }
  189.  
  190.   /**
  191.    * Return an iterator positioned at my first item.
  192.    */
  193.   public synchronized ForwardIterator start()
  194.     {
  195.     return mySequence.start();
  196.     }
  197.  
  198.   /**
  199.    * Return an iterator positioned immediately afer my last item.
  200.    */
  201.   public synchronized ForwardIterator finish()
  202.     {
  203.     return mySequence.finish();
  204.     }
  205.  
  206.   /**
  207.    * Swap my contents with another Stack.
  208.    * @param stack The Stack that I will swap my contents with.
  209.    */
  210.   public synchronized void swap( Stack stack )
  211.     {
  212.     synchronized( stack )
  213.       {
  214.       Sequence tmp_sequence = mySequence;
  215.       mySequence = stack.mySequence;
  216.       stack.mySequence = tmp_sequence;
  217.       }
  218.     }
  219.  
  220.   /**
  221.    * Remove the element at a particular position.
  222.    * @param pos The enumeration representing of the object to remove.
  223.    * @exception COM.objectspace.jgl.InvalidOperationException Thrown by default.
  224.    */
  225.   public Object remove( Enumeration pos )
  226.     {
  227.     throw new InvalidOperationException( "cannot execute remove() on a stack" );
  228.     }
  229.  
  230.   /**
  231.    * Remove the elements in the specified range.
  232.    * @param pos The enumeration representing of the object to remove.
  233.    * @exception COM.objectspace.jgl.InvalidOperationException Thrown by default.
  234.    */
  235.   public int remove( Enumeration first, Enumeration last )
  236.     {
  237.     throw new InvalidOperationException( "cannot execute remove() on a stack" );
  238.     }
  239.   }
  240.