001: /*
002: * Copyright 2003-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;
017:
018: import java.util.Iterator;
019:
020: /**
021: * Defines an iterator that operates over a <code>Map</code>.
022: * <p>
023: * This iterator is a special version designed for maps. It can be more
024: * efficient to use this rather than an entry set iterator where the option
025: * is available, and it is certainly more convenient.
026: * <p>
027: * A map that provides this interface may not hold the data internally using
028: * Map Entry objects, thus this interface can avoid lots of object creation.
029: * <p>
030: * In use, this iterator iterates through the keys in the map. After each call
031: * to <code>next()</code>, the <code>getValue()</code> method provides direct
032: * access to the value. The value can also be set using <code>setValue()</code>.
033: * <pre>
034: * MapIterator it = map.mapIterator();
035: * while (it.hasNext()) {
036: * Object key = it.next();
037: * Object value = it.getValue();
038: * it.setValue(newValue);
039: * }
040: * </pre>
041: *
042: * @since Commons Collections 3.0
043: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
044: *
045: * @author Stephen Colebourne
046: */
047: public interface MapIterator extends Iterator {
048:
049: /**
050: * Checks to see if there are more entries still to be iterated.
051: *
052: * @return <code>true</code> if the iterator has more elements
053: */
054: boolean hasNext();
055:
056: /**
057: * Gets the next <em>key</em> from the <code>Map</code>.
058: *
059: * @return the next key in the iteration
060: * @throws java.util.NoSuchElementException if the iteration is finished
061: */
062: Object next();
063:
064: //-----------------------------------------------------------------------
065: /**
066: * Gets the current key, which is the key returned by the last call
067: * to <code>next()</code>.
068: *
069: * @return the current key
070: * @throws IllegalStateException if <code>next()</code> has not yet been called
071: */
072: Object getKey();
073:
074: /**
075: * Gets the current value, which is the value associated with the last key
076: * returned by <code>next()</code>.
077: *
078: * @return the current value
079: * @throws IllegalStateException if <code>next()</code> has not yet been called
080: */
081: Object getValue();
082:
083: //-----------------------------------------------------------------------
084: /**
085: * Removes the last returned key from the underlying <code>Map</code> (optional operation).
086: * <p>
087: * This method can be called once per call to <code>next()</code>.
088: *
089: * @throws UnsupportedOperationException if remove is not supported by the map
090: * @throws IllegalStateException if <code>next()</code> has not yet been called
091: * @throws IllegalStateException if <code>remove()</code> has already been called
092: * since the last call to <code>next()</code>
093: */
094: void remove();
095:
096: /**
097: * Sets the value associated with the current key (optional operation).
098: *
099: * @param value the new value
100: * @return the previous value
101: * @throws UnsupportedOperationException if setValue is not supported by the map
102: * @throws IllegalStateException if <code>next()</code> has not yet been called
103: * @throws IllegalStateException if <code>remove()</code> has been called since the
104: * last call to <code>next()</code>
105: */
106: Object setValue(Object value);
107:
108: }
|