-
Notifications
You must be signed in to change notification settings - Fork 107
/
CompactMap.java
1089 lines (1001 loc) · 34 KB
/
CompactMap.java
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.cedarsoftware.util;
import java.lang.reflect.Constructor;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
/**
* Many developers do not realize than they may have thousands or hundreds of thousands of Maps in memory, often
* representing small JSON objects. These maps (often HashMaps) usually have a table of 16/32/64... elements in them,
* with many empty elements. HashMap doubles it's internal storage each time it expands, so often these Maps have
* barely 50% of these arrays filled.<p></p>
*
* CompactMap is a Map that strives to reduce memory at all costs while retaining speed that is close to HashMap's speed.
* It does this by using only one (1) member variable (of type Object) and changing it as the Map grows. It goes from
* single value, to a single MapEntry, to an Object[], and finally it uses a Map (user defined). CompactMap is
* especially small when 0 and 1 entries are stored in it. When size() is from `2` to compactSize(), then entries
* are stored internally in single Object[]. If the size() is {@literal >} compactSize() then the entries are stored in a
* regular `Map`.<pre>
*
* Methods you may want to override:
*
* // If this key is used and only 1 element then only the value is stored
* protected K getSingleValueKey() { return "someKey"; }
*
* // Map you would like it to use when size() {@literal >} compactSize(). HashMap is default
* protected abstract Map{@literal <}K, V{@literal >} getNewMap();
*
* // If you want case insensitivity, return true and return new CaseInsensitiveMap or TreeMap(String.CASE_INSENSITIVE_ORDER) from getNewMap()
* protected boolean isCaseInsensitive() { return false; }
*
* // When size() {@literal >} than this amount, the Map returned from getNewMap() is used to store elements.
* protected int compactSize() { return 80; }
*
* </pre>
* **Empty**
* This class only has one (1) member variable of type `Object`. If there are no entries in it, then the value of that
* member variable takes on a pointer (points to sentinel value.)<p></p>
*
* **One entry**
* If the entry has a key that matches the value returned from `getSingleValueKey()` then there is no key stored
* and the internal single member points to the value only.<p></p>
*
* If the single entry's key does not match the value returned from `getSingleValueKey()` then the internal field points
* to an internal `Class` `CompactMapEntry` which contains the key and the value (nothing else). Again, all APIs still operate
* the same.<p></p>
*
* **Two thru compactSize() entries**
* In this case, the single member variable points to a single Object[] that contains all the keys and values. The
* keys are in the even positions, the values are in the odd positions (1 up from the key). [0] = key, [1] = value,
* [2] = next key, [3] = next value, and so on. The Object[] is dynamically expanded until size() {@literal >} compactSize(). In
* addition, it is dynamically shrunk until the size becomes 1, and then it switches to a single Map Entry or a single
* value.<p></p>
*
* **size() greater than compactSize()**
* In this case, the single member variable points to a `Map` instance (supplied by `getNewMap()` API that user supplied.)
* This allows `CompactMap` to work with nearly all `Map` types.<p></p>
*
* This Map supports null for the key and values, as long as the Map returned by getNewMap() supports null keys-values.
*
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
@SuppressWarnings("unchecked")
public class CompactMap<K, V> implements Map<K, V>
{
private static final String EMPTY_MAP = "_︿_ψ_☼";
private Object val = EMPTY_MAP;
public CompactMap()
{
if (compactSize() < 2)
{
throw new IllegalStateException("compactSize() must be >= 2");
}
}
public CompactMap(Map<K, V> other)
{
this();
putAll(other);
}
public int size()
{
if (val instanceof Object[])
{ // 2 to compactSize
return ((Object[])val).length >> 1;
}
else if (val instanceof Map)
{ // > compactSize
return ((Map<K, V>)val).size();
}
else if (val == EMPTY_MAP)
{ // empty
return 0;
}
// size == 1
return 1;
}
public boolean isEmpty()
{
return val == EMPTY_MAP;
}
private boolean compareKeys(Object key, Object aKey)
{
if (key instanceof String)
{
if (aKey instanceof String)
{
if (isCaseInsensitive())
{
return ((String)aKey).equalsIgnoreCase((String) key);
}
else
{
return aKey.equals(key);
}
}
return false;
}
return Objects.equals(key, aKey);
}
public boolean containsKey(Object key)
{
if (val instanceof Object[])
{ // 2 to compactSize
Object[] entries = (Object[]) val;
final int len = entries.length;
for (int i=0; i < len; i += 2)
{
if (compareKeys(key, entries[i]))
{
return true;
}
}
return false;
}
else if (val instanceof Map)
{ // > compactSize
Map<K, V> map = (Map<K, V>) val;
return map.containsKey(key);
}
else if (val == EMPTY_MAP)
{ // empty
return false;
}
// size == 1
return compareKeys(key, getLogicalSingleKey());
}
public boolean containsValue(Object value)
{
if (val instanceof Object[])
{ // 2 to Compactsize
Object[] entries = (Object[]) val;
final int len = entries.length;
for (int i=0; i < len; i += 2)
{
Object aValue = entries[i + 1];
if (Objects.equals(value, aValue))
{
return true;
}
}
return false;
}
else if (val instanceof Map)
{ // > compactSize
Map<K, V> map = (Map<K, V>) val;
return map.containsValue(value);
}
else if (val == EMPTY_MAP)
{ // empty
return false;
}
// size == 1
return getLogicalSingleValue() == value;
}
public V get(Object key)
{
if (val instanceof Object[])
{ // 2 to compactSize
Object[] entries = (Object[]) val;
final int len = entries.length;
for (int i=0; i < len; i += 2)
{
Object aKey = entries[i];
if (compareKeys(key, aKey))
{
return (V) entries[i + 1];
}
}
return null;
}
else if (val instanceof Map)
{ // > compactSize
Map<K, V> map = (Map<K, V>) val;
return map.get(key);
}
else if (val == EMPTY_MAP)
{ // empty
return null;
}
// size == 1
return compareKeys(key, getLogicalSingleKey()) ? getLogicalSingleValue() : null;
}
public V put(K key, V value)
{
if (val instanceof Object[])
{ // 2 to compactSize
Object[] entries = (Object[]) val;
final int len = entries.length;
for (int i=0; i < len; i += 2)
{
Object aKey = entries[i];
Object aValue = entries[i + 1];
if (compareKeys(key, aKey))
{ // Overwrite case
entries[i + 1] = value;
return (V) aValue;
}
}
// Not present in Object[]
if (size() < compactSize())
{ // Grow array
Object[] expand = new Object[len + 2];
System.arraycopy(entries, 0, expand, 0, len);
// Place new entry at end
expand[expand.length - 2] = key;
expand[expand.length - 1] = value;
val = expand;
}
else
{ // Switch to Map - copy entries
Map<K, V> map = getNewMap(size() + 1);
entries = (Object[]) val;
final int len2 = entries.length;
for (int i=0; i < len2; i += 2)
{
Object aKey = entries[i];
Object aValue = entries[i + 1];
map.put((K) aKey, (V) aValue);
}
// Place new entry
map.put(key, value);
val = map;
}
return null;
}
else if (val instanceof Map)
{ // > compactSize
Map<K, V> map = (Map<K, V>) val;
return map.put(key, value);
}
else if (val == EMPTY_MAP)
{ // empty
if (compareKeys(key, getLogicalSingleKey()) && !(value instanceof Map || value instanceof Object[]))
{
val = value;
}
else
{
val = new CompactMapEntry(key, value);
}
return null;
}
// size == 1
if (compareKeys(key, getLogicalSingleKey()))
{ // Overwrite
V save = getLogicalSingleValue();
if (compareKeys(key, getSingleValueKey()) && !(value instanceof Map || value instanceof Object[]))
{
val = value;
}
else
{
val = new CompactMapEntry(key, value);
}
return save;
}
else
{ // CompactMapEntry to []
Object[] entries = new Object[4];
entries[0] = getLogicalSingleKey();
entries[1] = getLogicalSingleValue();
entries[2] = key;
entries[3] = value;
val = entries;
return null;
}
}
public V remove(Object key)
{
if (val instanceof Object[])
{ // 2 to compactSize
Object[] entries = (Object[]) val;
if (size() == 2)
{ // When at 2 entries, we must drop back to CompactMapEntry or val (use clear() and put() to get us there).
if (compareKeys(key, entries[0]))
{
Object prevValue = entries[1];
clear();
put((K)entries[2], (V)entries[3]);
return (V) prevValue;
}
else if (compareKeys(key, entries[2]))
{
Object prevValue = entries[3];
clear();
put((K)entries[0], (V)entries[1]);
return (V) prevValue;
}
}
else
{
final int len = entries.length;
for (int i = 0; i < len; i += 2)
{
Object aKey = entries[i];
if (compareKeys(key, aKey))
{ // Found, must shrink
Object prior = entries[i + 1];
Object[] shrink = new Object[len - 2];
System.arraycopy(entries, 0, shrink, 0, i);
System.arraycopy(entries, i + 2, shrink, i, shrink.length - i);
val = shrink;
return (V) prior;
}
}
}
return null; // not found
}
else if (val instanceof Map)
{ // > compactSize
Map<K, V> map = (Map<K, V>) val;
if (!map.containsKey(key))
{
return null;
}
V save = map.remove(key);
if (map.size() == compactSize())
{ // Down to compactSize, need to switch to Object[]
Object[] entries = new Object[compactSize() * 2];
Iterator<Entry<K, V>> i = map.entrySet().iterator();
int idx = 0;
while (i.hasNext())
{
Entry<K, V> entry = i.next();
entries[idx] = entry.getKey();
entries[idx + 1] = entry.getValue();
idx += 2;
}
val = entries;
}
return save;
}
else if (val == EMPTY_MAP)
{ // empty
return null;
}
// size == 1
if (compareKeys(key, getLogicalSingleKey()))
{ // found
V save = getLogicalSingleValue();
val = EMPTY_MAP;
return save;
}
else
{ // not found
return null;
}
}
public void putAll(Map<? extends K, ? extends V> map)
{
if (map == null)
{
return;
}
int mSize = map.size();
if (val instanceof Map || mSize > compactSize())
{
if (val == EMPTY_MAP)
{
val = getNewMap(mSize);
}
((Map<K, V>) val).putAll(map);
}
else
{
for (Entry<? extends K, ? extends V> entry : map.entrySet())
{
put(entry.getKey(), entry.getValue());
}
}
}
public void clear()
{
val = EMPTY_MAP;
}
public int hashCode()
{
if (val instanceof Object[])
{
int h = 0;
Object[] entries = (Object[]) val;
final int len = entries.length;
for (int i=0; i < len; i += 2)
{
Object aKey = entries[i];
Object aValue = entries[i + 1];
h += computeKeyHashCode(aKey) ^ computeValueHashCode(aValue);
}
return h;
}
else if (val instanceof Map)
{
return val.hashCode();
}
else if (val == EMPTY_MAP)
{
return 0;
}
// size == 1
return computeKeyHashCode(getLogicalSingleKey()) ^ computeValueHashCode(getLogicalSingleValue());
}
public boolean equals(Object obj)
{
if (this == obj) return true;
if (!(obj instanceof Map)) return false;
Map<?, ?> other = (Map<?, ?>) obj;
if (size() != other.size()) return false;
if (val instanceof Object[])
{ // 2 to compactSize
for (Entry<?, ?> entry : other.entrySet())
{
final Object thatKey = entry.getKey();
if (!containsKey(thatKey))
{
return false;
}
Object thatValue = entry.getValue();
Object thisValue = get(thatKey);
if (thatValue == null || thisValue == null)
{ // Perform null checks
if (thatValue != thisValue)
{
return false;
}
}
else if (!thisValue.equals(thatValue))
{
return false;
}
}
}
else if (val instanceof Map)
{ // > compactSize
Map<K, V> map = (Map<K, V>) val;
return map.equals(other);
}
else if (val == EMPTY_MAP)
{ // empty
return other.isEmpty();
}
// size == 1
return entrySet().equals(other.entrySet());
}
public String toString()
{
Iterator<Entry<K,V>> i = entrySet().iterator();
if (!i.hasNext())
{
return "{}";
}
StringBuilder sb = new StringBuilder();
sb.append('{');
for (;;)
{
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
sb.append(key == this ? "(this Map)" : key);
sb.append('=');
sb.append(value == this ? "(this Map)" : value);
if (!i.hasNext())
{
return sb.append('}').toString();
}
sb.append(',').append(' ');
}
}
public Set<K> keySet()
{
return new AbstractSet<K>()
{
public Iterator<K> iterator()
{
if (useCopyIterator())
{
return new CopyKeyIterator();
}
else
{
return new CompactKeyIterator();
}
}
public int size() { return CompactMap.this.size(); }
public void clear() { CompactMap.this.clear(); }
public boolean contains(Object o) { return CompactMap.this.containsKey(o); } // faster than inherited method
public boolean remove(Object o)
{
final int size = size();
CompactMap.this.remove(o);
return size() != size;
}
public boolean removeAll(Collection c)
{
int size = size();
for (Object o : c)
{
CompactMap.this.remove(o);
}
return size() != size;
}
public boolean retainAll(Collection c)
{
// Create fast-access O(1) to all elements within passed in Collection
Map<K, V> other = new CompactMap<K, V>()
{ // Match outer
protected boolean isCaseInsensitive() { return CompactMap.this.isCaseInsensitive(); }
protected int compactSize() { return CompactMap.this.compactSize(); }
protected Map<K, V> getNewMap() { return CompactMap.this.getNewMap(c.size()); }
};
for (Object o : c)
{
other.put((K)o, null);
}
final int size = size();
keySet().removeIf(key -> !other.containsKey(key));
return size() != size;
}
};
}
public Collection<V> values()
{
return new AbstractCollection<V>()
{
public Iterator<V> iterator()
{
if (useCopyIterator())
{
return new CopyValueIterator();
}
else
{
return new CompactValueIterator();
}
}
public int size() { return CompactMap.this.size(); }
public void clear() { CompactMap.this.clear(); }
};
}
public Set<Entry<K, V>> entrySet()
{
return new AbstractSet()
{
public Iterator<Entry<K, V>> iterator()
{
if (useCopyIterator())
{
return new CopyEntryIterator();
}
else
{
return new CompactEntryIterator();
}
}
public int size() { return CompactMap.this.size(); }
public void clear() { CompactMap.this.clear(); }
public boolean contains(Object o)
{ // faster than inherited method
if (o instanceof Entry)
{
Entry<K, V> entry = (Entry<K, V>)o;
K entryKey = entry.getKey();
Object value = CompactMap.this.get(entryKey);
if (value != null)
{ // Found non-null value with key, return true if values are equals()
return Objects.equals(value, entry.getValue());
}
else if (CompactMap.this.containsKey(entryKey))
{
value = CompactMap.this.get(entryKey);
return Objects.equals(value, entry.getValue());
}
}
return false;
}
public boolean remove(Object o)
{
if (!(o instanceof Entry)) { return false; }
final int size = size();
Entry<K, V> that = (Entry<K, V>) o;
CompactMap.this.remove(that.getKey());
return size() != size;
}
/**
* This method is required. JDK method is broken, as it relies
* on iterator solution. This method is fast because contains()
* and remove() are both hashed O(1) look ups.
*/
public boolean removeAll(Collection c)
{
final int size = size();
for (Object o : c)
{
remove(o);
}
return size() != size;
}
public boolean retainAll(Collection c)
{
// Create fast-access O(1) to all elements within passed in Collection
Map<K, V> other = new CompactMap<K, V>()
{ // Match outer
protected boolean isCaseInsensitive() { return CompactMap.this.isCaseInsensitive(); }
protected int compactSize() { return CompactMap.this.compactSize(); }
protected Map<K, V> getNewMap() { return CompactMap.this.getNewMap(c.size()); }
};
for (Object o : c)
{
if (o instanceof Entry)
{
other.put(((Entry<K, V>)o).getKey(), ((Entry<K, V>) o).getValue());
}
}
int origSize = size();
// Drop all items that are not in the passed in Collection
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext())
{
Entry<K, V> entry = i.next();
K key = entry.getKey();
V value = entry.getValue();
if (!other.containsKey(key))
{ // Key not even present, nuke the entry
i.remove();
}
else
{ // Key present, now check value match
Object v = other.get(key);
if (!Objects.equals(v, value))
{
i.remove();
}
}
}
return size() != origSize;
}
};
}
private Map<K, V> getCopy()
{
Map<K, V> copy = getNewMap(size()); // Use their Map (TreeMap, HashMap, LinkedHashMap, etc.)
if (val instanceof Object[])
{ // 2 to compactSize - copy Object[] into Map
Object[] entries = (Object[]) CompactMap.this.val;
final int len = entries.length;
for (int i=0; i < len; i += 2)
{
copy.put((K)entries[i], (V)entries[i + 1]);
}
}
else if (val instanceof Map)
{ // > compactSize - putAll to copy
copy.putAll((Map<K, V>)CompactMap.this.val);
}
else if (val == EMPTY_MAP)
{ // empty - nothing to copy
}
else
{ // size == 1
copy.put(getLogicalSingleKey(), getLogicalSingleValue());
}
return copy;
}
private void iteratorRemove(Entry<K, V> currentEntry, Iterator<Entry<K, V>> i)
{
if (currentEntry == null)
{ // remove() called on iterator prematurely
throw new IllegalStateException("remove() called on an Iterator before calling next()");
}
remove(currentEntry.getKey());
}
public Map<K, V> minus(Object removeMe)
{
throw new UnsupportedOperationException("Unsupported operation [minus] or [-] between Maps. Use removeAll() or retainAll() instead.");
}
public Map<K, V> plus(Object right)
{
throw new UnsupportedOperationException("Unsupported operation [plus] or [+] between Maps. Use putAll() instead.");
}
protected enum LogicalValueType
{
EMPTY, OBJECT, ENTRY, MAP, ARRAY
}
protected LogicalValueType getLogicalValueType()
{
if (val instanceof Object[])
{ // 2 to compactSize
return LogicalValueType.ARRAY;
}
else if (val instanceof Map)
{ // > compactSize
return LogicalValueType.MAP;
}
else if (val == EMPTY_MAP)
{ // empty
return LogicalValueType.EMPTY;
}
else
{ // size == 1
if (CompactMapEntry.class.isInstance(val))
{
return LogicalValueType.ENTRY;
}
else
{
return LogicalValueType.OBJECT;
}
}
}
/**
* Marker Class to hold key and value when the key is not the same as the getSingleValueKey().
* This method transmits the setValue() changes to the outer CompactMap instance.
*/
public class CompactMapEntry extends AbstractMap.SimpleEntry<K, V>
{
public CompactMapEntry(K key, V value)
{
super(key, value);
}
public V setValue(V value)
{
V save = this.getValue();
super.setValue(value);
CompactMap.this.put(getKey(), value); // "Transmit" (write-thru) to underlying Map.
return save;
}
public boolean equals(Object o)
{
if (!(o instanceof Map.Entry)) { return false; }
if (o == this) { return true; }
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return compareKeys(getKey(), e.getKey()) && Objects.equals(getValue(), e.getValue());
}
public int hashCode()
{
return computeKeyHashCode(getKey()) ^ computeValueHashCode(getValue());
}
}
protected int computeKeyHashCode(Object key)
{
if (key instanceof String)
{
if (isCaseInsensitive())
{
return StringUtilities.hashCodeIgnoreCase((String)key);
}
else
{ // k can't be null here (null is not instanceof String)
return key.hashCode();
}
}
else
{
int keyHash;
if (key == null)
{
return 0;
}
else
{
keyHash = key == CompactMap.this ? 37: key.hashCode();
}
return keyHash;
}
}
protected int computeValueHashCode(Object value)
{
if (value == CompactMap.this)
{
return 17;
}
else
{
return value == null ? 0 : value.hashCode();
}
}
private K getLogicalSingleKey()
{
if (CompactMapEntry.class.isInstance(val))
{
CompactMapEntry entry = (CompactMapEntry) val;
return entry.getKey();
}
return getSingleValueKey();
}
private V getLogicalSingleValue()
{
if (CompactMapEntry.class.isInstance(val))
{
CompactMapEntry entry = (CompactMapEntry) val;
return entry.getValue();
}
return (V) val;
}
/**
* @return String key name when there is only one entry in the Map.
*/
protected K getSingleValueKey() { return (K) "key"; };
/**
* @return new empty Map instance to use when size() becomes {@literal >} compactSize().
*/
protected Map<K, V> getNewMap() { return new HashMap<>(compactSize() + 1); }
protected Map<K, V> getNewMap(int size)
{
Map<K, V> map = getNewMap();
try
{
Constructor<?> constructor = ReflectionUtils.getConstructor(map.getClass(), Integer.TYPE);
return (Map<K, V>) constructor.newInstance(size);
}
catch (Exception e)
{
return map;
}
}
protected boolean isCaseInsensitive() { return false; }
protected int compactSize() { return 80; }
protected boolean useCopyIterator() {
Map<K, V> newMap = getNewMap();
if (newMap instanceof CaseInsensitiveMap) {
newMap = ((CaseInsensitiveMap<K, V>) newMap).getWrappedMap();
}
return newMap instanceof SortedMap;
}
/* ------------------------------------------------------------ */
// iterators
abstract class CompactIterator {
Iterator<Map.Entry<K,V>> mapIterator;
Object current; // Map.Entry if > compactsize, key <= compactsize
int expectedSize; // for fast-fail
int index; // current slot
CompactIterator() {
expectedSize = size();
current = EMPTY_MAP;
index = -1;
if (val instanceof Map) {
mapIterator = ((Map<K, V>)val).entrySet().iterator();
}
}
public final boolean hasNext() {
if (mapIterator!=null) {
return mapIterator.hasNext();
}
else {
return (index+1) < size();
}
}
final void advance() {
if (expectedSize != size()) {
throw new ConcurrentModificationException();
}
if (++index>=size()) {
throw new NoSuchElementException();
}
if (mapIterator!=null) {
current = mapIterator.next();
}
else if (expectedSize==1) {
current = getLogicalSingleKey();
}
else {
current = ((Object [])val)[index*2];
}
}
public final void remove() {
if (current==EMPTY_MAP) {
throw new IllegalStateException();
}
if (size() != expectedSize)
throw new ConcurrentModificationException();
int newSize = expectedSize-1;
// account for the change in size
if (mapIterator!=null && newSize==compactSize()) {
current = ((Map.Entry<K,V>)current).getKey();
mapIterator = null;
}
// perform the remove
if (mapIterator==null) {
CompactMap.this.remove(current);
} else {
mapIterator.remove();
}
index--;