-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathNativeScriptAbstractMap.java
More file actions
515 lines (462 loc) · 15.1 KB
/
NativeScriptAbstractMap.java
File metadata and controls
515 lines (462 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package com.tns;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* A base class for {@code Map} implementations.
*
* <p>
* Subclasses that permit new mappings to be added must override {@link #put}.
*
* <p>
* The default implementations of many methods are inefficient for large maps. For example in the default implementation, each call to {@link #get} performs a linear iteration of the entry set. Subclasses should override such methods to improve their performance.
*
* @since 1.2
*/
public abstract class NativeScriptAbstractMap<K, V> implements Map<K, V> {
// Lazily-initialized key set (for implementing {@link #keySet}).
Set<K> keySet;
// Lazily-initialized values collection (for implementing {@link #values}).
Collection<V> valuesCollection;
/**
* An immutable key-value mapping. Despite the name, this class is non-final
* and its subclasses may be mutable.
*
* @since 1.6
*/
public static class SimpleImmutableEntry<K extends Serializable, V extends Serializable> implements Map.Entry<K, V>, Serializable {
private static final long serialVersionUID = 7138329143949025153L;
private final K key;
private final V value;
public SimpleImmutableEntry(K theKey, V theValue) {
key = theKey;
value = theValue;
}
/**
* Constructs an instance with the key and value of {@code copyFrom}.
*/
public SimpleImmutableEntry(Map.Entry<? extends K, ? extends V> copyFrom) {
key = copyFrom.getKey();
value = copyFrom.getValue();
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
/**
* This base implementation throws {@code UnsupportedOperationException} always.
*/
public V setValue(V object) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object instanceof Map.Entry) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
return (key == null ? entry.getKey() == null : key.equals(entry.getKey())) && (value == null ? entry.getValue() == null : value.equals(entry.getValue()));
}
return false;
}
@Override
public int hashCode() {
return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
}
@Override
public String toString() {
return key + "=" + value;
}
}
/**
* A key-value mapping with mutable values.
*
* @since 1.6
*/
public static class SimpleEntry<K extends Serializable, V extends Serializable> implements Map.Entry<K, V>, Serializable {
private static final long serialVersionUID = -8499721149061103585L;
private final K key;
private V value;
public SimpleEntry(K theKey, V theValue) {
key = theKey;
value = theValue;
}
/**
* Constructs an instance with the key and value of {@code copyFrom}.
*/
public SimpleEntry(Map.Entry<? extends K, ? extends V> copyFrom) {
key = copyFrom.getKey();
value = copyFrom.getValue();
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V object) {
V result = value;
value = object;
return result;
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object instanceof Map.Entry) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
return (key == null ? entry.getKey() == null : key.equals(entry.getKey())) && (value == null ? entry.getValue() == null : value.equals(entry.getValue()));
}
return false;
}
@Override
public int hashCode() {
return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
}
@Override
public String toString() {
return key + "=" + value;
}
}
protected NativeScriptAbstractMap() {
}
/**
* {@inheritDoc}
*
* <p>
* This implementation calls {@code entrySet().clear()}.
*/
public void clear() {
entrySet().clear();
}
/**
* {@inheritDoc}
*
* <p>
* This implementation iterates its key set, looking for a key that {@code key} equals.
*/
public boolean containsKey(Object key) {
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
if (key != null) {
while (it.hasNext()) {
if (key.equals(it.next().getKey())) {
return true;
}
}
} else {
while (it.hasNext()) {
if (it.next().getKey() == null) {
return true;
}
}
}
return false;
}
/**
* {@inheritDoc}
*
* <p>
* This implementation iterates its entry set, looking for an entry with a value that {@code value} equals.
*/
public boolean containsValue(Object value) {
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
if (value != null) {
while (it.hasNext()) {
if (value.equals(it.next().getValue())) {
return true;
}
}
} else {
while (it.hasNext()) {
if (it.next().getValue() == null) {
return true;
}
}
}
return false;
}
public abstract Set<Map.Entry<K, V>> entrySet();
/**
* {@inheritDoc}
*
* <p>
* This implementation first checks the structure of {@code object}. If it is not a map or of a different size, this returns false. Otherwise it iterates its own entry set, looking up each entry's key in {@code object} . If any value does not equal the other map's value for the same key, this returns false. Otherwise it returns true.
*/
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
if (size() != map.size()) {
return false;
}
try {
for (Entry<K, V> entry : entrySet()) {
K key = entry.getKey();
V mine = entry.getValue();
Object theirs = map.get(key);
if (mine == null) {
if (theirs != null || !map.containsKey(key)) {
return false;
}
} else if (!mine.equals(theirs)) {
return false;
}
}
} catch (NullPointerException ignored) {
return false;
} catch (ClassCastException ignored) {
return false;
}
return true;
}
return false;
}
/**
* {@inheritDoc}
*
* <p>
* This implementation iterates its entry set, looking for an entry with a key that {@code key} equals.
*/
public V get(Object key) {
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
if (key != null) {
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
if (key.equals(entry.getKey())) {
return entry.getValue();
}
}
} else {
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
if (entry.getKey() == null) {
return entry.getValue();
}
}
}
return null;
}
/**
* {@inheritDoc}
*
* <p>
* This implementation iterates its entry set, summing the hashcodes of its entries.
*/
@Override
public int hashCode() {
int result = 0;
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
while (it.hasNext()) {
result += it.next().hashCode();
}
return result;
}
/**
* {@inheritDoc}
*
* <p>
* This implementation compares {@code size()} to 0.
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* {@inheritDoc}
*
* <p>
* This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.
*/
public Set<K> keySet() {
if (keySet == null) {
keySet = new AbstractSet<K>() {
@Override
public boolean contains(Object object) {
return containsKey(object);
}
@Override
public int size() {
return NativeScriptAbstractMap.this.size();
}
@Override
public Iterator<K> iterator() {
return new Iterator<K>() {
Iterator<Map.Entry<K, V>> setIterator = entrySet().iterator();
public boolean hasNext() {
return setIterator.hasNext();
}
public K next() {
return setIterator.next().getKey();
}
public void remove() {
setIterator.remove();
}
};
}
};
}
return keySet;
}
/**
* {@inheritDoc}
*
* <p>
* This base implementation throws {@code UnsupportedOperationException}.
*/
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* <p>
* This implementation iterates through {@code map}'s entry set, calling {@code put()} for each.
*/
public void putAll(Map<? extends K, ? extends V> map) {
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
/**
* {@inheritDoc}
*
* <p>
* This implementation iterates its entry set, removing the entry with a key that {@code key} equals.
*/
public V remove(Object key) {
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
if (key != null) {
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
if (key.equals(entry.getKey())) {
it.remove();
return entry.getValue();
}
}
} else {
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
if (entry.getKey() == null) {
it.remove();
return entry.getValue();
}
}
}
return null;
}
/**
* {@inheritDoc}
*
* <p>
* This implementation returns its entry set's size.
*/
public int size() {
return entrySet().size();
}
/**
* {@inheritDoc}
*
* <p>
* This implementation composes a string by iterating its entry set. If this map contains itself as a key or a value, the string "(this Map)" will appear in its place.
*/
@Override
public String toString() {
if (isEmpty()) {
return "{}";
}
StringBuilder buffer = new StringBuilder(size() * 28);
buffer.append('{');
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
Object key = entry.getKey();
if (key != this) {
buffer.append(key);
} else {
buffer.append("(this Map)");
}
buffer.append('=');
Object value = entry.getValue();
if (value != this) {
buffer.append(value);
} else {
buffer.append("(this Map)");
}
if (it.hasNext()) {
buffer.append(", ");
}
}
buffer.append('}');
return buffer.toString();
}
/**
* {@inheritDoc}
*
* <p>
* This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return values.
*/
public Collection<V> values() {
if (valuesCollection == null) {
valuesCollection = new AbstractCollection<V>() {
@Override
public int size() {
return NativeScriptAbstractMap.this.size();
}
@Override
public boolean contains(Object object) {
return containsValue(object);
}
@Override
public Iterator<V> iterator() {
return new Iterator<V>() {
Iterator<Map.Entry<K, V>> setIterator = entrySet().iterator();
public boolean hasNext() {
return setIterator.hasNext();
}
public V next() {
return setIterator.next().getValue();
}
public void remove() {
setIterator.remove();
}
};
}
};
}
return valuesCollection;
}
@SuppressWarnings("unchecked")
@Override
protected Object clone() throws CloneNotSupportedException {
NativeScriptAbstractMap<K, V> result = (NativeScriptAbstractMap<K, V>) super.clone();
result.keySet = null;
result.valuesCollection = null;
return result;
}
}