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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * A DListIterator is a bidirectional iterator that allows you to iterate through
  8.  * the contents of a DList.
  9.  * <p>
  10.  * @see jgl.BidirectionalIterator
  11.  * @version 1.1
  12.  * @author ObjectSpace, Inc.
  13.  */
  14.  
  15. public final class DListIterator implements BidirectionalIterator
  16.   {
  17.   DList myDList;
  18.   DListNode myNode;
  19.   
  20.   /**
  21.    * Construct myself to be an iterator with no associated object structure or position.
  22.    */
  23.   public DListIterator()
  24.     {
  25.     }
  26.  
  27.   /**
  28.    * Construct myself to be positioned at a particular node in a specified list.
  29.    * @param list My associated list.
  30.    * @param node My associated node.
  31.    */
  32.   DListIterator( DList list, DListNode node )
  33.     {
  34.     myDList = list;
  35.     myNode = node;
  36.     }
  37.  
  38.   /**
  39.    * Construct myself to be a copy of an existing iterator.
  40.    * @param iterator The iterator to copy.
  41.    */
  42.   public DListIterator( DListIterator iterator )
  43.     {
  44.     myDList = iterator.myDList;
  45.     myNode = iterator.myNode;
  46.     }
  47.  
  48.   /**
  49.    * Return a clone of myself.
  50.    */
  51.   public Object clone()
  52.     {
  53.     return new DListIterator( this );
  54.     }
  55.  
  56.   /**
  57.    * Return true if a specified object is the same kind of iterator as me 
  58.    * and is positioned at the same element.
  59.    * @param object Any object.
  60.    */
  61.   public boolean equals( Object object )
  62.     {
  63.     return object instanceof DListIterator && equals( (DListIterator) object );
  64.     }
  65.  
  66.   /**
  67.    * Return true if iterator is positioned at the same element as me.
  68.    * @param iterator The iterator to compare myself against.
  69.    */
  70.   public boolean equals( DListIterator iterator )
  71.     {
  72.     return myNode == iterator.myNode;
  73.     }
  74.  
  75.   /**
  76.    * Return true if I'm positioned at the first item of my input stream.
  77.    */
  78.   public boolean atBegin()
  79.     {
  80.     return myNode == myDList.myNode.next;
  81.     }
  82.  
  83.   /**
  84.    * Return true if I'm positioned after the last item in my input stream.
  85.    */
  86.   public boolean atEnd()
  87.     {
  88.     return myNode == myDList.myNode;
  89.     }
  90.  
  91.   /**
  92.    * Return true if there are more elements in my input stream.
  93.    */
  94.   public boolean hasMoreElements()
  95.     {
  96.     return myNode != myDList.myNode;
  97.     }
  98.  
  99.   /**
  100.    * Advance by one.
  101.    */
  102.   public void advance()
  103.     {
  104.     myNode = myNode.next;
  105.     }
  106.  
  107.   /**
  108.    * Advance by a specified amount.
  109.    * @param n The amount to advance.
  110.    */
  111.   public void advance( int n )
  112.     {
  113.     myNode = nodeAt( n );
  114.     }
  115.  
  116.   /**
  117.    * Retreat by one.
  118.    */
  119.   public void retreat()
  120.     {
  121.     myNode = myNode.previous;
  122.     }
  123.  
  124.   /**
  125.    * Retreat by a specified amount.
  126.    * @param n The amount to retreat.
  127.    */
  128.   public void retreat( int n )
  129.     {
  130.     myNode = nodeAt( -n );
  131.     }
  132.  
  133.   /**
  134.    * Return the next element in my input stream.
  135.    */
  136.   public Object nextElement()
  137.     {
  138.     Object object = myNode.object;
  139.     myNode = myNode.next;
  140.     return object;
  141.     }
  142.  
  143.   /**
  144.    * Return the object at my current position.
  145.    */
  146.   public Object get()
  147.     {
  148.     return myNode.object;
  149.     }
  150.  
  151.   /**
  152.    * Set the object at my current position to a specified value.
  153.    * @param object The object to be written at my current position.
  154.    */
  155.   public void put( Object object )
  156.     {
  157.     myNode.object = object;
  158.     }
  159.  
  160.   /**
  161.    * Return the object that is a specified distance from my current position.
  162.    * @param offset The offset from my current position.
  163.    */
  164.   public Object get( int offset )
  165.     {
  166.     return nodeAt( offset ).object;
  167.     }
  168.  
  169.   /**
  170.    * Write an object at a specified distance from my current position.
  171.    * @param offset The offset from my current position.
  172.    * @param object The object to write.
  173.    */
  174.   public void put( int offset, Object object )
  175.     {
  176.     nodeAt( offset ).object = object;
  177.     }
  178.  
  179.   /**
  180.    * Return the distance from myself to another iterator.
  181.    * I should be before the specified iterator.
  182.    * @param iterator The iterator to compare myself against.
  183.    */
  184.   public int distance( ForwardIterator iterator )
  185.     {
  186.     return distance( myNode, ((DListIterator) iterator).myNode );
  187.     }
  188.  
  189.   /**
  190.    * Return my current index.
  191.    */
  192.   public int index()
  193.     {
  194.     return distance( myDList.myNode.next, myNode );
  195.     }
  196.  
  197.   private int distance( DListNode from, DListNode to )
  198.     {
  199.     int n = 0;
  200.  
  201.     while( from != to )
  202.       {
  203.       ++n;
  204.       from = from.next;
  205.       }
  206.  
  207.     return n;
  208.     }  
  209.  
  210.   private DListNode nodeAt( int offset )
  211.     {
  212.     DListNode node = myNode;
  213.  
  214.     if( offset >= 0 )
  215.       while( offset-- > 0 )
  216.         node = node.next;
  217.     else
  218.       while( offset++ < 0 )
  219.         node = node.previous;
  220.  
  221.     return node;
  222.     }
  223.  
  224.   /** 
  225.    * Return my associated container.
  226.    */
  227.   public Container getContainer() 
  228.     {
  229.     return myDList;
  230.     }
  231.   }
  232.  
  233.