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