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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * A HashSetIterator is a forward iterator that allows you to iterate through
  8.  * the contents of a HashSet.
  9.  * <p>
  10.  * @see jgl.ForwardIterator
  11.  * @version 1.1
  12.  * @author ObjectSpace, Inc.
  13.  */
  14.  
  15. public final class HashSetIterator implements ForwardIterator
  16.   {
  17.   HashSet myHashSet;
  18.   HashSetNode myNode;
  19.  
  20.   /**
  21.    * Construct myself to be an iterator with no associated data structure or position.
  22.    */
  23.   public HashSetIterator()
  24.     {
  25.     }
  26.  
  27.   /**
  28.    * Construct myself to be a copy of an existing iterator.
  29.    * @param iterator The iterator to copy.
  30.    */
  31.   public HashSetIterator( HashSetIterator iterator )
  32.     {
  33.     myHashSet = iterator.myHashSet;
  34.     myNode = iterator.myNode;
  35.     }
  36.  
  37.   /**
  38.    * Construct myself to be positioned at a particular node in a specified Table.
  39.    * @param node My associated node.
  40.    * @param set My associated HashSet.
  41.    */
  42.   HashSetIterator( HashSetNode node, HashSet set )
  43.     {
  44.     myHashSet = set;
  45.     myNode = node;
  46.     }
  47.  
  48.   /**
  49.    * Return a clone of myself.
  50.    */
  51.   public Object clone()
  52.     {
  53.     return new HashSetIterator( 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 HashSetIterator && equals( (HashSetIterator) 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( HashSetIterator 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.     if ( myHashSet == null ) 
  81.       return false;
  82.  
  83.     for( int i = 0; i < myHashSet.length; i++ )
  84.       if( myHashSet.buckets[ i ] != null )
  85.         return myNode == myHashSet.buckets[ i ];
  86.  
  87.     return true;
  88.     }
  89.  
  90.   /**
  91.    * Return true if I'm positioned after the last item in my input stream.
  92.    */
  93.   public boolean atEnd()
  94.     {
  95.     return myNode == null;
  96.     }
  97.  
  98.   /**
  99.    * Return true if there are more elements in my input stream.
  100.    */
  101.   public boolean hasMoreElements()
  102.     {
  103.     return myNode != null;
  104.     }
  105.  
  106.   /**
  107.    * Advance by one.
  108.    */
  109.   public void advance()
  110.     {
  111.     myNode = ( myNode.next != null ? myNode.next : next( myNode ) );
  112.     }
  113.  
  114.   /**
  115.    * Advance by a specified amount.
  116.    * @param n The amount to advance.
  117.    */
  118.   public void advance( int n )
  119.     {
  120.     while( n-- > 0 )
  121.       advance();
  122.     }
  123.  
  124.   /**
  125.    * Return the next element in my input stream.
  126.    */
  127.   public Object nextElement()
  128.     {
  129.     Object object = myNode.object;
  130.     myNode = ( myNode.next != null ? myNode.next : next( myNode ) );
  131.     return object;
  132.     }
  133.  
  134.   /**
  135.    * Return the object at my current position.
  136.    */
  137.   public Object get()
  138.     {
  139.     return myNode.object;
  140.     }
  141.  
  142.   /**
  143.    * HashSet the object at my current position to a specified value.
  144.    * @param object The object to be written at my current position.
  145.    */
  146.   public void put( Object object )
  147.     {
  148.     myNode.object = object;
  149.     }
  150.  
  151.   /**
  152.    * Return the distance from myself to another iterator.
  153.    * I should be before the specified iterator.
  154.    * @param iterator The iterator to compare myself against.
  155.    */
  156.   public int distance( ForwardIterator iterator )
  157.     {
  158.     HashSetNode oldNode = myNode;
  159.     HashSetNode node = ((HashSetIterator) iterator).myNode;
  160.     int n = 0;
  161.  
  162.     while( myNode != node )
  163.       {
  164.       ++n;
  165.       myNode = ( myNode.next != null ? myNode.next : next( myNode ) );
  166.       }
  167.  
  168.     myNode = oldNode;
  169.     return n;
  170.     }
  171.  
  172.   /** 
  173.    * Return my associated HashSet
  174.    */
  175.   public Container getContainer() 
  176.     {
  177.     return myHashSet;
  178.     }
  179.  
  180.   private HashSetNode next( HashSetNode node )
  181.     {
  182.     for( int i = ( node.hash % myHashSet.length ) + 1; i < myHashSet.length; i++ )
  183.       if( myHashSet.buckets[ i ] != null )
  184.         return myHashSet.buckets[ i ];
  185.  
  186.     return null;
  187.     }
  188.   }
  189.  
  190.