EntrySetMapIterator.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.iterators;

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

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

  22. /**
  23.  * Implements a {@code MapIterator} using a Map entrySet.
  24.  * Reverse iteration is not supported.
  25.  * <pre>
  26.  * MapIterator it = map.mapIterator();
  27.  * while (it.hasNext()) {
  28.  *   Object key = it.next();
  29.  *   Object value = it.getValue();
  30.  *   it.setValue(newValue);
  31.  * }
  32.  * </pre>
  33.  *
  34.  * @param <K> the type of keys
  35.  * @param <V> the type of mapped values
  36.  * @since 3.0
  37.  */
  38. public class EntrySetMapIterator<K, V> implements MapIterator<K, V>, ResettableIterator<K> {

  39.     private final Map<K, V> map;
  40.     private Iterator<Map.Entry<K, V>> iterator;
  41.     private Map.Entry<K, V> last;
  42.     private boolean canRemove;

  43.     /**
  44.      * Constructs a new instance.
  45.      *
  46.      * @param map  the map to iterate over
  47.      */
  48.     public EntrySetMapIterator(final Map<K, V> map) {
  49.         this.map = map;
  50.         this.iterator = map.entrySet().iterator();
  51.     }

  52.     /**
  53.      * Gets the current key, which is the key returned by the last call
  54.      * to {@code next()}.
  55.      *
  56.      * @return the current key
  57.      * @throws IllegalStateException if {@code next()} has not yet been called
  58.      */
  59.     @Override
  60.     public K getKey() {
  61.         if (last == null) {
  62.             throw new IllegalStateException("Iterator getKey() can only be called after next() and before remove()");
  63.         }
  64.         return last.getKey();
  65.     }

  66.     /**
  67.      * Gets the current value, which is the value associated with the last key
  68.      * returned by {@code next()}.
  69.      *
  70.      * @return the current value
  71.      * @throws IllegalStateException if {@code next()} has not yet been called
  72.      */
  73.     @Override
  74.     public V getValue() {
  75.         if (last == null) {
  76.             throw new IllegalStateException("Iterator getValue() can only be called after next() and before remove()");
  77.         }
  78.         return last.getValue();
  79.     }

  80.     /**
  81.      * Checks to see if there are more entries still to be iterated.
  82.      *
  83.      * @return {@code true} if the iterator has more elements
  84.      */
  85.     @Override
  86.     public boolean hasNext() {
  87.         return iterator.hasNext();
  88.     }

  89.     /**
  90.      * Gets the next <em>key</em> from the {@code Map}.
  91.      *
  92.      * @return the next key in the iteration
  93.      * @throws java.util.NoSuchElementException if the iteration is finished
  94.      */
  95.     @Override
  96.     public K next() {
  97.         last = iterator.next();
  98.         canRemove = true;
  99.         return last.getKey();
  100.     }

  101.     /**
  102.      * Removes the last returned key from the underlying {@code Map}.
  103.      * <p>
  104.      * This method can be called once per call to {@code next()}.
  105.      *
  106.      * @throws UnsupportedOperationException if remove is not supported by the map
  107.      * @throws IllegalStateException if {@code next()} has not yet been called
  108.      * @throws IllegalStateException if {@code remove()} has already been called
  109.      *  since the last call to {@code next()}
  110.      */
  111.     @Override
  112.     public void remove() {
  113.         if (!canRemove) {
  114.             throw new IllegalStateException("Iterator remove() can only be called once after next()");
  115.         }
  116.         iterator.remove();
  117.         last = null;
  118.         canRemove = false;
  119.     }

  120.     /**
  121.      * Resets the state of the iterator.
  122.      */
  123.     @Override
  124.     public void reset() {
  125.         iterator = map.entrySet().iterator();
  126.         last = null;
  127.         canRemove = false;
  128.     }

  129.     /**
  130.      * Sets the value associated with the current key.
  131.      *
  132.      * @param value  the new value
  133.      * @return the previous value
  134.      * @throws UnsupportedOperationException if setValue is not supported by the map
  135.      * @throws IllegalStateException if {@code next()} has not yet been called
  136.      * @throws IllegalStateException if {@code remove()} has been called since the
  137.      *  last call to {@code next()}
  138.      */
  139.     @Override
  140.     public V setValue(final V value) {
  141.         if (last == null) {
  142.             throw new IllegalStateException("Iterator setValue() can only be called after next() and before remove()");
  143.         }
  144.         return last.setValue(value);
  145.     }

  146.     /**
  147.      * Gets the iterator as a String.
  148.      *
  149.      * @return a string version of the iterator
  150.      */
  151.     @Override
  152.     public String toString() {
  153.         if (last != null) {
  154.             return "MapIterator[" + getKey() + "=" + getValue() + "]";
  155.         }
  156.         return "MapIterator[]";
  157.     }

  158. }