@SuppressWarnings("unchecked") SerializedForm(ImmutableSortedMap<?, ?> sortedMap) { super(sortedMap); comparator = (Comparator<Object>) sortedMap.comparator(); }
@SuppressWarnings("unchecked") SerializedForm(ImmutableSortedMap<?, ?> sortedMap) { super(sortedMap); comparator = (Comparator<Object>) sortedMap.comparator(); }
@SuppressWarnings("unchecked") SerializedForm(ImmutableSortedMap<?, ?> sortedMap) { super(sortedMap); comparator = (Comparator<Object>) sortedMap.comparator(); }
static void serialize(SerializationStreamWriter writer, ImmutableSortedMap<?, ?> instance) throws SerializationException { writer.writeObject(instance.comparator()); Map_CustomFieldSerializerBase.serialize(writer, instance); }
/** * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys ranges * from {@code fromKey} to {@code toKey}, inclusive or exclusive as indicated by the boolean * flags. * * <p>The {@link SortedMap#subMap} documentation states that a submap of a submap throws an {@link * IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code fromKey}. * However, this method doesn't throw an exception in that situation, but instead keeps the * original {@code fromKey}. Similarly, this method keeps the original {@code toKey}, instead of * throwing an exception, if passed a {@code toKey} greater than an earlier {@code toKey}. * * @since 12.0 */ @Override public ImmutableSortedMap<K, V> subMap( K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { checkNotNull(fromKey); checkNotNull(toKey); checkArgument( comparator().compare(fromKey, toKey) <= 0, "expected fromKey <= toKey but %s > %s", fromKey, toKey); return headMap(toKey, toInclusive).tailMap(fromKey, fromInclusive); }
/** * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys ranges * from {@code fromKey} to {@code toKey}, inclusive or exclusive as indicated by the boolean * flags. * * <p>The {@link SortedMap#subMap} documentation states that a submap of a submap throws an {@link * IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code fromKey}. * However, this method doesn't throw an exception in that situation, but instead keeps the * original {@code fromKey}. Similarly, this method keeps the original {@code toKey}, instead of * throwing an exception, if passed a {@code toKey} greater than an earlier {@code toKey}. * * @since 12.0 */ @Override public ImmutableSortedMap<K, V> subMap( K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { checkNotNull(fromKey); checkNotNull(toKey); checkArgument( comparator().compare(fromKey, toKey) <= 0, "expected fromKey <= toKey but %s > %s", fromKey, toKey); return headMap(toKey, toInclusive).tailMap(fromKey, fromInclusive); }
public void testCopyOfSortedNatural() { SortedMap<String, Integer> original = Maps.newTreeMap(); original.put("one", 1); original.put("two", 2); original.put("three", 3); ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOfSorted(original); assertMapEquals(copy, "one", 1, "three", 3, "two", 2); assertSame(copy, ImmutableSortedMap.copyOfSorted(copy)); assertSame(Ordering.natural(), copy.comparator()); }
private ImmutableSortedMap<K, V> getSubMap(int fromIndex, int toIndex) { if (fromIndex == 0 && toIndex == size()) { return this; } else if (fromIndex == toIndex) { return emptyMap(comparator()); } else { return new ImmutableSortedMap<>( keySet.getSubSet(fromIndex, toIndex), valueList.subList(fromIndex, toIndex)); } }
public void testCopyOf() { Map<String, Integer> original = new LinkedHashMap<>(); original.put("one", 1); original.put("two", 2); original.put("three", 3); ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOf(original); assertMapEquals(copy, "one", 1, "three", 3, "two", 2); assertSame(copy, ImmutableSortedMap.copyOf(copy)); assertSame(Ordering.natural(), copy.comparator()); }
@Override public ImmutableSortedMap<K, V> descendingMap() { // TODO(kevinb): the descendingMap is never actually cached at all. Either it should be or the // code below simplified. ImmutableSortedMap<K, V> result = descendingMap; if (result == null) { if (isEmpty()) { return result = emptyMap(Ordering.from(comparator()).reverse()); } else { return result = new ImmutableSortedMap<>( (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this); } } return result; }
public void testCopyOfExplicitComparator() { Comparator<String> comparator = Ordering.natural().reverse(); Map<String, Integer> original = new LinkedHashMap<>(); original.put("one", 1); original.put("two", 2); original.put("three", 3); ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOf(original, comparator); assertMapEquals(copy, "two", 2, "three", 3, "one", 1); assertSame(copy, ImmutableSortedMap.copyOf(copy, comparator)); assertSame(comparator, copy.comparator()); }
public void testCopyOfSortedExplicit() { Comparator<String> comparator = Ordering.natural().reverse(); SortedMap<String, Integer> original = Maps.newTreeMap(comparator); original.put("one", 1); original.put("two", 2); original.put("three", 3); ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOfSorted(original); assertMapEquals(copy, "two", 2, "three", 3, "one", 1); assertSame(copy, ImmutableSortedMap.copyOfSorted(copy)); assertSame(comparator, copy.comparator()); }
public void testCopyOfSingletonMap() { ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOf(Collections.singletonMap("one", 1)); assertMapEquals(copy, "one", 1); assertSame(copy, ImmutableSortedMap.copyOf(copy)); assertSame(Ordering.natural(), copy.comparator()); }
public void testCopyOfEmptyMap() { ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOf(Collections.<String, Integer>emptyMap()); assertEquals(Collections.<String, Integer>emptyMap(), copy); assertSame(copy, ImmutableSortedMap.copyOf(copy)); assertSame(Ordering.natural(), copy.comparator()); }
private ImmutableSortedMap<K, V> getSubMap(int fromIndex, int toIndex) { if (fromIndex == 0 && toIndex == size()) { return this; } else if (fromIndex == toIndex) { return emptyMap(comparator()); } else { return new ImmutableSortedMap<>( keySet.getSubSet(fromIndex, toIndex), valueList.subList(fromIndex, toIndex)); } }
@Override public ImmutableSortedMap<K, V> descendingMap() { // TODO(kevinb): the descendingMap is never actually cached at all. Either it should be or the // code below simplified. ImmutableSortedMap<K, V> result = descendingMap; if (result == null) { if (isEmpty()) { return result = emptyMap(Ordering.from(comparator()).reverse()); } else { return result = new ImmutableSortedMap<>( (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this); } } return result; }
public void testCopyOfImmutableSortedSetDifferentComparator() { Comparator<String> comparator = Ordering.natural().reverse(); Map<String, Integer> original = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3); ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOf(original, comparator); assertMapEquals(copy, "two", 2, "three", 3, "one", 1); assertSame(copy, ImmutableSortedMap.copyOf(copy, comparator)); assertSame(comparator, copy.comparator()); }
public void testBuilderReverseOrder() { ImmutableSortedMap<String, Integer> map = ImmutableSortedMap.<String, Integer>reverseOrder() .put("one", 1) .put("two", 2) .put("three", 3) .put("four", 4) .put("five", 5) .build(); assertMapEquals(map, "two", 2, "three", 3, "one", 1, "four", 4, "five", 5); assertEquals(Ordering.natural().reverse(), map.comparator()); }
public void testBuilderComparator() { Comparator<String> comparator = Ordering.natural().reverse(); ImmutableSortedMap<String, Integer> map = new ImmutableSortedMap.Builder<String, Integer>(comparator) .put("one", 1) .put("two", 2) .put("three", 3) .put("four", 4) .put("five", 5) .build(); assertMapEquals(map, "two", 2, "three", 3, "one", 1, "four", 4, "five", 5); assertSame(comparator, map.comparator()); }
@SuppressWarnings("unchecked") SerializedForm(ImmutableSortedMap<?, ?> sortedMap) { super(sortedMap); comparator = (Comparator<Object>) sortedMap.comparator(); } @Override Object readResolve() {