home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / jgl_1_1.exe / src / VectorArray.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  4.8 KB  |  220 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. import java.util.Vector;
  8.  
  9. /**
  10.  * VectorArray allows a java.util.Vector to be accessed like a Container.
  11.  * It is particularly useful for applying generic algorithms like Sorting.sort()
  12.  * to a java.util.Vector.
  13.  * <p>
  14.  * @version 1.1
  15.  * @author ObjectSpace, Inc.
  16.  */
  17.  
  18. public class VectorArray extends ArrayAdapter
  19.   {
  20.   java.util.Vector myVector;
  21.  
  22.   public VectorArray( java.util.Vector vector )
  23.     {
  24.     myVector = vector;
  25.     }
  26.  
  27.   public VectorArray( VectorArray vector )
  28.     {
  29.     myVector = vector.myVector;
  30.     }
  31.  
  32.   /**
  33.    * Return a shallow copy of myself.
  34.    */
  35.   public Object clone()
  36.     {
  37.     return new VectorArray( this );
  38.     }
  39.  
  40.   /**
  41.    * Return a string that describes me.
  42.    */
  43.   public String toString()
  44.     {
  45.     return myVector.toString();
  46.     }
  47.  
  48.   /**
  49.    * Return true if I'm equal to a specified object.
  50.    * @param object The object to compare myself against.
  51.    * @return true if I'm equal to the specified object.
  52.    */
  53.   public boolean equals( Object object )
  54.     {
  55.     return myVector.equals( ((VectorArray) object).myVector );
  56.     }
  57.   
  58.   /**
  59.    * Return the number of objects that I contain.
  60.    */
  61.   public int size()
  62.     {
  63.     return myVector.size();
  64.     }
  65.  
  66.   /**
  67.    * Return the maximum number of objects that I can contain.
  68.    */
  69.   public int maxSize()
  70.     {
  71.     return Allocator.maxSize();
  72.     }
  73.  
  74.   /**
  75.    * Return an Enumeration of my elements.
  76.    */
  77.   public Enumeration elements()
  78.     {
  79.     return VectorIterator.begin( myVector, this );
  80.     }
  81.  
  82.   /**
  83.    * Return an iterator positioned at my first item.
  84.    */
  85.   public ForwardIterator start()
  86.     {
  87.     return VectorIterator.begin( myVector, this );
  88.     }
  89.  
  90.   /**
  91.    * Return an iterator positioned immediately after my last item.
  92.    */
  93.   public ForwardIterator finish()
  94.     {
  95.     return VectorIterator.end( myVector, this );
  96.     }
  97.  
  98.   /**
  99.    * Return the object at the specified index.
  100.    * @param index The index.
  101.    */
  102.   public Object at( int index )
  103.     {
  104.     return myVector.elementAt( index );
  105.     }
  106.  
  107.   /**
  108.    * Set the object at a specified index.  The object must be a Integer
  109.    * @param index The index.
  110.    * @param object The object to place at the specified index.
  111.    * @exception java.lang.IndexOutOfBoundsException if index is not in range.
  112.    */
  113.   public void put( int index, Object object )
  114.     {
  115.     myVector.setElementAt( object, index );
  116.     }
  117.  
  118.   /**
  119.    * Remove all of my objects. 
  120.    */
  121.   public void clear()
  122.     {
  123.     myVector.removeAllElements();
  124.     }
  125.  
  126.   /**
  127.    * Add an object to myself. 
  128.    */
  129.   public Object add( Object object )
  130.     {
  131.     myVector.addElement( object );
  132.     return null;
  133.     }
  134.  
  135.   /**
  136.    * Insert an object in front of my first element.
  137.    * @param object The object to insert.
  138.    */
  139.   public void pushFront( Object object ) 
  140.     {
  141.     myVector.insertElementAt( object, 0 );
  142.     }
  143.  
  144.   /**
  145.    * Remove and return my first element.
  146.    */
  147.   public Object popFront()
  148.     {
  149.     Object r = myVector.firstElement();
  150.     myVector.removeElementAt( 0 );
  151.     return r;
  152.     }
  153.  
  154.   /**
  155.    * Add an object at my end.
  156.    * @param object The object to add.
  157.    */
  158.   public void pushBack( Object object )
  159.     {
  160.     myVector.addElement( object );
  161.     }
  162.  
  163.   /**
  164.    * Remove and return my last element.
  165.    */
  166.   public Object popBack()
  167.     {
  168.     Object r = myVector.lastElement();
  169.     myVector.removeElementAt( myVector.size() - 1 );
  170.     return r;
  171.     }
  172.  
  173.   /**
  174.    * Remove all elements that match a specified object and return the number of
  175.    * objects that were removed.
  176.    * @param object The object to remove.
  177.    */
  178.   public int remove( Object object )
  179.     {
  180.     int count = 0;
  181.     while( myVector.removeElement( object ) )
  182.       count++;
  183.       
  184.     return count;
  185.     }
  186.  
  187.   /**
  188.    * Remove all elements within a specified range that match a particular object
  189.    * and return the number of objects that were removed.
  190.    * @param first The index of the first object to remove.
  191.    * @param last The index of the last object to remove.
  192.    * @param object The object to remove.
  193.    * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
  194.    */
  195.   public int remove( int first, int last, Object object )
  196.     {
  197.     if ( ( first < 0 ) || ( last > myVector.size() - 1 ) )
  198.       throw new IndexOutOfBoundsException( "index out of range for this Vector." );
  199.  
  200.     int count = 0;
  201.     int index = first;
  202.     
  203.     for( int i = first; i < last; i++ )
  204.       {
  205.       if( ( myVector.elementAt( index ) ).equals( object ) )
  206.         {
  207.         myVector.removeElementAt( index );
  208.         count++;
  209.         }
  210.       else
  211.         {
  212.         index++;
  213.         }
  214.       }
  215.         
  216.     return count;
  217.     }
  218.   }
  219.  
  220.