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

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