home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / queue.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  4.5 KB  |  216 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 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 jgl.Sequence
  14.  * @see jgl.SList
  15.  * @see jgl.examples.QueueExamples
  16.  * @version 1.1
  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.     mySequence = sequence;
  40.     }
  41.  
  42.   /**
  43.    * Construct myself to be a shallow copy of a specified Queue.
  44.    * @param queue The Queue to be copied.
  45.    */
  46.   public Queue( Queue queue )
  47.     {
  48.     mySequence = (Sequence) queue.mySequence.clone();
  49.     }
  50.  
  51.   /**
  52.    * Become a shallow copy of a Queue.
  53.    * @param queue The Queue to be copied.
  54.    */
  55.   public void copy( Queue queue )
  56.     {
  57.     if( this != queue )
  58.       mySequence = (Sequence) queue.mySequence.clone();
  59.     }
  60.  
  61.   /**
  62.    * Return a string that describes me.
  63.    */
  64.   public String toString()
  65.     {
  66.     return "Queue( " + mySequence.toString() + " )";
  67.     }
  68.  
  69.   /**
  70.    * Return true if object is a Queue whose underlying sequence is equal to mine.
  71.    * @param object Any object.
  72.    */
  73.   public boolean equals( Object object )
  74.     {
  75.     return object instanceof Queue && equals( (Queue) object );
  76.     }
  77.  
  78.   /**
  79.    * Return true if a Queue's sequence is equal to mine.
  80.    * @param queue The Queue to compare myself against.
  81.    */
  82.   public boolean equals( Queue queue )
  83.     {
  84.     return mySequence.equals( queue.mySequence );
  85.     }
  86.  
  87.   /**
  88.    * Return my hash code for support of hashing containers
  89.    */
  90.   public int hashCode()
  91.     {                             
  92.     return Hashing.orderedHash( this );
  93.     }
  94.   
  95.   /**
  96.    * Return a shallow copy of myself.
  97.    * A shallow copy is made of my underlying sequence.
  98.    */
  99.   public Object clone()
  100.     {
  101.     return new Queue( (Sequence) mySequence.clone() );
  102.     }
  103.  
  104.   /**
  105.    * Remove all of my objects.
  106.    */
  107.   public void clear()
  108.     {
  109.     mySequence.clear();
  110.     }
  111.  
  112.   /**
  113.    * Return true if I contain no objects.
  114.    */
  115.   public boolean isEmpty()
  116.     {
  117.     return mySequence.isEmpty();
  118.     }
  119.  
  120.   /**
  121.    * Return the number of objects that I contain.
  122.    */
  123.   public int size()
  124.     {
  125.     return mySequence.size();
  126.     }
  127.  
  128.   /**
  129.    * Return the maximum number of objects that I can contain.
  130.    */
  131.   public int maxSize()
  132.     {
  133.     return mySequence.maxSize();
  134.     }
  135.  
  136.   /**
  137.    * Return the object at my front.
  138.    * @exception jgl.InvalidOperationException If the Queue is empty.
  139.    */
  140.   public Object front()
  141.     {
  142.     return mySequence.front();
  143.     }
  144.  
  145.   /**
  146.    * Return the object at my back.
  147.    * @exception jgl.InvalidOperationException If the Queue is empty.
  148.    */
  149.   public Object back()
  150.     {
  151.     return mySequence.back();
  152.     }
  153.  
  154.   /**
  155.    * Add an object to my back.
  156.    * @param object The object to add.
  157.    */
  158.   public Object add( Object object )
  159.     {
  160.     return mySequence.add( object );
  161.     }
  162.  
  163.   /**
  164.    * Add an object to my back.
  165.    */
  166.   public void push( Object object )
  167.     {
  168.     mySequence.add( object );
  169.     }
  170.  
  171.   /**
  172.    * Remove an object from my front and return it.
  173.    * @exception jgl.InvalidOperationException If the Queue is empty.
  174.    */
  175.   public Object pop()
  176.     {
  177.     return mySequence.popFront();
  178.     }
  179.  
  180.   /**
  181.    * Return an Enumeration of my components.
  182.    */
  183.   public Enumeration elements()
  184.     {
  185.     return mySequence.elements();
  186.     }
  187.  
  188.   /**
  189.    * Return an iterator positioned at my first item.
  190.    */
  191.   public ForwardIterator start()
  192.     {
  193.     return mySequence.start();
  194.     }
  195.  
  196.   /**
  197.    * Return an iterator positioned immediately afer my last item.
  198.    */
  199.   public ForwardIterator finish()
  200.     {
  201.     return mySequence.finish();
  202.     }
  203.  
  204.   /**
  205.    * Swap my contents with another Queue.
  206.    * @param queue The Queue that I will swap my contents with.
  207.    */
  208.   public void swap( Queue queue )
  209.     {
  210.     Sequence tmpSequence = mySequence;
  211.     mySequence = queue.mySequence;
  212.     queue.mySequence = tmpSequence;
  213.     }
  214.   }
  215.  
  216.