Debian Codegraph @

debian/additionalSrc/trove4j/util/src/gnu/trove/generate/TObjectPIterator.template

raw
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 // THIS FILE IS AUTOGENERATED, PLEASE DO NOT EDIT OR ELSE
19 package gnu.trove;
20
21 import java.util.ConcurrentModificationException;
22
23 /**
24 * Iterator for maps of type Object and #e#.
25 * <p/>
26 * <p>The iterator semantics for Trove's primitive maps is slightly different
27 * from those defined in <tt>java.util.Iterator</tt>, but still well within
28 * the scope of the pattern, as defined by Gamma, et al.</p>
29 * <p/>
30 * <p>This iterator does <b>not</b> implicitly advance to the next entry when
31 * the value at the current position is retrieved. Rather, you must explicitly
32 * ask the iterator to <tt>advance()</tt> and then retrieve either the <tt>key()</tt>,
33 * the <tt>value()</tt> or both. This is done so that you have the option, but not
34 * the obligation, to retrieve keys and/or values as your application requires, and
35 * without introducing wrapper objects that would carry both. As the iteration is
36 * stateful, access to the key/value parts of the current map entry happens in
37 * constant time.</p>
38 * <p/>
39 * <p>In practice, the iterator is akin to a "search finger" that you move from
40 * position to position. Read or write operations affect the current entry only and
41 * do not assume responsibility for moving the finger.</p>
42 * <p/>
43 * <p>Here are some sample scenarios for this class of iterator:</p>
44 * <p/>
45 * <pre>
46 * // accessing keys/values through an iterator:
47 * for (TObject#E#Iterator it = map.iterator();
48 * it.hasNext();) {
49 * it.forward();
50 * if (satisfiesCondition(it.key()) {
51 * doSomethingWithValue(it.value());
52 * }
53 * }
54 * </pre>
55 * <p/>
56 * <pre>
57 * // modifying values in-place through iteration:
58 * for (TObject#E#Iterator it = map.iterator();
59 * it.hasNext();) {
60 * it.forward();
61 * if (satisfiesCondition(it.key()) {
62 * it.setValue(newValueForKey(it.key()));
63 * }
64 * }
65 * </pre>
66 * <p/>
67 * <pre>
68 * // deleting entries during iteration:
69 * for (TObject#E#Iterator it = map.iterator();
70 * it.hasNext();) {
71 * it.forward();
72 * if (satisfiesCondition(it.key()) {
73 * it.remove();
74 * }
75 * }
76 * </pre>
77 * <p/>
78 * <pre>
79 * // faster iteration by avoiding hasNext():
80 * TObject#E#Iterator iterator = map.iterator();
81 * for (int i = map.size(); i-- > 0;) {
82 * iterator.advance();
83 * doSomethingWithKeyAndValue(iterator.key(), iterator.value());
84 * }
85 * </pre>
86 *
87 * @author Eric D. Friedman
88 */
89
90 public class TObject#E#Iterator<K> extends TIterator {
91 private final TObject#E#HashMap<K> _map;
92
93 public TObject#E#Iterator(TObject#E#HashMap<K> map) {
94 super(map);
95 _map = map;
96 }
97
98 /**
99 * Returns the index of the next value in the data structure
100 * or a negative value if the iterator is exhausted.
101 *
102 * @return an <code>int</code> value
103 */
104 @Override
105 protected final int nextIndex() {
106 if (_expectedSize != _hash.size()) {
107 throw new ConcurrentModificationException();
108 }
109
110 Object[] set = _map._set;
111 int i = _index;
112 while (i-- > 0 && (set[i] == null || set[i] == TObjectHash.REMOVED)) ;
113 return i;
114 }
115
116 /**
117 * Moves the iterator forward to the next entry in the underlying map.
118 *
119 * @throws java.util.NoSuchElementException
120 * if the iterator is already exhausted
121 */
122 public void advance() {
123 moveToNextIndex();
124 }
125
126 /**
127 * Provides access to the key of the mapping at the iterator's position.
128 * Note that you must <tt>advance()</tt> the iterator at least once
129 * before invoking this method.
130 *
131 * @return the key of the entry at the iterator's current position.
132 */
133 public K key() {
134 //noinspection unchecked
135 return (K) _map._set[_index];
136 }
137
138 /**
139 * Provides access to the value of the mapping at the iterator's position.
140 * Note that you must <tt>advance()</tt> the iterator at least once
141 * before invoking this method.
142 *
143 * @return the value of the entry at the iterator's current position.
144 */
145 public #e# value() {
146 return _map._values[_index];
147 }
148
149 /**
150 * Replace the value of the mapping at the iterator's position with the
151 * specified value. Note that you must <tt>advance()</tt> the iterator at
152 * least once before invoking this method.
153 *
154 * @param val the value to set in the current entry
155 * @return the old value of the entry.
156 */
157 public #e# setValue(#e# val) {
158 #e# old = value();
159 _map._values[_index] = val;
160 return old;
161 }
162 }// TObject#E#Iterator