001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020:
021: import org.apache.commons.collections.MapIterator;
022: import org.apache.commons.collections.ResettableIterator;
023:
024: /**
025: * Implements a <code>MapIterator</code> using a Map entrySet.
026: * Reverse iteration is not supported.
027: * <pre>
028: * MapIterator it = map.mapIterator();
029: * while (it.hasNext()) {
030: * Object key = it.next();
031: * Object value = it.getValue();
032: * it.setValue(newValue);
033: * }
034: * </pre>
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: */
041: public class EntrySetMapIterator implements MapIterator,
042: ResettableIterator {
043:
044: private final Map map;
045: private Iterator iterator;
046: private Map.Entry last;
047: private boolean canRemove = false;
048:
049: /**
050: * Constructor.
051: *
052: * @param map the map to iterate over
053: */
054: public EntrySetMapIterator(Map map) {
055: super ();
056: this .map = map;
057: this .iterator = map.entrySet().iterator();
058: }
059:
060: //-----------------------------------------------------------------------
061: /**
062: * Checks to see if there are more entries still to be iterated.
063: *
064: * @return <code>true</code> if the iterator has more elements
065: */
066: public boolean hasNext() {
067: return iterator.hasNext();
068: }
069:
070: /**
071: * Gets the next <em>key</em> from the <code>Map</code>.
072: *
073: * @return the next key in the iteration
074: * @throws java.util.NoSuchElementException if the iteration is finished
075: */
076: public Object next() {
077: last = (Map.Entry) iterator.next();
078: canRemove = true;
079: return last.getKey();
080: }
081:
082: //-----------------------------------------------------------------------
083: /**
084: * Removes the last returned key from the underlying <code>Map</code>.
085: * <p>
086: * This method can be called once per call to <code>next()</code>.
087: *
088: * @throws UnsupportedOperationException if remove is not supported by the map
089: * @throws IllegalStateException if <code>next()</code> has not yet been called
090: * @throws IllegalStateException if <code>remove()</code> has already been called
091: * since the last call to <code>next()</code>
092: */
093: public void remove() {
094: if (canRemove == false) {
095: throw new IllegalStateException(
096: "Iterator remove() can only be called once after next()");
097: }
098: iterator.remove();
099: last = null;
100: canRemove = false;
101: }
102:
103: //-----------------------------------------------------------------------
104: /**
105: * Gets the current key, which is the key returned by the last call
106: * to <code>next()</code>.
107: *
108: * @return the current key
109: * @throws IllegalStateException if <code>next()</code> has not yet been called
110: */
111: public Object getKey() {
112: if (last == null) {
113: throw new IllegalStateException(
114: "Iterator getKey() can only be called after next() and before remove()");
115: }
116: return last.getKey();
117: }
118:
119: /**
120: * Gets the current value, which is the value associated with the last key
121: * returned by <code>next()</code>.
122: *
123: * @return the current value
124: * @throws IllegalStateException if <code>next()</code> has not yet been called
125: */
126: public Object getValue() {
127: if (last == null) {
128: throw new IllegalStateException(
129: "Iterator getValue() can only be called after next() and before remove()");
130: }
131: return last.getValue();
132: }
133:
134: /**
135: * Sets the value associated with the current key.
136: *
137: * @param value the new value
138: * @return the previous value
139: * @throws UnsupportedOperationException if setValue is not supported by the map
140: * @throws IllegalStateException if <code>next()</code> has not yet been called
141: * @throws IllegalStateException if <code>remove()</code> has been called since the
142: * last call to <code>next()</code>
143: */
144: public Object setValue(Object value) {
145: if (last == null) {
146: throw new IllegalStateException(
147: "Iterator setValue() can only be called after next() and before remove()");
148: }
149: return last.setValue(value);
150: }
151:
152: //-----------------------------------------------------------------------
153: /**
154: * Resets the state of the iterator.
155: */
156: public void reset() {
157: iterator = map.entrySet().iterator();
158: last = null;
159: canRemove = false;
160: }
161:
162: /**
163: * Gets the iterator as a String.
164: *
165: * @return a string version of the iterator
166: */
167: public String toString() {
168: if (last != null) {
169: return "MapIterator[" + getKey() + "=" + getValue() + "]";
170: } else {
171: return "MapIterator[]";
172: }
173: }
174:
175: }
|