/** * Returns an immutable map containing the same entries as the provided sorted map, with the same * ordering. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws NullPointerException if any key or value in {@code map} is null */ @SuppressWarnings("unchecked") public static <K, V> ImmutableSortedMap<K, V> copyOfSorted(SortedMap<K, ? extends V> map) { Comparator<? super K> comparator = map.comparator(); if (comparator == null) { // If map has a null comparator, the keys should have a natural ordering, // even though K doesn't explicitly implement Comparable. comparator = (Comparator<? super K>) NATURAL_ORDER; } if (map instanceof ImmutableSortedMap) { // TODO(kevinb): Prove that this cast is safe, even though // Collections.unmodifiableSortedMap requires the same key type. @SuppressWarnings("unchecked") ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map; if (!kvMap.isPartialView()) { return kvMap; } } return fromEntries(comparator, true, map.entrySet()); }
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal( Map<? extends K, ? extends V> map, Comparator<? super K> comparator) { boolean sameComparator = false; if (map instanceof SortedMap) { SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map; Comparator<?> comparator2 = sortedMap.comparator(); sameComparator = (comparator2 == null) ? comparator == NATURAL_ORDER : comparator.equals(comparator2); } if (sameComparator && (map instanceof ImmutableSortedMap)) { // TODO(kevinb): Prove that this cast is safe, even though // Collections.unmodifiableSortedMap requires the same key type. @SuppressWarnings("unchecked") ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map; if (!kvMap.isPartialView()) { return kvMap; } } return fromEntries(comparator, sameComparator, map.entrySet()); }
public Comparator<? super byte[]> comparator() { return map.comparator(); }
public Comparator<? super K> comparator() { checkInit(); return delegate.comparator(); }
@Override public void doInitialize(SortedMap<K, V> sortedMap, boolean empty) { init(empty ? new TreeMap<K, V>(sortedMap.comparator()) : sortedMap, null, false); }
private <K, V> void verify( SortedMap<K, V> immutableMap, SortedMap<K, V> mutableMap, K key1, K key2, V value) { try { immutableMap.clear(); Assert.assertTrue(mutableMap.isEmpty()); } catch (UnsupportedOperationException e) { Assert.assertFalse(mutableMap.isEmpty()); } Assert.assertEquals(immutableMap.containsKey(key1), mutableMap.containsKey(key1)); Assert.assertEquals(immutableMap.containsKey(key2), mutableMap.containsKey(key2)); Assert.assertEquals(immutableMap.containsValue(value), mutableMap.containsValue(value)); Assert.assertEquals(immutableMap.containsValue(null), mutableMap.containsValue(null)); Assert.assertEquals(immutableMap.entrySet(), mutableMap.entrySet()); Assert.assertEquals(immutableMap.get(key1), mutableMap.get(key1)); Assert.assertEquals(immutableMap.get(key2), mutableMap.get(key2)); Assert.assertEquals(immutableMap.isEmpty(), mutableMap.isEmpty()); Assert.assertEquals(immutableMap.keySet(), mutableMap.keySet()); try { immutableMap.put(key1, value); Assert.fail(); } catch (UnsupportedOperationException e) { } try { immutableMap.putAll(java.util.Collections.singletonMap(key1, value)); Assert.fail(); } catch (UnsupportedOperationException e) { } try { immutableMap.remove(key1); } catch (UnsupportedOperationException e) { } Assert.assertEquals(immutableMap.size(), mutableMap.size()); // Is it OK that this fails? // Assert.assertEquals(immutableMap.values(), mutableMap.values()); Assert.assertEquals(immutableMap.values().size(), mutableMap.values().size()); if (!mutableMap.isEmpty()) { Assert.assertEquals( immutableMap.values().iterator().next(), mutableMap.values().iterator().next()); } Assert.assertSame(immutableMap.comparator(), mutableMap.comparator()); if (!mutableMap.isEmpty()) { Assert.assertEquals(immutableMap.firstKey(), mutableMap.firstKey()); Assert.assertEquals(immutableMap.lastKey(), mutableMap.lastKey()); } else { try { immutableMap.firstKey(); Assert.fail(); } catch (NoSuchElementException e) { } try { immutableMap.lastKey(); Assert.fail(); } catch (NoSuchElementException e) { } } Assert.assertEquals(immutableMap.headMap(key1), mutableMap.headMap(key1)); Assert.assertEquals(immutableMap.headMap(key2), mutableMap.headMap(key2)); Assert.assertEquals(immutableMap.subMap(key1, key2), mutableMap.subMap(key1, key2)); Assert.assertEquals(immutableMap.tailMap(key1), mutableMap.tailMap(key1)); Assert.assertEquals(immutableMap.tailMap(key2), mutableMap.tailMap(key2)); }
/** Switches row and column qualifier. Use same comparator as the given map. Returns TreeMap. */ public static <V> SortedMap<Key, V> transposeMap(SortedMap<Key, V> mapOrig) { SortedMap<Key, V> m = new TreeMap<>(mapOrig.comparator()); return transposeMapHelp(mapOrig, m); }