1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
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.
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.
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 ///////////////////////////////////////////////////////////////////////////////
21 import java.util.ConcurrentModificationException;
22 import java.util.NoSuchElementException;
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.
29 * @author Eric D. Friedman
30 * @version $Id: TIterator.java,v 1.3 2007/06/29 20:03:10 robeden Exp $
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. */
42 * Create an instance of TIterator over the specified THash.
44 public TIterator(THash hash) {
46 _expectedSize = _hash.size();
47 _index = _hash.capacity();
51 * Returns true if the iterator can be advanced past its current
54 * @return a <code>boolean</code> value
56 public boolean hasNext() {
57 return nextIndex() >= 0;
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
66 public void remove() {
67 if (_expectedSize != _hash.size()) {
68 throw new ConcurrentModificationException();
71 // Disable auto compaction during the remove. This is a workaround for bug 1642768.
73 _hash.tempDisableAutoCompaction();
74 _hash.removeAt(_index);
77 _hash.reenableAutoCompaction( false );
84 * Sets the internal <tt>index</tt> so that the `next' object
87 protected final void moveToNextIndex() {
88 // doing the assignment && < 0 in one line shaves
90 if ((_index = nextIndex()) < 0) {
91 throw new NoSuchElementException();
96 * Returns the index of the next value in the data structure
97 * or a negative value if the iterator is exhausted.
99 * @return an <code>int</code> value
101 abstract protected int nextIndex();