home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-10 | 7.3 KB | 275 lines |
- // Copyright(c) 1996 ObjectSpace, Inc.
- // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
-
- package jgl;
-
- import java.util.Vector;
-
- /**
- * A VectorIterator is a random access iterator that allows you to iterate through
- * the contents of a java.util.Vector.
- * <p>
- * @see jgl.RandomAccessIterator
- * @version 1.1
- * @author ObjectSpace, Inc.
- */
-
- public final class VectorIterator implements RandomAccessIterator
- {
- VectorArray myVectorArray;
- java.util.Vector myVector;
- int myIndex;
-
- /**
- * Return an iterator positioned at the first element of a particular java.util.Vector.
- * @param vector The Vector whose first element I will be positioned at.
- */
- public static VectorIterator begin( java.util.Vector vector )
- {
- return new VectorIterator( vector, 0, new VectorArray( vector ) );
- }
-
- /**
- * Return an iterator positioned at the first element of a particular java.util.Vector.
- * @param vector The Vector whose first element I will be positioned at.
- * @param vectorArray The container the iterator is associated with.
- */
- public static VectorIterator begin( java.util.Vector vector, VectorArray vectorArray )
- {
- return new VectorIterator( vector, 0, vectorArray );
- }
-
- /**
- * Return an iterator positioned immediately after the last element of a
- * particular java.util.Vector.
- * @param vector The Vector whose last element I will be positioned after.
- */
- public static VectorIterator end( java.util.Vector vector )
- {
- return new VectorIterator( vector, vector.size(), new VectorArray( vector ) );
- }
-
- /**
- * Return an iterator positioned immediately after the last element of a
- * particular java.util.Vector.
- * @param vector The Vector whose last element I will be positioned after.
- * @param vectorArray The container the iterator is associated with.
- */
- public static VectorIterator end( java.util.Vector vector, VectorArray vectorArray )
- {
- return new VectorIterator( vector, vector.size(), vectorArray );
- }
-
- /**
- * Construct myself to be an iterator with no associated data structure or position.
- */
- public VectorIterator()
- {
- }
-
- /**
- * Construct myself to be a copy of an existing iterator.
- * @param iterator The iterator to copy.
- */
- public VectorIterator( VectorIterator iterator )
- {
- myVectorArray = iterator.myVectorArray;
- myVector = iterator.myVector;
- myIndex = iterator.myIndex;
- }
-
- /**
- * Construct myself to be positioned at a particular index of a specific Vector.
- * @param vector My associated Vector.
- * @param index My associated index.
- */
- public VectorIterator( java.util.Vector vector, int index )
- {
- this( vector, index, new VectorArray( vector ) );
- }
-
- /**
- * Construct myself to be positioned at a particular index of a specific Vector.
- * @param vector My associated Vector.
- * @param index My associated index.
- * @param vectorArray The container the iterator is associated with.
- */
- public VectorIterator( java.util.Vector vector, int index, VectorArray vectorArray )
- {
- myVectorArray = vectorArray;
- myVector = vector;
- myIndex = index;
- }
-
- /**
- * Return a clone of myself.
- */
- public Object clone()
- {
- return new VectorIterator( 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 VectorIterator && equals( (VectorIterator) object );
- }
-
- /**
- * Return true if iterator is positioned at the same element as me.
- * @param iterator The iterator to compare myself against.
- */
- public boolean equals( VectorIterator iterator )
- {
- return iterator.myIndex == myIndex && iterator.myVector == myVector;
- }
-
- /**
- * Return true if I'm before a specified iterator.
- * @param iterator The iterator to compare myself against.
- */
- public boolean less( RandomAccessIterator iterator )
- {
- return myIndex < ((VectorIterator) iterator).myIndex;
- }
-
- /**
- * Return the object that is a specified distance from my current position.
- * @param offset The offset from my current position.
- * @exception ArrayIndexOutOfBoundsException If the adjusted index is invalid.
- */
- public Object get( int offset )
- {
- return myVector.elementAt( 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.
- * @exception ArrayIndexOutOfBoundsException If the adjusted index is invalid.
- */
- public void put( int offset, Object object )
- {
- myVector.setElementAt( object, myIndex + offset );
- }
-
- /**
- * 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 == myVector.size();
- }
-
- /**
- * Return true if there are more elements in my input stream.
- */
- public boolean hasMoreElements()
- {
- return myIndex < myVector.size();
- }
-
- /**
- * 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.
- * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
- */
- public Object nextElement()
- {
- return myVector.elementAt( myIndex++ );
- }
-
- /**
- * Return the object at my current position.
- * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
- */
- public Object get()
- {
- return myVector.elementAt( myIndex );
- }
-
- /**
- * Set the object at my current position to a specified value.
- * @param object The object to be written at my current position.
- * @exception ArrayIndexOutOfBoundsException If I'm positioned at an invalid index.
- */
- public void put( Object object )
- {
- myVector.setElementAt( object, myIndex );
- }
-
- /**
- * 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 ((VectorIterator) iterator).myIndex - myIndex;
- }
-
- /**
- * Return my current index.
- */
- public int index()
- {
- return myIndex;
- }
-
- /**
- * Return null for my associated Container since java.util.Vector
- * isn't in the JGL hierarchy (but should be ;-)
- */
- public Container getContainer()
- {
- return myVectorArray;
- }
- }
-
-