]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/gnu/trove/TIterator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / gnu / trove / TIterator.java
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;
22 import java.util.NoSuchElementException;
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.3 2007/06/29 20:03:10 robeden 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();
69         }
70
71         // Disable auto compaction during the remove. This is a workaround for bug 1642768.
72         try {
73             _hash.tempDisableAutoCompaction();
74             _hash.removeAt(_index);
75         }
76         finally {
77             _hash.reenableAutoCompaction( false );
78         }
79
80         _expectedSize--;
81     }
82
83     /**
84      * Sets the internal <tt>index</tt> so that the `next' object
85      * can be returned.
86      */
87     protected final void moveToNextIndex() {
88         // doing the assignment && < 0 in one line shaves
89         // 3 opcodes...
90         if ((_index = nextIndex()) < 0) {
91             throw new NoSuchElementException();
92         }
93     }
94
95     /**
96      * Returns the index of the next value in the data structure
97      * or a negative value if the iterator is exhausted.
98      *
99      * @return an <code>int</code> value
100      */
101     abstract protected int nextIndex();
102 } // TIterator