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

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