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

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