home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / vectoriterator.java < prev   
Encoding:
Java Source  |  1996-09-10  |  7.3 KB  |  275 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.Vector;
  7.  
  8. /**
  9.  * A VectorIterator is a random access iterator that allows you to iterate through
  10.  * the contents of a java.util.Vector.
  11.  * <p>
  12.  * @see jgl.RandomAccessIterator
  13.  * @version 1.1
  14.  * @author ObjectSpace, Inc.
  15.  */
  16.  
  17. public final class VectorIterator implements RandomAccessIterator
  18.   {
  19.   VectorArray myVectorArray;
  20.   java.util.Vector myVector;
  21.   int myIndex;
  22.   
  23.   /**
  24.    * Return an iterator positioned at the first element of a particular java.util.Vector.  
  25.    * @param vector The Vector whose first element I will be positioned at.
  26.    */
  27.   public static VectorIterator begin( java.util.Vector vector )
  28.     {
  29.     return new VectorIterator( vector, 0, new VectorArray( vector ) );
  30.     }
  31.  
  32.   /**
  33.    * Return an iterator positioned at the first element of a particular java.util.Vector.  
  34.    * @param vector The Vector whose first element I will be positioned at.
  35.    * @param vectorArray The container the iterator is associated with.
  36.    */
  37.   public static VectorIterator begin( java.util.Vector vector, VectorArray vectorArray )
  38.     {
  39.     return new VectorIterator( vector, 0, vectorArray );
  40.     }
  41.  
  42.   /**
  43.    * Return an iterator positioned immediately after the last element of a 
  44.    * particular java.util.Vector.
  45.    * @param vector The Vector whose last element I will be positioned after.
  46.    */
  47.   public static VectorIterator end( java.util.Vector vector )
  48.     {
  49.     return new VectorIterator( vector, vector.size(), new VectorArray( vector ) );
  50.     }
  51.  
  52.   /**
  53.    * Return an iterator positioned immediately after the last element of a 
  54.    * particular java.util.Vector.
  55.    * @param vector The Vector whose last element I will be positioned after.
  56.    * @param vectorArray The container the iterator is associated with.
  57.    */
  58.   public static VectorIterator end( java.util.Vector vector, VectorArray vectorArray )
  59.     {
  60.     return new VectorIterator( vector, vector.size(), vectorArray );
  61.     }
  62.  
  63.   /**
  64.    * Construct myself to be an iterator with no associated data structure or position.
  65.    */
  66.   public VectorIterator()
  67.     {
  68.     }
  69.  
  70.   /**
  71.    * Construct myself to be a copy of an existing iterator.
  72.    * @param iterator The iterator to copy.
  73.    */
  74.   public VectorIterator( VectorIterator iterator )
  75.     {
  76.     myVectorArray = iterator.myVectorArray;
  77.     myVector = iterator.myVector;
  78.     myIndex = iterator.myIndex;
  79.     }
  80.  
  81.   /**
  82.    * Construct myself to be positioned at a particular index of a specific Vector.
  83.    * @param vector My associated Vector.
  84.    * @param index My associated index.
  85.    */
  86.   public VectorIterator( java.util.Vector vector, int index )
  87.     {
  88.     this( vector, index, new VectorArray( vector ) );
  89.     }
  90.  
  91.   /**
  92.    * Construct myself to be positioned at a particular index of a specific Vector.
  93.    * @param vector My associated Vector.
  94.    * @param index My associated index.
  95.    * @param vectorArray The container the iterator is associated with.
  96.    */
  97.   public VectorIterator( java.util.Vector vector, int index, VectorArray vectorArray )
  98.     {
  99.     myVectorArray = vectorArray;
  100.     myVector = vector;
  101.     myIndex = index;
  102.     }
  103.  
  104.   /**
  105.    * Return a clone of myself.
  106.    */
  107.   public Object clone()
  108.     {
  109.     return new VectorIterator( this );
  110.     }
  111.  
  112.   /**
  113.    * Return true if a specified object is the same kind of iterator as me 
  114.    * and is positioned at the same element.
  115.    * @param object Any object.
  116.    */
  117.   public boolean equals( Object object )
  118.     {
  119.     return object instanceof VectorIterator && equals( (VectorIterator) object );
  120.     }
  121.  
  122.   /**
  123.    * Return true if iterator is positioned at the same element as me.
  124.    * @param iterator The iterator to compare myself against.
  125.    */
  126.   public boolean equals( VectorIterator iterator )
  127.     {
  128.     return iterator.myIndex == myIndex && iterator.myVector == myVector;
  129.     }
  130.  
  131.   /**
  132.    * Return true if I'm before a specified iterator.
  133.    * @param iterator The iterator to compare myself against.
  134.    */
  135.   public boolean less( RandomAccessIterator iterator )
  136.     {
  137.     return myIndex < ((VectorIterator) iterator).myIndex;
  138.     }
  139.  
  140.   /**
  141.    * Return the object that is a specified distance from my current position.
  142.    * @param offset The offset from my current position.
  143.    * @exception ArrayIndexOutOfBoundsException If the adjusted index is invalid.
  144.    */
  145.   public Object get( int offset )
  146.     {
  147.     return myVector.elementAt( myIndex + offset );
  148.     }
  149.  
  150.   /**
  151.    * Write an object at a specified distance from my current position.
  152.    * @param offset The offset from my current position.
  153.    * @param object The object to write.
  154.    * @exception ArrayIndexOutOfBoundsException If the adjusted index is invalid.
  155.    */
  156.   public void put( int offset, Object object )
  157.     {
  158.     myVector.setElementAt( object, myIndex + offset );
  159.     }
  160.  
  161.   /**
  162.    * Return true if I'm positioned at the first item of my input stream.
  163.    */
  164.   public boolean atBegin()
  165.     {
  166.     return myIndex == 0;
  167.     }
  168.  
  169.   /**
  170.    * Return true if I'm positioned after the last item in my input stream.
  171.    */
  172.   public boolean atEnd()
  173.     {
  174.     return myIndex == myVector.size();
  175.     }
  176.  
  177.   /**
  178.    * Return true if there are more elements in my input stream.
  179.    */
  180.   public boolean hasMoreElements()
  181.     {
  182.     return myIndex < myVector.size();
  183.     }
  184.  
  185.   /**
  186.    * Advance by one.
  187.    */
  188.   public void advance()
  189.     {
  190.     myIndex++;
  191.     }
  192.  
  193.   /**
  194.    * Advance by a specified amount.
  195.    * @param n The amount to advance.
  196.    */
  197.   public void advance( int n )
  198.     {
  199.     myIndex += n;
  200.     }
  201.  
  202.   /**
  203.    * Retreat by one.
  204.    */
  205.   public void retreat()
  206.     {
  207.     myIndex--;
  208.     }
  209.  
  210.   /**
  211.    * Retreat by a specified amount.
  212.    * @param n The amount to retreat.
  213.    */
  214.   public void retreat( int n )
  215.     {
  216.     myIndex -= n;
  217.     }
  218.  
  219.   /**
  220.    * Return the next element in my input stream.
  221.    * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
  222.    */
  223.   public Object nextElement()
  224.     {
  225.     return myVector.elementAt( myIndex++ );
  226.     }
  227.  
  228.   /**
  229.    * Return the object at my current position.
  230.    * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
  231.    */
  232.   public Object get()
  233.     {
  234.     return myVector.elementAt( myIndex );
  235.     }
  236.  
  237.   /**
  238.    * Set the object at my current position to a specified value.
  239.    * @param object The object to be written at my current position.
  240.    * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
  241.    */
  242.   public void put( Object object )
  243.     {
  244.     myVector.setElementAt( object, myIndex );
  245.     }
  246.  
  247.   /**
  248.    * Return the distance from myself to another iterator.
  249.    * I should be before the specified iterator.
  250.    * @param iterator The iterator to compare myself against.
  251.    */
  252.   public int distance( ForwardIterator iterator )
  253.     {
  254.     return ((VectorIterator) iterator).myIndex - myIndex;
  255.     }
  256.  
  257.   /**
  258.    * Return my current index.
  259.    */
  260.   public int index()
  261.     {
  262.     return myIndex;
  263.     }
  264.  
  265.   /** 
  266.    * Return null for my associated Container since java.util.Vector
  267.    * isn't in the JGL hierarchy (but should be ;-)
  268.    */
  269.   public Container getContainer() 
  270.     {
  271.     return myVectorArray;
  272.     }
  273.   }
  274.  
  275.