home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-10 | 4.9 KB | 233 lines |
- // Copyright(c) 1996 ObjectSpace, Inc.
- // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
-
- package jgl;
-
- /**
- * A DListIterator is a bidirectional iterator that allows you to iterate through
- * the contents of a DList.
- * <p>
- * @see jgl.BidirectionalIterator
- * @version 1.1
- * @author ObjectSpace, Inc.
- */
-
- public final class DListIterator implements BidirectionalIterator
- {
- DList myDList;
- DListNode myNode;
-
- /**
- * Construct myself to be an iterator with no associated object structure or position.
- */
- public DListIterator()
- {
- }
-
- /**
- * Construct myself to be positioned at a particular node in a specified list.
- * @param list My associated list.
- * @param node My associated node.
- */
- DListIterator( DList list, DListNode node )
- {
- myDList = list;
- myNode = node;
- }
-
- /**
- * Construct myself to be a copy of an existing iterator.
- * @param iterator The iterator to copy.
- */
- public DListIterator( DListIterator iterator )
- {
- myDList = iterator.myDList;
- myNode = iterator.myNode;
- }
-
- /**
- * Return a clone of myself.
- */
- public Object clone()
- {
- return new DListIterator( 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 DListIterator && equals( (DListIterator) object );
- }
-
- /**
- * Return true if iterator is positioned at the same element as me.
- * @param iterator The iterator to compare myself against.
- */
- public boolean equals( DListIterator iterator )
- {
- return myNode == iterator.myNode;
- }
-
- /**
- * Return true if I'm positioned at the first item of my input stream.
- */
- public boolean atBegin()
- {
- return myNode == myDList.myNode.next;
- }
-
- /**
- * Return true if I'm positioned after the last item in my input stream.
- */
- public boolean atEnd()
- {
- return myNode == myDList.myNode;
- }
-
- /**
- * Return true if there are more elements in my input stream.
- */
- public boolean hasMoreElements()
- {
- return myNode != myDList.myNode;
- }
-
- /**
- * Advance by one.
- */
- public void advance()
- {
- myNode = myNode.next;
- }
-
- /**
- * Advance by a specified amount.
- * @param n The amount to advance.
- */
- public void advance( int n )
- {
- myNode = nodeAt( n );
- }
-
- /**
- * Retreat by one.
- */
- public void retreat()
- {
- myNode = myNode.previous;
- }
-
- /**
- * Retreat by a specified amount.
- * @param n The amount to retreat.
- */
- public void retreat( int n )
- {
- myNode = nodeAt( -n );
- }
-
- /**
- * Return the next element in my input stream.
- */
- public Object nextElement()
- {
- Object object = myNode.object;
- myNode = myNode.next;
- return object;
- }
-
- /**
- * Return the object at my current position.
- */
- public Object get()
- {
- return myNode.object;
- }
-
- /**
- * 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 )
- {
- myNode.object = object;
- }
-
- /**
- * 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 nodeAt( offset ).object;
- }
-
- /**
- * 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 )
- {
- nodeAt( offset ).object = object;
- }
-
- /**
- * 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 distance( myNode, ((DListIterator) iterator).myNode );
- }
-
- /**
- * Return my current index.
- */
- public int index()
- {
- return distance( myDList.myNode.next, myNode );
- }
-
- private int distance( DListNode from, DListNode to )
- {
- int n = 0;
-
- while( from != to )
- {
- ++n;
- from = from.next;
- }
-
- return n;
- }
-
- private DListNode nodeAt( int offset )
- {
- DListNode node = myNode;
-
- if( offset >= 0 )
- while( offset-- > 0 )
- node = node.next;
- else
- while( offset++ < 0 )
- node = node.previous;
-
- return node;
- }
-
- /**
- * Return my associated container.
- */
- public Container getContainer()
- {
- return myDList;
- }
- }
-
-