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

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