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.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.AbstractList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.ListIterator;
28 import java.util.Map;
29 import java.util.function.Predicate;
30
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
33 import org.apache.commons.collections4.iterators.UnmodifiableListIterator;
34 import org.apache.commons.collections4.list.UnmodifiableList;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class LinkedMap<K, V> extends AbstractLinkedMap<K, V> implements Serializable, Cloneable {
72
73
74
75
76 static class LinkedMapList<K> extends AbstractList<K> {
77
78 private final LinkedMap<K, ?> parent;
79
80 LinkedMapList(final LinkedMap<K, ?> parent) {
81 this.parent = parent;
82 }
83
84 @Override
85 public void clear() {
86 throw new UnsupportedOperationException();
87 }
88
89 @Override
90 public boolean contains(final Object obj) {
91 return parent.containsKey(obj);
92 }
93
94 @Override
95 public boolean containsAll(final Collection<?> coll) {
96 return parent.keySet().containsAll(coll);
97 }
98
99 @Override
100 public K get(final int index) {
101 return parent.get(index);
102 }
103
104 @Override
105 public int indexOf(final Object obj) {
106 return parent.indexOf(obj);
107 }
108
109 @Override
110 public Iterator<K> iterator() {
111 return UnmodifiableIterator.unmodifiableIterator(parent.keySet().iterator());
112 }
113
114 @Override
115 public int lastIndexOf(final Object obj) {
116 return parent.indexOf(obj);
117 }
118
119 @Override
120 public ListIterator<K> listIterator() {
121 return UnmodifiableListIterator.unmodifiableListIterator(super.listIterator());
122 }
123
124 @Override
125 public ListIterator<K> listIterator(final int fromIndex) {
126 return UnmodifiableListIterator.unmodifiableListIterator(super.listIterator(fromIndex));
127 }
128
129 @Override
130 public K remove(final int index) {
131 throw new UnsupportedOperationException();
132 }
133
134 @Override
135 public boolean remove(final Object obj) {
136 throw new UnsupportedOperationException();
137 }
138
139 @Override
140 public boolean removeAll(final Collection<?> coll) {
141 throw new UnsupportedOperationException();
142 }
143
144
145
146
147 @Override
148 public boolean removeIf(final Predicate<? super K> filter) {
149 throw new UnsupportedOperationException();
150 }
151
152 @Override
153 public boolean retainAll(final Collection<?> coll) {
154 throw new UnsupportedOperationException();
155 }
156
157 @Override
158 public int size() {
159 return parent.size();
160 }
161
162 @Override
163 public List<K> subList(final int fromIndexInclusive, final int toIndexExclusive) {
164 return UnmodifiableList.unmodifiableList(super.subList(fromIndexInclusive, toIndexExclusive));
165 }
166
167 @Override
168 public Object[] toArray() {
169 return parent.keySet().toArray();
170 }
171
172 @Override
173 public <T> T[] toArray(final T[] array) {
174 return parent.keySet().toArray(array);
175 }
176 }
177
178
179 private static final long serialVersionUID = 9077234323521161066L;
180
181
182
183
184 public LinkedMap() {
185 super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
186 }
187
188
189
190
191
192
193
194 public LinkedMap(final int initialCapacity) {
195 super(initialCapacity);
196 }
197
198
199
200
201
202
203
204
205
206
207 public LinkedMap(final int initialCapacity, final float loadFactor) {
208 super(initialCapacity, loadFactor);
209 }
210
211
212
213
214
215
216
217 public LinkedMap(final Map<? extends K, ? extends V> map) {
218 super(map);
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public List<K> asList() {
239 return new LinkedMapList<>(this);
240 }
241
242
243
244
245
246
247 @Override
248 public LinkedMap<K, V> clone() {
249 return (LinkedMap<K, V>) super.clone();
250 }
251
252
253
254
255
256
257
258
259 public K get(final int index) {
260 return getEntry(index).getKey();
261 }
262
263
264
265
266
267
268
269
270 public V getValue(final int index) {
271 return getEntry(index).getValue();
272 }
273
274
275
276
277
278
279
280 public int indexOf(Object key) {
281 key = convertKey(key);
282 int i = 0;
283 for (LinkEntry<K, V> entry = header.after; entry != header; entry = entry.after, i++) {
284 if (isEqualKey(key, entry.key)) {
285 return i;
286 }
287 }
288 return CollectionUtils.INDEX_NOT_FOUND;
289 }
290
291
292
293
294
295
296
297
298 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
299 in.defaultReadObject();
300 doReadObject(in);
301 }
302
303
304
305
306
307
308
309
310
311 public V remove(final int index) {
312 return remove(get(index));
313 }
314
315
316
317
318
319
320
321 private void writeObject(final ObjectOutputStream out) throws IOException {
322 out.defaultWriteObject();
323 doWriteObject(out);
324 }
325
326 }