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