home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / slistiterator.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  3.7 KB  |  181 lines

  1. package jgl;
  2.  
  3. /**
  4.  * An SListIterator is a forward iterator that allows you to iterate through
  5.  * the contents of an SList.
  6.  * <p>
  7.  * @see jgl.ForwardIterator
  8.  * @version 1.1
  9.  * @author ObjectSpace, Inc.
  10.  */
  11.  
  12. public final class SListIterator implements ForwardIterator
  13.   {
  14.   SList mySList;
  15.   SListNode myNode;
  16.   
  17.   /**
  18.    * Construct myself to be an iterator with no associated data structure or position.
  19.    */
  20.   public SListIterator()
  21.     {
  22.     }
  23.  
  24.   /**
  25.    * Construct myself to be positioned at a particular node in a specified list.
  26.    * @param list My associated list.
  27.    * @param node My associated node.
  28.    */
  29.   SListIterator( SList list, SListNode node )
  30.     {
  31.     mySList = list;
  32.     myNode = node;
  33.     }
  34.  
  35.   /**
  36.    * Construct myself to be a copy of an existing iterator.
  37.    * @param iterator The iterator to copy.
  38.    */
  39.   public SListIterator( SListIterator iterator )
  40.     {
  41.     mySList = iterator.mySList;
  42.     myNode = iterator.myNode;
  43.     }
  44.  
  45.   /**
  46.    * Return a clone of myself.
  47.    */
  48.   public Object clone()
  49.     {
  50.     return new SListIterator( this );
  51.     }
  52.  
  53.   /**
  54.    * Return true if a specified object is the same kind of iterator as me 
  55.    * and is positioned at the same element.
  56.    * @param object Any object.
  57.    */
  58.   public boolean equals( Object object )
  59.     {
  60.     return object instanceof SListIterator && equals( (SListIterator) object );
  61.     }
  62.  
  63.   /**
  64.    * Return true if iterator is positioned at the same element as me.
  65.    * @param iterator The iterator to compare myself against.
  66.    */
  67.   public boolean equals( SListIterator iterator )
  68.     {
  69.     return myNode == iterator.myNode;
  70.     }
  71.  
  72.   /**
  73.    * Return true if I'm positioned at the first item of my input stream.
  74.    */
  75.   public boolean atBegin()
  76.     {
  77.     return myNode == mySList.myHead;
  78.     }
  79.  
  80.   /**
  81.    * Return true if I'm positioned after the last item in my input stream.
  82.    */
  83.   public boolean atEnd()
  84.     {
  85.     return myNode == null;
  86.     }
  87.  
  88.   /**
  89.    * Return true if there are more elements in my input stream.
  90.    */
  91.   public boolean hasMoreElements()
  92.     {
  93.     return myNode != null;
  94.     }
  95.  
  96.   /**
  97.    * Advance by one.
  98.    */
  99.   public void advance()
  100.     {
  101.     myNode = myNode.next;
  102.     }
  103.  
  104.   /**
  105.    * Advance by a specified amount.
  106.    * @param n The amount to advance.
  107.    */
  108.   public void advance( int n )
  109.     {
  110.     while( n-- > 0 )
  111.       myNode = myNode.next;
  112.     }
  113.  
  114.   /**
  115.    * Return the next element in my input stream.
  116.    */
  117.   public Object nextElement()
  118.     {
  119.     Object object = myNode.object;
  120.     myNode = myNode.next;
  121.     return object;
  122.     }
  123.  
  124.   /**
  125.    * Return the object at my current position.
  126.    */
  127.   public Object get()
  128.     {
  129.     return myNode.object;
  130.     }
  131.  
  132.   /**
  133.    * Set the object at my current position to a specified value.
  134.    * @param object The object to be written at my current position.
  135.    */
  136.   public void put( Object object )
  137.     {
  138.     myNode.object = object;
  139.     }
  140.  
  141.   /**
  142.    * Return the distance from myself to another iterator.
  143.    * I should be before the specified iterator.
  144.    * @param iterator The iterator to compare myself against.
  145.    */
  146.   public int distance( ForwardIterator iterator )
  147.     {
  148.     return distance( myNode, ((SListIterator) iterator).myNode );
  149.     }
  150.  
  151.   /**
  152.    * Return my current index.
  153.    */
  154.   public int index()
  155.     {
  156.     return distance( mySList.myHead, myNode );
  157.     }
  158.  
  159.   private int distance( SListNode from, SListNode to )
  160.     {
  161.     int n = 0;
  162.  
  163.     while( from != to )
  164.       {
  165.       ++n;
  166.       from = from.next;
  167.       }
  168.  
  169.     return n;
  170.     }    
  171.  
  172.   /** 
  173.    * Return my associated SList.
  174.    */
  175.   public Container getContainer() 
  176.     {
  177.     return mySList;
  178.     }
  179.   }
  180.  
  181.