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 / Queue.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  5.7 KB  |  250 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 Queue is an adapter that allows you to use any Sequence as a
  10.  * first-in, first-out data structure. By default, a Queue uses an
  11.  * SList.
  12.  * <p>
  13.  * @see COM.objectspace.jgl.Sequence
  14.  * @see COM.objectspace.jgl.SList
  15.  * @see COM.objectspace.jgl.examples.QueueExamples
  16.  * @version 2.0.2
  17.  * @author ObjectSpace, Inc.
  18.  */
  19.  
  20. public class Queue implements Container
  21.   {
  22.   Sequence mySequence;
  23.  
  24.   /**
  25.    * Construct myself to be an empty Queue.
  26.    * Use an SList for my underlying implementation.
  27.    */
  28.   public Queue()
  29.     {
  30.     mySequence = new SList();
  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 Queue( 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 Queue.
  47.    * @param queue The Queue to be copied.
  48.    */
  49.   public Queue( Queue queue )
  50.     {
  51.     synchronized( queue )
  52.       {
  53.       mySequence = (Sequence)queue.mySequence.clone();
  54.       }
  55.     }
  56.  
  57.   /**
  58.    * Become a shallow copy of a Queue.
  59.    * @param queue The Queue to be copied.
  60.    */
  61.   public synchronized void copy( Queue queue )
  62.     {
  63.     synchronized( queue )
  64.       {
  65.       if ( this != queue )
  66.         mySequence = (Sequence)queue.mySequence.clone();
  67.       }
  68.     }
  69.  
  70.   /**
  71.    * Return a string that describes me.
  72.    */
  73.   public synchronized String toString()
  74.     {
  75.     return "Queue( " + mySequence.toString() + " )";
  76.     }
  77.  
  78.   /**
  79.    * Return true if object is a Queue whose underlying sequence is equal to mine.
  80.    * @param object Any object.
  81.    */
  82.   public boolean equals( Object object )
  83.     {
  84.     return object instanceof Queue && equals( (Queue)object );
  85.     }
  86.  
  87.   /**
  88.    * Return true if a Queue's sequence is equal to mine.
  89.    * @param queue The Queue to compare myself against.
  90.    */
  91.   public synchronized boolean equals( Queue queue )
  92.     {
  93.     synchronized( queue )
  94.       {
  95.       return mySequence.equals( queue.mySequence );
  96.       }
  97.     }
  98.  
  99.   /**
  100.    * Return my hash code for support of hashing containers
  101.    */
  102.   public synchronized int hashCode()
  103.     {
  104.     return mySequence.hashCode();
  105.     }
  106.  
  107.   /**
  108.    * Return a shallow copy of myself.
  109.    * A shallow copy is made of my underlying sequence.
  110.    */
  111.   public synchronized Object clone()
  112.     {
  113.     return new Queue( (Sequence)mySequence.clone() );
  114.     }
  115.  
  116.   /**
  117.    * Remove all of my objects.
  118.    */
  119.   public synchronized void clear()
  120.     {
  121.     mySequence.clear();
  122.     }
  123.  
  124.   /**
  125.    * Return true if I contain no objects.
  126.    */
  127.   public boolean isEmpty()
  128.     {
  129.     return mySequence.isEmpty();
  130.     }
  131.  
  132.   /**
  133.    * Return the number of objects that I contain.
  134.    */
  135.   public int size()
  136.     {
  137.     return mySequence.size();
  138.     }
  139.  
  140.   /**
  141.    * Return the maximum number of objects that I can contain.
  142.    */
  143.   public int maxSize()
  144.     {
  145.     return mySequence.maxSize();
  146.     }
  147.  
  148.   /**
  149.    * Return the object at my front.
  150.    * @exception COM.objectspace.jgl.InvalidOperationException If the Queue is empty.
  151.    */
  152.   public synchronized Object front()
  153.     {
  154.     return mySequence.front();
  155.     }
  156.  
  157.   /**
  158.    * Return the object at my back.
  159.    * @exception COM.objectspace.jgl.InvalidOperationException If the Queue is empty.
  160.    */
  161.   public synchronized Object back()
  162.     {
  163.     return mySequence.back();
  164.     }
  165.  
  166.   /**
  167.    * Add an object to my back.
  168.    * @param object The object to add.
  169.    */
  170.   public synchronized Object add( Object object )
  171.     {
  172.     return mySequence.add( object );
  173.     }
  174.  
  175.   /**
  176.    * Add an object to my back.
  177.    */
  178.   public void push( Object object )
  179.     {
  180.     add( object );
  181.     }
  182.  
  183.   /**
  184.    * Remove an object from my front and return it.
  185.    * @exception COM.objectspace.jgl.InvalidOperationException If the Queue is empty.
  186.    */
  187.   public synchronized Object pop()
  188.     {
  189.     return mySequence.popFront();
  190.     }
  191.  
  192.   /**
  193.    * Return an Enumeration of my components.
  194.    */
  195.   public synchronized Enumeration elements()
  196.     {
  197.     return mySequence.elements();
  198.     }
  199.  
  200.   /**
  201.    * Return an iterator positioned at my first item.
  202.    */
  203.   public synchronized ForwardIterator start()
  204.     {
  205.     return mySequence.start();
  206.     }
  207.  
  208.   /**
  209.    * Return an iterator positioned immediately afer my last item.
  210.    */
  211.   public synchronized ForwardIterator finish()
  212.     {
  213.     return mySequence.finish();
  214.     }
  215.  
  216.   /**
  217.    * Swap my contents with another Queue.
  218.    * @param queue The Queue that I will swap my contents with.
  219.    */
  220.   public synchronized void swap( Queue queue )
  221.     {
  222.     synchronized ( queue )
  223.       {
  224.       Sequence tmpSequence = mySequence;
  225.       mySequence = queue.mySequence;
  226.       queue.mySequence = tmpSequence;
  227.       }
  228.     }
  229.  
  230.   /**
  231.    * Remove the element at a particular position.
  232.    * @param pos The enumeration representing of the object to remove.
  233.    * @exception COM.objectspace.jgl.InvalidOperationException Thrown by default.
  234.    */
  235.   public Object remove( Enumeration pos )
  236.     {
  237.     throw new InvalidOperationException( "cannot execute remove() on a queue" );
  238.     }
  239.  
  240.   /**
  241.    * Remove the elements in the specified range.
  242.    * @param pos The enumeration representing of the object to remove.
  243.    * @exception COM.objectspace.jgl.InvalidOperationException Thrown by default.
  244.    */
  245.   public int remove( Enumeration first, Enumeration last )
  246.     {
  247.     throw new InvalidOperationException( "cannot execute remove() on a queue" );
  248.     }
  249.   }
  250.