-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathCompactMap.java
2876 lines (2660 loc) · 112 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 javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
/**
* A memory-efficient {@code Map} implementation that adapts its internal storage structure
* to minimize memory usage while maintaining excellent performance.
*
* <h2>Creating a CompactMap</h2>
* There are two primary ways to create a CompactMap:
*
* <h3>1. Using the Builder Pattern (Recommended)</h3>
* <pre>{@code
* // Create a case-insensitive, sorted CompactMap
* CompactMap<String, Object> map = CompactMap.<String, Object>builder()
* .caseSensitive(false)
* .sortedOrder()
* .compactSize(80)
* .build();
*
* // Create a CompactMap with insertion ordering
* CompactMap<String, Object> ordered = CompactMap.<String, Object>builder()
* .insertionOrder()
* .mapType(LinkedHashMap.class)
* .build();
* }</pre>
*
* <h3>Type Inference and Builder Usage</h3>
* Note the type witness ({@code <String, Object>}) in the example above. When using the builder pattern
* with method chaining, you may need to provide a type witness to help Java's type inference:
*
* <pre>{@code
* // Alternative approach without type witness
* Builder<String, Object> builder = CompactMap.builder();
* CompactMap<String, Object> map2 = builder
* .caseSensitive(false)
* .sortedOrder()
* .build();
* }</pre>
*
* The type witness ({@code <String, Object>}) is required due to Java's type inference
* limitations when method chaining directly from the builder() method. If you find the
* type witness syntax cumbersome, you can split the builder creation and configuration
* into separate statements as shown in the second example above.
*
* <h3>2. Using Constructor</h3>
* <pre>{@code
* // Creates a default CompactMap that scales based on size
* CompactMap<String, Object> map = new CompactMap<>();
*
* // Creates a CompactMap initialized with entries from another map
* CompactMap<String, Object> copy = new CompactMap<>(existingMap);
* }</pre>
*
* In the examples above, the behavior of the CompactMap will be that of a HashMap,
* while using the minimal amount of memory possible to hold the contents. The CompactMap
* has only one instance variable.
*
* <h2>Configuration Options</h2>
* When using the Builder pattern, the following options are available:
* <table border="1" cellpadding="5" summary="Builder Options">
* <tr><th>Method</th><th>Description</th><th>Default</th></tr>
* <tr>
* <td>{@code caseSensitive(boolean)}</td>
* <td>Controls case sensitivity for string keys</td>
* <td>true</td>
* </tr>
* <tr>
* <td>{@code compactSize(int)}</td>
* <td>Maximum size before switching to backing map</td>
* <td>70</td>
* </tr>
* <tr>
* <td>{@code mapType(Class)}</td>
* <td>Type of backing map when size exceeds compact size</td>
* <td>HashMap.class</td>
* </tr>
* <tr>
* <td>{@code singleValueKey(K)}</td>
* <td>Special key that enables optimized storage when map contains only one entry with this key</td>
* <td>"id"</td>
* </tr>
* <tr>
* <td>{@code sourceMap(Map)}</td>
* <td>Initializes the CompactMap with entries from the provided map</td>
* <td>null</td>
* </tr>
* <tr>
* <td>{@code sortedOrder()}</td>
* <td>Maintains keys in sorted order</td>
* <td>unordered</td>
* </tr>
* <tr>
* <td>{@code reverseOrder()}</td>
* <td>Maintains keys in reverse order</td>
* <td>unordered</td>
* </tr>
* <tr>
* <td>{@code insertionOrder()}</td>
* <td>Maintains keys in insertion order</td>
* <td>unordered</td>
* </tr>
* </table>
*
* <h3>Example with Additional Properties</h3>
* <pre>{@code
* CompactMap<String, Object> map = CompactMap.builder()
* .caseSensitive(false)
* .sortedOrder()
* .compactSize(80)
* .singleValueKey("uuid") // Optimize storage for single entry with key "uuid"
* .sourceMap(existingMap) // Initialize with existing entries
* .build();
* }</pre>
*
* <h2>Internal Storage States</h2>
* As elements are added to or removed from the map, it transitions through different internal states
* to optimize memory usage:
*
* <table border="1" cellpadding="5" summary="Internal States">
* <tr>
* <th>State</th>
* <th>Condition</th>
* <th>Storage</th>
* <th>Size Range</th>
* </tr>
* <tr>
* <td>Empty</td>
* <td>{@code val == EMPTY_MAP}</td>
* <td>Sentinel value</td>
* <td>0</td>
* </tr>
* <tr>
* <td>Single Entry</td>
* <td>Direct value or Entry</td>
* <td>Optimized single value storage</td>
* <td>1</td>
* </tr>
* <tr>
* <td>Compact Array</td>
* <td>{@code val} is Object[]</td>
* <td>Array with alternating keys/values</td>
* <td>2 to compactSize</td>
* </tr>
* <tr>
* <td>Backing Map</td>
* <td>{@code val} is Map</td>
* <td>Standard Map implementation</td>
* <td>> compactSize</td>
* </tr>
* </table>
*
* <h2>Implementation Note</h2>
* <p>This class uses runtime optimization techniques to create specialized implementations
* based on the configuration options. When a CompactMap is first created with a specific
* combination of options (case sensitivity, ordering, map type, etc.), a custom class
* is dynamically generated and cached to provide optimal performance for that configuration.
* This is an implementation detail that is transparent to users of the class.</p>
*
* <p>The generated class names encode the configuration settings. For example:</p>
* <ul>
* <li>{@code CompactMap$HashMap_CS_S70_id_Unord} - A case-sensitive, unordered map
* with HashMap backing, compact size of 70, and "id" as single value key</li>
* <li>{@code CompactMap$TreeMap_CI_S100_UUID_Sort} - A case-insensitive, sorted map
* with TreeMap backing, compact size of 100, and "UUID" as single value key</li>
* <li>{@code CompactMap$LinkedHashMap_CS_S50_Key_Ins} - A case-sensitive map with
* insertion ordering, LinkedHashMap backing, compact size of 50, and "Key" as
* single value key</li>
* </ul>
*
* <p>For developers interested in the internal mechanics, the source code contains
* detailed documentation of the template generation and compilation process.</p>
* <p>Note: As elements are removed, the map will transition back through these states
* in reverse order to maintain optimal memory usage.</p>
*
* <p>While subclassing CompactMap is still supported for backward compatibility,
* it is recommended to use the Builder pattern for new implementations.</p>
*
* @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 = "_︿_ψ_☼";
// Constants for option keys
public static final String COMPACT_SIZE = "compactSize";
public static final String CASE_SENSITIVE = "caseSensitive";
public static final String MAP_TYPE = "mapType";
public static final String SINGLE_KEY = "singleKey";
public static final String SOURCE_MAP = "source";
public static final String ORDERING = "ordering";
// Constants for ordering options
public static final String UNORDERED = "unordered";
public static final String SORTED = "sorted";
public static final String INSERTION = "insertion";
public static final String REVERSE = "reverse";
// Default values
private static final int DEFAULT_COMPACT_SIZE = 70;
private static final boolean DEFAULT_CASE_SENSITIVE = true;
private static final Class<? extends Map> DEFAULT_MAP_TYPE = HashMap.class;
private static final String DEFAULT_SINGLE_KEY = "id";
private static final String INNER_MAP_TYPE = "innerMapType";
private static final TemplateClassLoader templateClassLoader = new TemplateClassLoader(ClassUtilities.getClassLoader(CompactMap.class));
private static final Map<String, ReentrantLock> CLASS_LOCKS = new ConcurrentHashMap<>();
// The only "state" and why this is a compactMap - one member variable
protected Object val = EMPTY_MAP;
/**
* Constructs an empty CompactMap with the default configuration.
* <p>
* This constructor ensures that the `compactSize()` method returns a value greater than or equal to 2.
* </p>
*
* @throws IllegalStateException if {@link #compactSize()} returns a value less than 2
*/
public CompactMap() {
if (compactSize() < 2) {
throw new IllegalArgumentException("compactSize() must be >= 2");
}
// Only check direct subclasses, not our generated classes
if (getClass() != CompactMap.class && isLegacyConstructed()) {
Map<K,V> map = getNewMap();
if (map instanceof SortedMap) {
SortedMap<?,?> sortedMap = (SortedMap<?,?>)map;
Comparator<?> comparator = sortedMap.comparator();
// Check case sensitivity consistency
if (comparator == String.CASE_INSENSITIVE_ORDER && !isCaseInsensitive()) {
throw new IllegalStateException(
"Inconsistent configuration: Map uses case-insensitive comparison but isCaseInsensitive() returns false");
}
}
}
}
/**
* Constructs a CompactMap initialized with the entries from the provided map.
* <p>
* The entries are copied from the provided map, and the internal representation
* is determined based on the number of entries and the {@link #compactSize()} threshold.
* </p>
*
* @param other the map whose entries are to be placed in this map
* @throws NullPointerException if {@code other} is null
*/
public CompactMap(Map<K, V> other) {
this();
putAll(other);
}
/**
* Returns the number of key-value mappings in this map.
* <p>
* If the map contains more than {@link Integer#MAX_VALUE} elements, returns {@link Integer#MAX_VALUE}.
* </p>
*
* @return the number of key-value mappings in this map
*/
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;
}
/**
* @return {@code true} if this map contains no key-value mappings; {@code false} otherwise
*/
public boolean isEmpty() {
return val == EMPTY_MAP;
}
/**
* Determines whether two keys are equal, considering case sensitivity for String keys.
*
* @param key the first key to compare
* @param aKey the second key to compare
* @return {@code true} if the keys are equal based on the comparison rules; {@code false} otherwise
*/
private boolean areKeysEqual(Object key, Object aKey) {
if (key instanceof String && aKey instanceof String) {
return isCaseInsensitive()
? ((String) key).equalsIgnoreCase((String) aKey)
: key.equals(aKey);
}
return Objects.equals(key, aKey);
}
/**
* Determines if this CompactMap instance was created using legacy construction (direct subclassing)
* rather than the template-based generation system.
* <p>
* Legacy construction refers to instances where CompactMap is directly subclassed by user code,
* rather than using the builder pattern or template generation system. This method helps
* differentiate between these two creation patterns to maintain backward compatibility.
* </p>
* <p>
* The method works by checking if the class name starts with the template prefix
* "com.cedarsoftware.util.CompactMap$". Template-generated classes will always have this
* prefix, while legacy subclasses will not.
* </p>
*
* @return {@code true} if this instance was created through legacy subclassing,
* {@code false} if it was created through the template generation system
*/
private boolean isLegacyConstructed() {
return !getClass().getName().startsWith("com.cedarsoftware.util.CompactMap$");
}
/**
* Returns {@code true} if this map contains a mapping for the specified key.
*
* @param key the key whose presence in this map is to be tested
* @return {@code true} if this map contains a mapping for the specified key; {@code false} otherwise
*/
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 (areKeysEqual(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 areKeysEqual(key, getLogicalSingleKey());
}
/**
* Returns {@code true} if this map maps one or more keys to the specified value.
*
* @param value the value whose presence in this map is to be tested
* @return {@code true} if this map maps one or more keys to the specified value;
* {@code false} otherwise
*/
public boolean containsValue(Object value) {
if (val instanceof Object[]) { // 2 to CompactSize
Object[] entries = (Object[]) val;
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 Objects.equals(getLogicalSingleValue(), value);
}
/**
* Returns the value to which the specified key is mapped, or {@code null} if this map contains no mapping for the key.
* <p>
* A return value of {@code null} does not necessarily indicate that the map contains no mapping for the key; it is also
* possible that the map explicitly maps the key to {@code null}.
* </p>
*
* @param key the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or {@code null} if this map contains no mapping for the key
*/
public V get(Object key) {
if (val instanceof Object[]) { // 2 to compactSize
Object[] entries = (Object[]) val;
int len = entries.length;
for (int i = 0; i < len; i += 2) {
if (areKeysEqual(key, entries[i])) {
return (V) entries[i + 1];
}
}
return null;
} else if (val instanceof Map) { // > compactSize
return ((Map<K, V>) val).get(key);
} else if (val == EMPTY_MAP) { // empty
return null;
}
// size == 1
if (areKeysEqual(key, getLogicalSingleKey())) {
return getLogicalSingleValue();
}
return null;
}
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with key, or {@code null} if there was no mapping for key.
* @throws NullPointerException if the specified key is null and this map does not permit null keys
* @throws ClassCastException if the key is of an inappropriate type for this map
*/
@Override
public V put(K key, V value) {
if (val instanceof Object[]) { // Compact array storage (2 to compactSize)
return putInCompactArray((Object[]) val, key, value);
} else if (val instanceof Map) { // Backing map storage (> compactSize)
return ((Map<K, V>) val).put(key, value);
} else if (val == EMPTY_MAP) { // Empty map
if (areKeysEqual(key, getSingleValueKey()) && !(value instanceof Map || value instanceof Object[])) {
// Store the value directly for optimized single-entry storage
// (can't allow Map or Object[] because that would throw off the 'state')
val = value;
} else {
// Create a CompactMapEntry for the first entry
val = new CompactMapEntry(key, value);
}
return null;
}
// Single entry state, handle overwrite, or insertion which transitions the Map to Object[4]
return handleSingleEntryPut(key, value);
}
/**
* Removes the mapping for the specified key from this map if present.
*/
@Override
public V remove(Object key) {
if (val instanceof Object[]) { // 2 to compactSize
return removeFromCompactArray(key);
} else if (val instanceof Map) { // > compactSize
Map<K, V> map = (Map<K, V>) val;
return removeFromMap(map, key);
} else if (val == EMPTY_MAP) { // empty
return null;
}
// size == 1
return handleSingleEntryRemove(key);
}
/**
* Adds or updates an entry in the compact array storage.
* <p>
* If the key exists, updates its value. If the key is new and there's room to stay as an array (< compactSize),
* appends the new entry by growing the Object[]. If adding would exceed compactSize(), transitions to map storage.
* </p>
*
* @param entries the current array storage containing alternating keys and values
* @param key the key to add or update
* @param value the value to associate with the key
* @return the previous value associated with the key, or null if the key was not present
*/
private V putInCompactArray(final Object[] entries, K key, V value) {
final int len = entries.length;
// Check for "update" case
for (int i = 0; i < len; i += 2) {
if (areKeysEqual(key, entries[i])) {
int i1 = i + 1;
V oldValue = (V) entries[i1];
entries[i1] = value;
return oldValue;
}
}
// New entry
if (size() < compactSize()) {
Object[] expand = new Object[len + 2];
System.arraycopy(entries, 0, expand, 0, len);
expand[len] = key;
expand[len + 1] = value;
val = expand; // Simply append, no sorting needed
} else {
switchToMap(entries, key, value);
}
return null;
}
/**
* Removes an entry from the compact array storage.
* <p>
* If size will become 1 after removal, transitions back to single entry storage.
* Otherwise, creates a new smaller array excluding the removed entry.
* </p>
*
* @param key the key whose entry should be removed
* @return the value associated with the key, or null if the key was not found
*/
private V removeFromCompactArray(Object key) {
Object[] entries = (Object[]) val;
int pairCount = size(); // Number of key-value pairs
if (pairCount == 2) { // Transition back to single entry
return handleTransitionToSingleEntry(entries, key);
}
int len = entries.length;
for (int i = 0; i < len; i += 2) {
if (areKeysEqual(key, entries[i])) {
V oldValue = (V) entries[i + 1];
Object[] shrink = new Object[len - 2];
// Copy entries before the found pair
if (i > 0) {
System.arraycopy(entries, 0, shrink, 0, i);
}
// Copy entries after the found pair
if (i + 2 < len) {
System.arraycopy(entries, i + 2, shrink, i, len - i - 2);
}
// Update the backing array without sorting
val = shrink;
return oldValue;
}
}
return null; // Key not found
}
/**
* Sorts the compact array while maintaining key-value pair relationships.
* <p>
* For legacy constructed maps, sorts only if backing map is a SortedMap.
* For template maps, sorts based on the specified ordering (sorted/reverse).
* Keys at even indices, values at odd indices are kept together during sort.
* </p>
*
* @param array the array of alternating keys and values to sort
*/
private void sortCompactArray(final Object[] array) {
int pairCount = array.length / 2;
if (pairCount <= 1) {
return;
}
if (isLegacyConstructed()) {
Map<K,V> mapInstance = getNewMap(); // Called only once before iteration
// Only sort if it's a SortedMap
if (mapInstance instanceof SortedMap) {
SortedMap<K,V> sortedMap = (SortedMap<K,V>)mapInstance;
boolean reverse = sortedMap.comparator() != null &&
sortedMap.comparator().getClass().getName().toLowerCase().contains("reversecomp");
Comparator<Object> comparator = new CompactMapComparator(isCaseInsensitive(), reverse);
quickSort(array, 0, pairCount - 1, comparator);
}
return;
}
// Non-legacy mode logic
String ordering = getOrdering();
if (ordering.equals(UNORDERED) || ordering.equals(INSERTION)) {
return;
}
Comparator<Object> comparator = new CompactMapComparator(isCaseInsensitive(),
REVERSE.equals(ordering));
quickSort(array, 0, pairCount - 1, comparator);
}
/**
* Implements QuickSort for the compact array, maintaining key-value pair relationships.
* <p>
* Indices represent pair positions (i.e., lowPair=1 refers to array indices 2,3).
* Uses recursion to sort subarrays around pivot points.
* </p>
*
* @param array the array of alternating keys and values to sort
* @param lowPair starting pair index of the subarray
* @param highPair ending pair index of the subarray
* @param comparator the comparator to use for key comparison
*/
private void quickSort(Object[] array, int lowPair, int highPair, Comparator<Object> comparator) {
if (lowPair < highPair) {
int pivotPair = partition(array, lowPair, highPair, comparator);
quickSort(array, lowPair, pivotPair - 1, comparator);
quickSort(array, pivotPair + 1, highPair, comparator);
}
}
/**
* Partitions array segment around a pivot while maintaining key-value pairs.
* <p>
* Uses median-of-three pivot selection and adjusts indices to handle paired elements.
* All comparisons are performed on keys (even indices) only.
* </p>
*
* @param array the array of alternating keys and values to partition
* @param lowPair starting pair index of the partition segment
* @param highPair ending pair index of the partition segment
* @param comparator the comparator to use for key comparison
* @return the final position (pair index) of the pivot
*/
private int partition(Object[] array, int lowPair, int highPair, Comparator<Object> comparator) {
int low = lowPair * 2;
int high = highPair * 2;
int mid = low + ((high - low) / 4) * 2;
Object pivot = selectPivot(array, low, mid, high, comparator);
int i = low - 2;
for (int j = low; j < high; j += 2) {
if (comparator.compare(array[j], pivot) <= 0) {
i += 2;
swapPairs(array, i, j);
}
}
i += 2;
swapPairs(array, i, high);
return i / 2;
}
/**
* Selects and positions the median-of-three pivot for partitioning.
* <p>
* Compares first, middle, and last elements to find the median value.
* Moves the selected pivot to the high position while maintaining pair relationships.
* </p>
*
* @param array the array of alternating keys and values
* @param low index of the first key in the segment
* @param mid index of the middle key in the segment
* @param high index of the last key in the segment
* @param comparator the comparator to use for key comparison
* @return the selected pivot value
*/
private Object selectPivot(Object[] array, int low, int mid, int high,
Comparator<Object> comparator) {
Object first = array[low];
Object middle = array[mid];
Object last = array[high];
if (comparator.compare(first, middle) <= 0) {
if (comparator.compare(middle, last) <= 0) {
swapPairs(array, mid, high); // median is middle
return middle;
} else if (comparator.compare(first, last) <= 0) {
// median is last, already in position
return last;
} else {
swapPairs(array, low, high); // median is first
return first;
}
} else {
if (comparator.compare(first, last) <= 0) {
swapPairs(array, low, high); // median is first
return first;
} else if (comparator.compare(middle, last) <= 0) {
swapPairs(array, mid, high); // median is middle
return middle;
} else {
// median is last, already in position
return last;
}
}
}
/**
* Swaps two key-value pairs in the array.
* <p>
* Exchanges elements at indices i,i+1 with j,j+1, maintaining
* the relationship between keys and their values.
* </p>
*
* @param array the array of alternating keys and values
* @param i the index of the first key to swap
* @param j the index of the second key to swap
*/
private void swapPairs(Object[] array, int i, int j) {
Object tempKey = array[i];
Object tempValue = array[i + 1];
array[i] = array[j];
array[i + 1] = array[j + 1];
array[j] = tempKey;
array[j + 1] = tempValue;
}
/**
* Transitions storage from compact array to backing map implementation.
* <p>
* Creates new map instance, copies existing entries from array,
* adds the new key-value pair, and updates internal storage reference.
* Called when size would exceed compactSize.
* </p>
*
* @param entries the current array of alternating keys and values
* @param key the new key triggering the transition
* @param value the value associated with the new key
*/
private void switchToMap(Object[] entries, K key, V value) {
// Get the correct map type with initial capacity
Map<K, V> map = getNewMap(); // This respects subclass overrides
// Copy existing entries preserving order
int len = entries.length;
for (int i = 0; i < len; i += 2) {
map.put((K) entries[i], (V) entries[i + 1]);
}
map.put(key, value);
val = map;
}
/**
* Transitions from two entries to single entry storage when removing a key.
* <p>
* If the specified key matches either entry, removes it and retains the other entry,
* transitioning back to single entry storage mode.
* </p>
*
* @param entries array containing exactly two key-value pairs
* @param key the key to remove
* @return the previous value associated with the removed key, or null if key not found
*/
private V handleTransitionToSingleEntry(Object[] entries, Object key) {
if (areKeysEqual(key, entries[0])) {
Object prevValue = entries[1];
clear();
put((K) entries[2], (V) entries[3]);
return (V) prevValue;
} else if (areKeysEqual(key, entries[2])) {
Object prevValue = entries[3];
clear();
put((K) entries[0], (V) entries[1]);
return (V) prevValue;
}
return null;
}
/**
* Handles put operation when map contains exactly one entry.
* <p>
* If key matches existing entry, updates value. Otherwise, transitions
* to array storage with both the existing and new entries.
* Optimizes storage when key matches singleValueKey.
* </p>
*
* @param key the key to add or update
* @param value the value to associate with the key
* @return the previous value if key existed, null otherwise
*/
private V handleSingleEntryPut(K key, V value) {
if (areKeysEqual(key, getLogicalSingleKey())) { // Overwrite
V save = getLogicalSingleValue();
if (areKeysEqual(key, getSingleValueKey()) && !(value instanceof Map || value instanceof Object[])) {
val = value;
} else {
val = new CompactMapEntry(key, value);
}
return save;
} else { // Transition to Object[]
Object[] entries = new Object[4];
K existingKey = getLogicalSingleKey();
V existingValue = getLogicalSingleValue();
// Simply append the entries in order: existing entry first, new entry second
entries[0] = existingKey;
entries[1] = existingValue;
entries[2] = key;
entries[3] = value;
val = entries;
return null;
}
}
/**
* Handles remove operation when map contains exactly one entry.
* <p>
* If key matches the single entry, removes it and transitions to empty state.
* Otherwise, returns null as key was not found.
* </p>
*
* @param key the key to remove
* @return the value associated with the removed key, or null if key not found
*/
private V handleSingleEntryRemove(Object key) {
if (areKeysEqual(key, getLogicalSingleKey())) { // Found
V save = getLogicalSingleValue();
clear();
return save;
}
return null; // Not found
}
/**
* Removes entry from map storage and handles transition to array if needed.
* <p>
* If size after removal equals compactSize, transitions back to array storage.
* Otherwise, maintains map storage with entry removed.
* </p>
*
* @param map the current map storage
* @param key the key to remove
* @return the value associated with the removed key, or null if key not found
*/
private V removeFromMap(Map<K, V> map, Object key) {
if (!map.containsKey(key)) {
return null;
}
V save = map.remove(key);
if (map.size() == compactSize()) { // Transition back to Object[]
Object[] entries = new Object[compactSize() * 2];
int idx = 0;
for (Entry<K, V> entry : map.entrySet()) {
entries[idx] = entry.getKey();
entries[idx + 1] = entry.getValue();
idx += 2;
}
val = entries;
}
return save;
}
/**
* Copies all mappings from the specified map into this map.
* <p>
* If resulting size would exceed compactSize, transitions directly to map storage.
* Otherwise, adds entries individually, allowing natural transitions to occur.
* </p>
*
* @param map mappings to be stored in this map
* @throws NullPointerException if the specified map is null
*/
public void putAll(Map<? extends K, ? extends V> map) {
if (map == null || map.isEmpty()) {
return;
}
for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
/**
* Removes all mappings from this map.
* <p>
* Resets internal storage to empty state, allowing garbage collection
* of any existing storage structures.
* </p>
*/
public void clear() {
val = EMPTY_MAP;
}
/**
* Returns the hash code value for this map.
* <p>
* The hash code of a map is defined as the sum of the hash codes of each entry in the map's entry set.
* This implementation ensures consistency with the `equals` method.
* </p>
*
* @return the hash code value for this 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());
}
/**
* Compares the specified object with this map for equality.
* <p>
* Returns {@code true} if the given object is also a map and the two maps represent the same mappings.
* More formally, two maps {@code m1} and {@code m2} are equal if:
* </p>
* <pre>{@code
* m1.entrySet().equals(m2.entrySet())
* }</pre>
*
* @param obj the object to be compared for equality with this map
* @return {@code true} if the specified object is equal to this map
*/
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());
}
/**
* Returns a string representation of this map.
* <p>
* The string representation consists of a list of key-value mappings in the order returned by the map's
* {@code entrySet} iterator, enclosed in braces ({@code "{}"}). Adjacent mappings are separated by the characters