1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31
32
33 public class EntrySetToMapIteratorAdapter<K, V> implements MapIterator<K, V>, ResettableIterator<K> {
34
35
36 final Set<Map.Entry<K, V>> entrySet;
37
38
39 transient Iterator<Map.Entry<K, V>> iterator;
40
41
42 transient Map.Entry<K, V> entry;
43
44
45
46
47
48 public EntrySetToMapIteratorAdapter(final Set<Map.Entry<K, V>> entrySet) {
49 this.entrySet = entrySet;
50 reset();
51 }
52
53
54
55
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
66
67 @Override
68 public K getKey() {
69 return current().getKey();
70 }
71
72
73
74
75 @Override
76 public V getValue() {
77 return current().getValue();
78 }
79
80
81
82
83 @Override
84 public boolean hasNext() {
85 return iterator.hasNext();
86 }
87
88
89
90
91 @Override
92 public K next() {
93 entry = iterator.next();
94 return getKey();
95 }
96
97
98
99
100 @Override
101 public void remove() {
102 iterator.remove();
103 entry = null;
104 }
105
106
107
108
109 @Override
110 public synchronized void reset() {
111 iterator = entrySet.iterator();
112 }
113
114
115
116
117 @Override
118 public V setValue(final V value) {
119 return current().setValue(value);
120 }
121 }