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

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