View Javadoc
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  
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.apache.commons.collections4.MapIterator;
24  import org.apache.commons.collections4.ResettableIterator;
25  
26  /**
27   * Adapts a Map entrySet to the MapIterator interface.
28   *
29   * @param <K> the type of the keys in the map
30   * @param <V> the type of the values in the map
31   *
32   * @since 4.0
33   */
34  public class EntrySetToMapIteratorAdapter<K, V> implements MapIterator<K, V>, ResettableIterator<K> {
35  
36      /** The adapted Map entry Set. */
37      final Set<Map.Entry<K, V>> entrySet;
38  
39      /** The resettable iterator in use. */
40      transient Iterator<Map.Entry<K, V>> iterator;
41  
42      /** The currently positioned Map entry. */
43      transient Map.Entry<K, V> entry;
44  
45      /**
46       * Create a new EntrySetToMapIteratorAdapter.
47       * @param entrySet  the entrySet to adapt
48       */
49      public EntrySetToMapIteratorAdapter(final Set<Map.Entry<K, V>> entrySet) {
50          this.entrySet = entrySet;
51          reset();
52      }
53  
54      /**
55       * Gets the currently active entry.
56       * @return Map.Entry&lt;K, V&gt;
57       */
58      protected synchronized Map.Entry<K, V> current() {
59          if (entry == null) {
60              throw new IllegalStateException();
61          }
62          return entry;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public K getKey() {
70          return current().getKey();
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public V getValue() {
78          return current().getValue();
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public boolean hasNext() {
86          return iterator.hasNext();
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public K next() {
94          entry = iterator.next();
95          return getKey();
96      }
97  
98      /**
99       * {@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 }