KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > TIterator


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.NoSuchElementException JavaDoc;
23
24 /**
25  * Abstract iterator class for THash implementations. This class provides some
26  * of the common iterator operations (hasNext(), remove()) and allows subclasses
27  * to define the mechanism(s) for advancing the iterator and returning data.
28  *
29  * @author Eric D. Friedman
30  * @version $Id: TIterator.java,v 1.1 2002/09/22 21:53:42 ericdf Exp $
31  */

32 abstract class TIterator {
33     /** the data structure this iterator traverses */
34     protected final THash _hash;
35     /** the number of elements this iterator believes are in the
36      * data structure it accesses. */

37     protected int _expectedSize;
38     /** the index used for iteration. */
39     protected int _index;
40
41     /**
42      * Create an instance of TIterator over the specified THash.
43      */

44     public TIterator(THash hash) {
45         _hash = hash;
46         _expectedSize = _hash.size();
47         _index = _hash.capacity();
48     }
49
50     /**
51      * Returns true if the iterator can be advanced past its current
52      * location.
53      *
54      * @return a <code>boolean</code> value
55      */

56     public boolean hasNext() {
57         return nextIndex() >= 0;
58     }
59
60     /**
61      * Removes the last entry returned by the iterator.
62      * Invoking this method more than once for a single entry
63      * will leave the underlying data structure in a confused
64      * state.
65      */

66     public void remove() {
67         if (_expectedSize != _hash.size()) {
68             throw new ConcurrentModificationException JavaDoc();
69         }
70         _hash.removeAt(_index);
71         _expectedSize--;
72     }
73
74     /**
75      * Sets the internal <tt>index</tt> so that the `next' object
76      * can be returned.
77      */

78     protected final void moveToNextIndex() {
79         // doing the assignment && < 0 in one line shaves
80
// 3 opcodes...
81
if ((_index = nextIndex()) < 0) {
82             throw new NoSuchElementException JavaDoc();
83         }
84     }
85
86     /**
87      * Returns the index of the next value in the data structure
88      * or a negative value if the iterator is exhausted.
89      *
90      * @return an <code>int</code> value
91      */

92     abstract protected int nextIndex();
93 } // TIterator
94
Popular Tags