|
79 | 79 | import java.util.Arrays;
|
80 | 80 | import java.util.Collection;
|
81 | 81 | import java.util.Collections;
|
| 82 | +import java.util.Comparator; |
82 | 83 | import java.util.Date;
|
83 | 84 | import java.util.EnumSet;
|
84 | 85 | import java.util.HashMap;
|
|
89 | 90 | import java.util.Locale;
|
90 | 91 | import java.util.Map;
|
91 | 92 | import java.util.Set;
|
| 93 | +import java.util.SortedMap; |
| 94 | +import java.util.TreeMap; |
92 | 95 | import java.util.concurrent.TimeUnit;
|
93 | 96 | import java.util.function.IntFunction;
|
94 | 97 |
|
@@ -641,12 +644,47 @@ public <K, V> Map<K, V> readMap(Writeable.Reader<K> keyReader, Writeable.Reader<
|
641 | 644 | return Collections.emptyMap();
|
642 | 645 | }
|
643 | 646 | Map<K, V> map = new HashMap<>(size);
|
| 647 | + readIntoMap(keyReader, valueReader, map, size); |
| 648 | + return map; |
| 649 | + } |
| 650 | + |
| 651 | + /** |
| 652 | + * Read a serialized map into a SortedMap using the default ordering for the keys. If the result is empty it might be immutable. |
| 653 | + */ |
| 654 | + public <K extends Comparable<K>, V> SortedMap<K, V> readOrderedMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader) |
| 655 | + throws IOException { |
| 656 | + return readOrderedMap(keyReader, valueReader, null); |
| 657 | + } |
| 658 | + |
| 659 | + /** |
| 660 | + * Read a serialized map into a SortedMap, specifying a Comparator for the keys. If the result is empty it might be immutable. |
| 661 | + */ |
| 662 | + public <K extends Comparable<K>, V> SortedMap<K, V> readOrderedMap( |
| 663 | + Writeable.Reader<K> keyReader, |
| 664 | + Writeable.Reader<V> valueReader, |
| 665 | + @Nullable Comparator<K> keyComparator |
| 666 | + ) throws IOException { |
| 667 | + int size = readArraySize(); |
| 668 | + if (size == 0) { |
| 669 | + return Collections.emptySortedMap(); |
| 670 | + } |
| 671 | + SortedMap<K, V> sortedMap; |
| 672 | + if (keyComparator == null) { |
| 673 | + sortedMap = new TreeMap<>(); |
| 674 | + } else { |
| 675 | + sortedMap = new TreeMap<>(keyComparator); |
| 676 | + } |
| 677 | + readIntoMap(keyReader, valueReader, sortedMap, size); |
| 678 | + return sortedMap; |
| 679 | + } |
| 680 | + |
| 681 | + private <K, V> void readIntoMap(Writeable.Reader<K> keyReader, Writeable.Reader<V> valueReader, Map<K, V> map, int size) |
| 682 | + throws IOException { |
644 | 683 | for (int i = 0; i < size; i++) {
|
645 | 684 | K key = keyReader.read(this);
|
646 | 685 | V value = valueReader.read(this);
|
647 | 686 | map.put(key, value);
|
648 | 687 | }
|
649 |
| - return map; |
650 | 688 | }
|
651 | 689 |
|
652 | 690 | /**
|
|
0 commit comments