    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}.
    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 * <p>
    029 * A map that provides this interface may not hold the data internally using
    030 * Map Entry objects, thus this interface can avoid lots of object creation.
    031 * </p>
    032 * <p>
    033 * In use, this iterator iterates through the keys in the map. After each call
    034 * to {@code next()}, the {@code getValue()} method provides direct
    035 * access to the value. The value can also be set using {@code setValue()}.
    036 * </p>
    037 * <pre>{@code
    038 * MapIterator<String,Integer> it = map.mapIterator();
    039 * while (it.hasNext()) {
    040 *   String key = it.next();
    041 *   Integer value = it.getValue();
    042 *   it.setValue(value + 1);
    043 * }
    044 * }</pre>
    045 *
    046 * @param <K> the type of the keys in the map
    047 * @param <V> the type of the values in the map
    048 * @since 3.0
    049 */
    050public interface MapIterator<K, V> extends Iterator<K> {
    051
    052    /**
    053     * Gets the current key, which is the key returned by the last call
    054     * to {@code next()}.
    055     *
    056     * @return the current key
    057     * @throws IllegalStateException if {@code next()} has not yet been called
    058     */
    059    K getKey();
    060
    061    /**
    062     * Gets the current value, which is the value associated with the last key
    063     * returned by {@code next()}.
    064     *
    065     * @return the current value
    066     * @throws IllegalStateException if {@code next()} has not yet been called
    067     */
    068    V getValue();
    069
    070    /**
    071     * Checks to see if there are more entries still to be iterated.
    072     *
    073     * @return {@code true} if the iterator has more elements
    074     */
    075    @Override
    076    boolean hasNext();
    077
    078    /**
    079     * Gets the next <em>key</em> from the {@code Map}.
    080     *
    081     * @return the next key in the iteration
    082     * @throws java.util.NoSuchElementException if the iteration is finished
    083     */
    084    @Override
    085    K next();
    086
    087    /**
    088     * Removes the last returned key from the underlying {@code Map} (optional operation).
    089     * <p>
    090     * This method can be called once per call to {@code next()}.
    091     * </p>
    092     *
    093     * @throws UnsupportedOperationException if remove is not supported by the map
    094     * @throws IllegalStateException if {@code next()} has not yet been called
    095     * @throws IllegalStateException if {@code remove()} has already been called
    096     *  since the last call to {@code next()}
    097     */
    098    @Override
    099    void remove();
    100
    101    /**
    102     * Sets the value associated with the current key (optional operation).
    103     *
    104     * @param value  the new value
    105     * @return the previous value
    106     * @throws UnsupportedOperationException if setValue is not supported by the map
    107     * @throws IllegalStateException if {@code next()} has not yet been called
    108     * @throws IllegalStateException if {@code remove()} has been called since the
    109     *  last call to {@code next()}
    110     */
    111    V setValue(V value);
    112
    113}