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

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