home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-10 | 7.3 KB | 286 lines |
- // Copyright(c) 1996 ObjectSpace, Inc.
- // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
-
- package jgl;
-
- /**
- * An ShortIterator is a random access iterator that allows you to iterate through
- * an array of shorts.
- * <p>
- * @see jgl.RandomAccessIterator
- * @version 1.1
- * @author ObjectSpace, Inc.
- */
-
- public final class ShortIterator implements RandomAccessIterator
- {
- ShortArray myShortArray;
- short 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 ShortIterator begin( short array[] )
- {
- return new ShortIterator( array, 0, new ShortArray( 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 shortArray The container the iterator is associated with.
- */
- public static ShortIterator begin( short array[], ShortArray shortArray )
- {
- return new ShortIterator( array, 0, shortArray );
- }
-
- /**
- * 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 ShortIterator end( short array[] )
- {
- return new ShortIterator( array, array.length, new ShortArray( 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 shortArray The container the iterator is associated with.
- */
- public static ShortIterator end( short array[], ShortArray shortArray )
- {
- return new ShortIterator( array, array.length, shortArray );
- }
-
- /**
- * Construct myself to be an iterator with no associated data structure or position.
- */
- public ShortIterator()
- {
- }
-
- /**
- * Construct myself to be a copy of an existing iterator.
- * @param iterator The iterator to copy.
- */
- public ShortIterator( ShortIterator iterator )
- {
- myShortArray = iterator.myShortArray;
- myArray = iterator.myArray;
- myIndex = iterator.myIndex;
- }
-
- /**
- * 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 ShortIterator( short array[] )
- {
- this( array, 0, new ShortArray( 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 shortArray The container the iterator is associated with.
- */
- public ShortIterator( short array[], ShortArray shortArray )
- {
- this( array, 0, shortArray );
- }
-
- /**
- * Construct myself to be positioned at a particular index of a specific array.
- * @param array My associated array.
- * @param i My associated index.
- */
- public ShortIterator( short array[], int index )
- {
- this( array, index, new ShortArray( array ) );
- }
-
- /**
- * Construct myself to be positioned at a particular index of a specific array.
- * @param array My associated array.
- * @param i My associated index.
- * @param shortArray The container the iterator is associated with.
- */
- public ShortIterator( short array[], int index, ShortArray shortArray )
- {
- myShortArray = shortArray;
- myArray = array;
- myIndex = index;
- }
-
- /**
- * Return a clone of myself.
- */
- public Object clone()
- {
- return new ShortIterator( this );
- }
-
- /**
- * Return my current index.
- */
- public int index()
- {
- return myIndex;
- }
-
- /**
- * 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 ShortIterator && equals( (ShortIterator) object );
- }
-
- /**
- * Return true if iterator is positioned at the same element as me.
- * @param iterator The iterator to compare myself against.
- */
- public boolean equals( ShortIterator 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 iter )
- {
- return myIndex < ((ShortIterator) iter).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 ] = (short) ((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 ] = (short) ((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 ((ShortIterator) iterator).myIndex - myIndex;
- }
-
- /**
- * Return null for my associated Container since none needs to exist.
- */
- public Container getContainer()
- {
- return myShortArray;
- }
- }
-
-