EntrySetToMapIteratorAdapter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.map;

  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import java.util.Set;

  21. import org.apache.commons.collections4.MapIterator;
  22. import org.apache.commons.collections4.ResettableIterator;

  23. /**
  24.  * Adapts a Map entrySet to the MapIterator interface.
  25.  *
  26.  * @param <K> the type of the keys in the map
  27.  * @param <V> the type of the values in the map
  28.  * @since 4.0
  29.  */
  30. public class EntrySetToMapIteratorAdapter<K, V> implements MapIterator<K, V>, ResettableIterator<K> {

  31.     /** The adapted Map entry Set. */
  32.     final Set<Map.Entry<K, V>> entrySet;

  33.     /** The resettable iterator in use. */
  34.     transient Iterator<Map.Entry<K, V>> iterator;

  35.     /** The currently positioned Map entry. */
  36.     transient Map.Entry<K, V> entry;

  37.     /**
  38.      * Create a new EntrySetToMapIteratorAdapter.
  39.      * @param entrySet  the entrySet to adapt
  40.      */
  41.     public EntrySetToMapIteratorAdapter(final Set<Map.Entry<K, V>> entrySet) {
  42.         this.entrySet = entrySet;
  43.         reset();
  44.     }

  45.     /**
  46.      * Gets the currently active entry.
  47.      * @return Map.Entry&lt;K, V&gt;
  48.      */
  49.     protected synchronized Map.Entry<K, V> current() {
  50.         if (entry == null) {
  51.             throw new IllegalStateException();
  52.         }
  53.         return entry;
  54.     }

  55.     /**
  56.      * {@inheritDoc}
  57.      */
  58.     @Override
  59.     public K getKey() {
  60.         return current().getKey();
  61.     }

  62.     /**
  63.      * {@inheritDoc}
  64.      */
  65.     @Override
  66.     public V getValue() {
  67.         return current().getValue();
  68.     }

  69.     /**
  70.      * {@inheritDoc}
  71.      */
  72.     @Override
  73.     public boolean hasNext() {
  74.         return iterator.hasNext();
  75.     }

  76.     /**
  77.      * {@inheritDoc}
  78.      */
  79.     @Override
  80.     public K next() {
  81.         entry = iterator.next();
  82.         return getKey();
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     @Override
  88.     public void remove() {
  89.         iterator.remove();
  90.         entry = null;
  91.     }

  92.     /**
  93.      * {@inheritDoc}
  94.      */
  95.     @Override
  96.     public synchronized void reset() {
  97.         iterator = entrySet.iterator();
  98.     }

  99.     /**
  100.      * {@inheritDoc}
  101.      */
  102.     @Override
  103.     public V setValue(final V value) {
  104.         return current().setValue(value);
  105.     }
  106. }