home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / arrayiterator.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  5.0 KB  |  218 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * A ArrayIterator is a random access iterator that allows you to iterate through
  8.  * the contents of a Array.
  9.  * <p>
  10.  * @see jgl.RandomAccessIterator
  11.  * @version 1.1
  12.  * @author ObjectSpace, Inc.
  13.  */
  14.  
  15. public final class ArrayIterator implements RandomAccessIterator
  16.   {
  17.   Array myArray;
  18.   int myIndex;
  19.   
  20.   /**
  21.    * Construct myself to be an iterator with no associated data structure or position.
  22.    */
  23.   public ArrayIterator()
  24.     {
  25.     }
  26.  
  27.   /**
  28.    * Construct myself to be a copy of an existing iterator.
  29.    * @param iterator The iterator to copy.
  30.    */
  31.   public ArrayIterator( ArrayIterator iterator )
  32.     {
  33.     myArray = iterator.myArray;
  34.     myIndex = iterator.myIndex;
  35.     }
  36.  
  37.   /**
  38.    * Construct myself to be positioned at a particular index of a specific Array.
  39.    * @param vector My associated Array.
  40.    * @param index My associated index.
  41.    */
  42.   public ArrayIterator( Array vector, int index )
  43.     {
  44.     myArray = vector;
  45.     myIndex = index;
  46.     }
  47.  
  48.   /**
  49.    * Return a clone of myself.
  50.    */
  51.   public Object clone()
  52.     {
  53.     return new ArrayIterator( this );
  54.     }
  55.  
  56.   /**
  57.    * Return true if a specified object is the same kind of iterator as me 
  58.    * and is positioned at the same element.
  59.    * @param object Any object.
  60.    */
  61.   public boolean equals( Object object )
  62.     {
  63.     return object instanceof ArrayIterator && equals( (ArrayIterator) object );
  64.     }
  65.  
  66.   /**
  67.    * Return true if iterator is positioned at the same element as me.
  68.    * @param iterator The iterator to compare myself against.
  69.    */
  70.   public boolean equals( ArrayIterator iterator )
  71.     {
  72.     return iterator.myIndex == myIndex && iterator.myArray == myArray;
  73.     }
  74.  
  75.   /**
  76.    * Return true if I'm before a specified iterator.
  77.    * @param iterator The iterator to compare myself against.
  78.    */
  79.   public boolean less( RandomAccessIterator iterator )
  80.     {
  81.     return myIndex < ((ArrayIterator) iterator).myIndex;
  82.     }
  83.  
  84.   /**
  85.    * Return the object that is a specified distance from my current position.
  86.    * @param offset The offset from my current position.
  87.    * @exception ArrayIndexOutOfBoundsException If the adjusted index is invalid.
  88.    */
  89.   public Object get( int offset )
  90.     {
  91.     return myArray.at( myIndex + offset );
  92.     }
  93.  
  94.   /**
  95.    * Write an object at a specified distance from my current position.
  96.    * @param offset The offset from my current position.
  97.    * @param object The object to write.
  98.    * @exception ArrayIndexOutOfBoundsException If the adjusted index is invalid.
  99.    */
  100.   public void put( int offset, Object object )
  101.     {
  102.     myArray.put( myIndex + offset, object );
  103.     }
  104.  
  105.   /**
  106.    * Return true if I'm positioned at the first item of my input stream.
  107.    */
  108.   public boolean atBegin()
  109.     {
  110.     return myIndex == 0;
  111.     }
  112.  
  113.   /**
  114.    * Return true if I'm positioned after the last item in my input stream.
  115.    */
  116.   public boolean atEnd()
  117.     {
  118.     return myIndex == myArray.size();
  119.     }
  120.  
  121.   /**
  122.    * Return true if there are more elements in my input stream.
  123.    */
  124.   public boolean hasMoreElements()
  125.     {
  126.     return myIndex < myArray.size();
  127.     }
  128.  
  129.   /**
  130.    * Advance by one.
  131.    */
  132.   public void advance()
  133.     {
  134.     myIndex++;
  135.     }
  136.  
  137.   /**
  138.    * Advance by a specified amount.
  139.    * @param n The amount to advance.
  140.    */
  141.   public void advance( int n )
  142.     {
  143.     myIndex += n;
  144.     }
  145.  
  146.   /**
  147.    * Retreat by one.
  148.    */
  149.   public void retreat()
  150.     {
  151.     myIndex--;
  152.     }
  153.  
  154.   /**
  155.    * Retreat by a specified amount.
  156.    * @param n The amount to retreat.
  157.    */
  158.   public void retreat( int n )
  159.     {
  160.     myIndex -= n;
  161.     }
  162.  
  163.   /**
  164.    * Return the next element in my input stream.
  165.    * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
  166.    */
  167.   public Object nextElement()
  168.     {
  169.     return myArray.at( myIndex++ );
  170.     }
  171.  
  172.   /**
  173.    * Return the object at my current position.
  174.    * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
  175.    */
  176.   public Object get()
  177.     {
  178.     return myArray.at( myIndex );
  179.     }
  180.  
  181.   /**
  182.    * Set the object at my current position to a specified value.
  183.    * @param object The object to be written at my current position.
  184.    * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
  185.    */
  186.   public void put( Object object )
  187.     {
  188.     myArray.put( myIndex, object );
  189.     }
  190.  
  191.   /**
  192.    * Return the distance from myself to another iterator.
  193.    * I should be before the specified iterator.
  194.    * @param iterator The iterator to compare myself against.
  195.    */
  196.   public int distance( ForwardIterator iterator )
  197.     {
  198.     return ((ArrayIterator) iterator).myIndex - myIndex;
  199.     }
  200.  
  201.   /**
  202.    * Return my current index.
  203.    */
  204.   public int index()
  205.     {
  206.     return myIndex;
  207.     }
  208.  
  209.   /** 
  210.    * Return my associated array.
  211.    */
  212.   public Container getContainer() 
  213.     {
  214.     return myArray;
  215.     }
  216.   }
  217.  
  218.