Debian Codegraph @

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