    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.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 *
    032 * @since 4.0
    033 */
    034public class EntrySetToMapIteratorAdapter<K, V> implements MapIterator<K, V>, ResettableIterator<K> {
    035
    036    /** The adapted Map entry Set. */
    037    final Set<Map.Entry<K, V>> entrySet;
    038
    039    /** The resettable iterator in use. */
    040    transient Iterator<Map.Entry<K, V>> iterator;
    041
    042    /** The currently positioned Map entry. */
    043    transient Map.Entry<K, V> entry;
    044
    045    /**
    046     * Create a new EntrySetToMapIteratorAdapter.
    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     * @return Map.Entry&lt;K, V&gt;
    057     */
    058    protected synchronized Map.Entry<K, V> current() {
    059        if (entry == null) {
    060            throw new IllegalStateException();
    061        }
    062        return entry;
    063    }
    064
    065    /**
    066     * {@inheritDoc}
    067     */
    068    @Override
    069    public K getKey() {
    070        return current().getKey();
    071    }
    072
    073    /**
    074     * {@inheritDoc}
    075     */
    076    @Override
    077    public V getValue() {
    078        return current().getValue();
    079    }
    080
    081    /**
    082     * {@inheritDoc}
    083     */
    084    @Override
    085    public boolean hasNext() {
    086        return iterator.hasNext();
    087    }
    088
    089    /**
    090     * {@inheritDoc}
    091     */
    092    @Override
    093    public K next() {
    094        entry = iterator.next();
    095        return getKey();
    096    }
    097
    098    /**
    099     * {@inheritDoc}
    100     */
    101    @Override
    102    public void remove() {
    103        iterator.remove();
    104        entry = null;
    105    }
    106
    107    /**
    108     * {@inheritDoc}
    109     */
    110    @Override
    111    public synchronized void reset() {
    112        iterator = entrySet.iterator();
    113    }
    114
    115    /**
    116     * {@inheritDoc}
    117     */
    118    @Override
    119    public V setValue(final V value) {
    120        return current().setValue(value);
    121    }
    122}