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.lang.reflect.Array;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.collections4.iterators.AbstractIteratorDecorator;
25 import org.apache.commons.collections4.keyvalue.AbstractMapEntryDecorator;
26 import org.apache.commons.collections4.set.AbstractSetDecorator;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 abstract class AbstractInputCheckedMapDecorator<K, V>
48 extends AbstractMapDecorator<K, V> {
49
50
51
52
53 private final class EntrySet extends AbstractSetDecorator<Map.Entry<K, V>> {
54
55
56 private static final long serialVersionUID = 4354731610923110264L;
57
58
59 private final AbstractInputCheckedMapDecorator<K, V> parent;
60
61 protected EntrySet(final Set<Map.Entry<K, V>> set, final AbstractInputCheckedMapDecorator<K, V> parent) {
62 super(set);
63 this.parent = parent;
64 }
65
66 @Override
67 public Iterator<Map.Entry<K, V>> iterator() {
68 return new EntrySetIterator(decorated().iterator(), parent);
69 }
70
71 @Override
72 @SuppressWarnings("unchecked")
73 public Object[] toArray() {
74 final Object[] array = decorated().toArray();
75 for (int i = 0; i < array.length; i++) {
76 array[i] = new MapEntry((Map.Entry<K, V>) array[i], parent);
77 }
78 return array;
79 }
80
81 @Override
82 @SuppressWarnings("unchecked")
83 public <T> T[] toArray(final T[] array) {
84 Object[] result = array;
85 if (array.length > 0) {
86
87
88 result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0);
89 }
90 result = decorated().toArray(result);
91 for (int i = 0; i < result.length; i++) {
92 result[i] = new MapEntry((Map.Entry<K, V>) result[i], parent);
93 }
94
95
96 if (result.length > array.length) {
97 return (T[]) result;
98 }
99
100
101 System.arraycopy(result, 0, array, 0, result.length);
102 if (array.length > result.length) {
103 array[result.length] = null;
104 }
105 return array;
106 }
107 }
108
109
110
111
112 private final class EntrySetIterator extends AbstractIteratorDecorator<Map.Entry<K, V>> {
113
114
115 private final AbstractInputCheckedMapDecorator<K, V> parent;
116
117 protected EntrySetIterator(final Iterator<Map.Entry<K, V>> iterator,
118 final AbstractInputCheckedMapDecorator<K, V> parent) {
119 super(iterator);
120 this.parent = parent;
121 }
122
123 @Override
124 public Map.Entry<K, V> next() {
125 final Map.Entry<K, V> entry = getIterator().next();
126 return new MapEntry(entry, parent);
127 }
128 }
129
130
131
132
133 private final class MapEntry extends AbstractMapEntryDecorator<K, V> {
134
135
136 private final AbstractInputCheckedMapDecorator<K, V> parent;
137
138 protected MapEntry(final Map.Entry<K, V> entry, final AbstractInputCheckedMapDecorator<K, V> parent) {
139 super(entry);
140 this.parent = parent;
141 }
142
143 @Override
144 public V setValue(V value) {
145 value = parent.checkSetValue(value);
146 return getMapEntry().setValue(value);
147 }
148 }
149
150
151
152
153 protected AbstractInputCheckedMapDecorator() {
154 }
155
156
157
158
159
160
161
162 protected AbstractInputCheckedMapDecorator(final Map<K, V> map) {
163 super(map);
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 protected abstract V checkSetValue(V value);
184
185 @Override
186 public Set<Map.Entry<K, V>> entrySet() {
187 if (isSetValueChecking()) {
188 return new EntrySet(map.entrySet(), this);
189 }
190 return map.entrySet();
191 }
192
193
194
195
196
197
198
199
200
201
202
203 protected boolean isSetValueChecking() {
204 return true;
205 }
206
207 }