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

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