    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 * @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     * @param entrySet  the entrySet to adapt
    047     */
    048    public EntrySetToMapIteratorAdapter(final Set<Map.Entry<K, V>> entrySet) {
    049        this.entrySet = entrySet;
    050        reset();
    051    }
    052
    053    /**
    054     * Gets the currently active entry.
    055     * @return Map.Entry&lt;K, V&gt;
    056     */
    057    protected synchronized Map.Entry<K, V> current() {
    058        if (entry == null) {
    059            throw new IllegalStateException();
    060        }
    061        return entry;
    062    }
    063
    064    /**
    065     * {@inheritDoc}
    066     */
    067    @Override
    068    public K getKey() {
    069        return current().getKey();
    070    }
    071
    072    /**
    073     * {@inheritDoc}
    074     */
    075    @Override
    076    public V getValue() {
    077        return current().getValue();
    078    }
    079
    080    /**
    081     * {@inheritDoc}
    082     */
    083    @Override
    084    public boolean hasNext() {
    085        return iterator.hasNext();
    086    }
    087
    088    /**
    089     * {@inheritDoc}
    090     */
    091    @Override
    092    public K next() {
    093        entry = iterator.next();
    094        return getKey();
    095    }
    096
    097    /**
    098     * {@inheritDoc}
    099     */
    100    @Override
    101    public void remove() {
    102        iterator.remove();
    103        entry = null;
    104    }
    105
    106    /**
    107     * {@inheritDoc}
    108     */
    109    @Override
    110    public synchronized void reset() {
    111        iterator = entrySet.iterator();
    112    }
    113
    114    /**
    115     * {@inheritDoc}
    116     */
    117    @Override
    118    public V setValue(final V value) {
    119        return current().setValue(value);
    120    }
    121}