KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > THashIterator


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19 package gnu.trove;
20
21 import java.util.ConcurrentModificationException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 /**
26  * Implements all iterator functions for the hashed object set.
27  * Subclasses may override objectAtIndex to vary the object
28  * returned by calls to next() (e.g. for values, and Map.Entry
29  * objects).
30  *
31  * <p> Note that iteration is fastest if you forego the calls to
32  * <tt>hasNext</tt> in favor of checking the size of the structure
33  * yourself and then call next() that many times:
34  *
35  * <pre>
36  * Iterator i = collection.iterator();
37  * for (int size = collection.size(); size-- > 0;) {
38  * Object o = i.next();
39  * }
40  * </pre>
41  *
42  * <p>You may, of course, use the hasNext(), next() idiom too if
43  * you aren't in a performance critical spot.</p>
44  *
45  */

46 abstract class THashIterator<V> extends TIterator implements Iterator JavaDoc<V> {
47     protected final TObjectHash _hash;
48
49     /**
50      * Create an instance of THashIterator over the values of the TObjectHash
51      */

52     public THashIterator(TObjectHash hash) {
53         super(hash);
54         _hash = hash;
55     }
56     
57     /**
58      * Moves the iterator to the next Object and returns it.
59      *
60      * @return an <code>Object</code> value
61      * @exception ConcurrentModificationException if the structure
62      * was changed using a method that isn't on this iterator.
63      * @exception NoSuchElementException if this is called on an
64      * exhausted iterator.
65      */

66     public V next() {
67         moveToNextIndex();
68         return objectAtIndex(_index);
69     }
70
71     /**
72      * Returns the index of the next value in the data structure
73      * or a negative value if the iterator is exhausted.
74      *
75      * @return an <code>int</code> value
76      * @exception ConcurrentModificationException if the underlying
77      * collection's size has been modified since the iterator was
78      * created.
79      */

80     protected final int nextIndex() {
81         if (_expectedSize != _hash.size()) {
82             throw new ConcurrentModificationException JavaDoc();
83         }
84
85         Object JavaDoc[] set = _hash._set;
86         int i = _index;
87         while (i-- > 0 && (set[i] == TObjectHash.FREE || set[i] == TObjectHash.REMOVED)) ;
88         return i;
89     }
90
91     /**
92      * Returns the object at the specified index. Subclasses should
93      * implement this to return the appropriate object for the given
94      * index.
95      *
96      * @param index the index of the value to return.
97      * @return an <code>Object</code> value
98      */

99     abstract protected V objectAtIndex(int index);
100 } // THashIterator
101
Popular Tags