// Copyright(c) 1996,1997 ObjectSpace, Inc.
// Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.

package COM.objectspace.jgl;

import java.io.Serializable;

/**
 * An IntIterator is a random access iterator that allows you to iterate through
 * an array of ints.
 * <p>
 * @see COM.objectspace.jgl.RandomAccessIterator
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public final class IntIterator implements RandomAccessIterator, Serializable
  {
  IntArray myIntArray;
  int myArray[];
  int myIndex;

  /**
   * Return an iterator positioned at the first element of a particular array.
   * @param array The array whose first element I will be positioned at.
   */
  public static IntIterator begin( int array[] )
    {
    return new IntIterator( array, 0, new IntArray( array ) );
    }

  /**
   * Return an iterator positioned at the first element of a particular array.
   * @param array The array whose first element I will be positioned at.
   * @param intArray The array the iterator is associated with.
   */
  public static IntIterator begin( int array[], IntArray intArray )
    {
    return new IntIterator( array, 0, intArray );
    }

  /**
   * Return an iterator positioned immediately after the last element of a particular array.
   * @param array The array whose last element I will be positioned after.
   */
  public static IntIterator end( int array[] )
    {
    return new IntIterator( array, array.length, new IntArray( array ) );
    }

  /**
   * Return an iterator positioned immediately after the last element of a particular array.
   * @param array The array whose last element I will be positioned after.
   * @param intArray The array the iterator is associated with.
   */
  public static IntIterator end( int array[], IntArray intArray )
    {
    return new IntIterator( array, array.length, intArray );
    }

  /**
   * Construct myself to be an iterator with no associated data structure or position.
   */
  public IntIterator()
    {
    }

  /**
   * Construct myself to be an iterator positioned at the first element of a specified
   * array.
   * @param array The array whose first element I will be positioned at.
   */
  public IntIterator( int array[] )
    {
    this( array, 0, new IntArray( array ) );
    }

  /**
   * Construct myself to be an iterator positioned at the first element of a specified
   * array.
   * @param array The array whose first element I will be positioned at.
   * @param intArray The array the iterator is associated with.
   */
  public IntIterator( int array[], IntArray intArray )
    {
    this( array, 0, intArray );
    }

  /**
   * Construct myself to be a copy of an existing iterator.
   * @param iterator The iterator to copy.
   */
  public IntIterator( IntIterator iterator )
    {
    myIntArray = iterator.myIntArray;
    myArray = iterator.myArray;
    myIndex = iterator.myIndex;
    }

  /**
   * Construct myself to be positioned at a particular index of a specific int[].
   * @param array My associated int[].
   * @param i My associated index.
   */
  public IntIterator( int array[], int index )
    {
    this( array, index, new IntArray( array ) );
    }

  /**
   * Construct myself to be positioned at a particular index of a specific int[].
   * @param array My associated int[].
   * @param index My associated index.
   * @param intArray The array the iterator is associated with.
   */
  public IntIterator( int array[], int index, IntArray intArray )
    {
    myIntArray = intArray;
    myArray = array;
    myIndex = index;
    }

  /**
   * Return my current index.
   */
  public int index()
    {
    return myIndex;
    }

  /**
   * Return a clone of myself.
   */
  public Object clone()
    {
    return new IntIterator( this );
    }

  /**
   * Return true if a specified object is the same kind of iterator as me
   * and is positioned at the same element.
   * @param object Any object.
   */
  public boolean equals( Object object )
    {
    return object instanceof IntIterator && equals( (IntIterator) object );
    }

  /**
   * Return true if iterator is positioned at the same element as me.
   * @param iterator The iterator to compare myself against.
   */
  public boolean equals( IntIterator iterator )
    {
    return iterator.myIndex == myIndex && iterator.myArray == myArray;
    }

  /**
   * Return true if I'm before a specified iterator.
   * @param iterator The iterator to compare myself against.
   */
  public boolean less( RandomAccessIterator iterator )
    {
    return myIndex < ((IntIterator) iterator).myIndex;
    }

  /**
   * Return the object that is a specified distance from my current position.
   * @param offset The offset from my current position.
   */
  public Object get( int offset )
    {
    return new Integer( myArray[ myIndex + offset ] );
    }

  /**
   * Write an object at a specified distance from my current position.
   * @param offset The offset from my current position.
   * @param object The object to write.
   */
  public void put( int offset, Object object )
    {
    myArray[ myIndex + offset ] = ((Integer) object).intValue();
    }

  /**
   * Return true if I'm positioned at the first item of my input stream.
   */
  public boolean atBegin()
    {
    return myIndex == 0;
    }

  /**
   * Return true if I'm positioned after the last item in my input stream.
   */
  public boolean atEnd()
    {
    return myIndex == myArray.length;
    }

  /**
   * Return true if there are more elements in my input stream.
   */
  public boolean hasMoreElements()
    {
    return myIndex < myArray.length;
    }

  /**
   * Advance by one.
   */
  public void advance()
    {
    myIndex++;
    }

  /**
   * Advance by a specified amount.
   * @param n The amount to advance.
   */
  public void advance( int n )
    {
    myIndex += n;
    }

  /**
   * Retreat by one.
   */
  public void retreat()
    {
    myIndex--;
    }

  /**
   * Retreat by a specified amount.
   * @param n The amount to retreat.
   */
  public void retreat( int n )
    {
    myIndex -= n;
    }

  /**
   * Return the next element in my input stream.
   */
  public Object nextElement()
    {
    return new Integer( myArray[ myIndex++ ] );
    }

  /**
   * Return the object at my current position.
   */
  public Object get()
    {
    return new Integer( myArray[ myIndex ] );
    }

  /**
   * Set the object at my current position to a specified value.
   * @param object The object to be written at my current position.
   */
  public void put( Object object )
    {
    myArray[ myIndex ] = ((Integer) object).intValue();
    }

  /**
   * Return the distance from myself to another iterator.
   * I should be before the specified iterator.
   * @param iterator The iterator to compare myself against.
   */
  public int distance( ForwardIterator iterator )
    {
    return ((IntIterator) iterator).myIndex - myIndex;
    }

  /**
   * Return null for my associated Container since none needs to exist.
   */
  public Container getContainer()
    {
    return myIntArray;
    }
  }
