001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4;
018
019import java.util.Iterator;
020
021/**
022 * Defines an iterator that operates over a <code>Map</code>.
023 * <p>
024 * This iterator is a special version designed for maps. It can be more
025 * efficient to use this rather than an entry set iterator where the option
026 * is available, and it is certainly more convenient.
027 * <p>
028 * A map that provides this interface may not hold the data internally using
029 * Map Entry objects, thus this interface can avoid lots of object creation.
030 * <p>
031 * In use, this iterator iterates through the keys in the map. After each call
032 * to <code>next()</code>, the <code>getValue()</code> method provides direct
033 * access to the value. The value can also be set using <code>setValue()</code>.
034 * <pre>
035 * MapIterator<String,Integer> it = map.mapIterator();
036 * while (it.hasNext()) {
037 *   String key = it.next();
038 *   Integer value = it.getValue();
039 *   it.setValue(value + 1);
040 * }
041 * </pre>
042 *
043 * @param <K> the type of the keys in the map
044 * @param <V> the type of the values in the map
045 * @since 3.0
046 * @version $Id: MapIterator.html 972421 2015-11-14 20:00:04Z tn $
047 */
048public interface MapIterator<K, V> extends Iterator<K> {
049
050    /**
051     * Checks to see if there are more entries still to be iterated.
052     *
053     * @return <code>true</code> if the iterator has more elements
054     */
055    boolean hasNext();
056
057    /**
058     * Gets the next <em>key</em> from the <code>Map</code>.
059     *
060     * @return the next key in the iteration
061     * @throws java.util.NoSuchElementException if the iteration is finished
062     */
063    K next();
064
065    //-----------------------------------------------------------------------
066    /**
067     * Gets the current key, which is the key returned by the last call
068     * to <code>next()</code>.
069     *
070     * @return the current key
071     * @throws IllegalStateException if <code>next()</code> has not yet been called
072     */
073    K getKey();
074
075    /**
076     * Gets the current value, which is the value associated with the last key
077     * returned by <code>next()</code>.
078     *
079     * @return the current value
080     * @throws IllegalStateException if <code>next()</code> has not yet been called
081     */
082    V getValue();
083
084    //-----------------------------------------------------------------------
085    /**
086     * Removes the last returned key from the underlying <code>Map</code> (optional operation).
087     * <p>
088     * This method can be called once per call to <code>next()</code>.
089     *
090     * @throws UnsupportedOperationException if remove is not supported by the map
091     * @throws IllegalStateException if <code>next()</code> has not yet been called
092     * @throws IllegalStateException if <code>remove()</code> has already been called
093     *  since the last call to <code>next()</code>
094     */
095    void remove();
096
097    /**
098     * Sets the value associated with the current key (optional operation).
099     *
100     * @param value  the new value
101     * @return the previous value
102     * @throws UnsupportedOperationException if setValue is not supported by the map
103     * @throws IllegalStateException if <code>next()</code> has not yet been called
104     * @throws IllegalStateException if <code>remove()</code> has been called since the
105     *  last call to <code>next()</code>
106     */
107    V setValue(V value);
108
109}