    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 *      https://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.map;
    018
    019import java.util.Iterator;
    020import java.util.Map;
    021import java.util.Set;
    022
    023import org.apache.commons.collections4.MapIterator;
    024import org.apache.commons.collections4.ResettableIterator;
    025
    026/**
    027 * Adapts a Map entrySet to the MapIterator interface.
    028 *
    029 * @param <K> The type of the keys in the map
    030 * @param <V> The type of the values in the map
    031 * @since 4.0
    032 */
    033public class EntrySetToMapIteratorAdapter<K, V> implements MapIterator<K, V>, ResettableIterator<K> {
    034
    035    /** The adapted Map entry Set. */
    036    final Set<Map.Entry<K, V>> entrySet;
    037
    038    /** The resettable iterator in use. */
    039    transient Iterator<Map.Entry<K, V>> iterator;
    040
    041    /** The currently positioned Map entry. */
    042    transient Map.Entry<K, V> entry;
    043
    044    /**
    045     * Create a new EntrySetToMapIteratorAdapter.
    046     *
    047     * @param entrySet  The entrySet to adapt
    048     */
    049    public EntrySetToMapIteratorAdapter(final Set<Map.Entry<K, V>> entrySet) {
    050        this.entrySet = entrySet;
    051        reset();
    052    }
    053
    054    /**
    055     * Gets the currently active entry.
    056     *
    057     * @return Map.Entry&lt;K, V&gt;
    058     */
    059    protected synchronized Map.Entry<K, V> current() {
    060        if (entry == null) {
    061            throw new IllegalStateException();
    062        }
    063        return entry;
    064    }
    065
    066    /**
    067     * {@inheritDoc}
    068     */
    069    @Override
    070    public K getKey() {
    071        return current().getKey();
    072    }
    073
    074    /**
    075     * {@inheritDoc}
    076     */
    077    @Override
    078    public V getValue() {
    079        return current().getValue();
    080    }
    081
    082    /**
    083     * {@inheritDoc}
    084     */
    085    @Override
    086    public boolean hasNext() {
    087        return iterator.hasNext();
    088    }
    089
    090    /**
    091     * {@inheritDoc}
    092     */
    093    @Override
    094    public K next() {
    095        entry = iterator.next();
    096        return getKey();
    097    }
    098
    099    /**
    100     * {@inheritDoc}
    101     */
    102    @Override
    103    public void remove() {
    104        iterator.remove();
    105        entry = null;
    106    }
    107
    108    /**
    109     * {@inheritDoc}
    110     */
    111    @Override
    112    public synchronized void reset() {
    113        iterator = entrySet.iterator();
    114    }
    115
    116    /**
    117     * {@inheritDoc}
    118     */
    119    @Override
    120    public V setValue(final V value) {
    121        return current().setValue(value);
    122    }
    123}