static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap( Comparator<? super K> comparator, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) { checkNotNull(comparator); checkNotNull(keyFunction); checkNotNull(valueFunction); /* * We will always fail if there are duplicate keys, and the keys are always sorted by * the Comparator, so the entries can come in in arbitrary order -- so we report UNORDERED. */ return Collector.of( () -> new ImmutableSortedMap.Builder<K, V>(comparator), (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)), ImmutableSortedMap.Builder::combine, ImmutableSortedMap.Builder::build, Collector.Characteristics.UNORDERED); }
/** * Returns a builder that creates immutable sorted maps whose keys are ordered by their natural * ordering. The sorted maps use {@link Ordering#natural()} as the comparator. */ public static <K extends Comparable<?>, V> Builder<K, V> naturalOrder() { return new Builder<>(Ordering.natural()); }
static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap( Comparator<? super K> comparator, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) { checkNotNull(comparator); checkNotNull(keyFunction); checkNotNull(valueFunction); /* * We will always fail if there are duplicate keys, and the keys are always sorted by * the Comparator, so the entries can come in in arbitrary order -- so we report UNORDERED. */ return Collector.of( () -> new ImmutableSortedMap.Builder<K, V>(comparator), (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)), ImmutableSortedMap.Builder::combine, ImmutableSortedMap.Builder::build, Collector.Characteristics.UNORDERED); }
@Override Object readResolve() { Builder<Object, Object> builder = new Builder<Object, Object>(comparator); return createMap(builder); } private static final long serialVersionUID = 0;
/** * Returns an immutable sorted map containing the given entries, sorted by the * natural ordering of their keys. * * @throws IllegalArgumentException if the two keys are equal according to * their natural ordering */ public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(K k1, V v1, K k2, V v2) { return new Builder<K, V>(Ordering.natural()) .put(k1, v1).put(k2, v2).build(); }
@Override Object readResolve() { Builder<Object, Object> builder = new Builder<Object, Object>(comparator); return createMap(builder); } private static final long serialVersionUID = 0;
/** * Returns a builder that creates immutable sorted maps with an explicit * comparator. If the comparator has a more general type than the map's keys, * such as creating a {@code SortedMap<Integer, String>} with a {@code * Comparator<Number>}, use the {@link Builder} constructor instead. * * @throws NullPointerException if {@code comparator} is null */ public static <K, V> Builder<K, V> orderedBy(Comparator<K> comparator) { return new Builder<K, V>(comparator); }
/** * Returns an immutable sorted map containing the given entries, sorted by the * natural ordering of their keys. * * @throws IllegalArgumentException if any two keys are equal according to * their natural ordering */ public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return new Builder<K, V>(Ordering.natural()) .put(k1, v1).put(k2, v2).put(k3, v3).put(k4, v4).build(); }
/** * Returns an immutable sorted map containing the given entries, sorted by the * natural ordering of their keys. * * @throws IllegalArgumentException if any two keys are equal according to * their natural ordering */ public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) { return new Builder<K, V>(Ordering.natural()) .put(k1, v1).put(k2, v2).put(k3, v3).build(); }
private <T> ImmutableSortedMap<String, T> resourceBundleToMap(Locale locale, @NonNls String baseName, Function<String, T> integerResolver) { Collator caseInsensitive = Collator.getInstance(locale); caseInsensitive.setStrength(Collator.PRIMARY); ImmutableSortedMap.Builder<String, T> nameToModeBuilder = new ImmutableSortedMap.Builder<String, T>(caseInsensitive); ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale); for (String key : bundle.keySet()) { String outKey; String outValue; if (reversedMap) { outKey = bundle.getString(key); outValue = key; } else { outKey = key; outValue = bundle.getString(key); } nameToModeBuilder.put(outKey, (T) keyResolver.apply(outValue)); } return nameToModeBuilder.build(); }
static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap( Comparator<? super K> comparator, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) { checkNotNull(comparator); checkNotNull(keyFunction); checkNotNull(valueFunction); /* * We will always fail if there are duplicate keys, and the keys are always sorted by * the Comparator, so the entries can come in in arbitrary order -- so we report UNORDERED. */ return Collector.of( () -> new ImmutableSortedMap.Builder<K, V>(comparator), (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)), ImmutableSortedMap.Builder::combine, ImmutableSortedMap.Builder::build, Collector.Characteristics.UNORDERED); }
@Override Object readResolve() { Builder<Object, Object> builder = new Builder<>(comparator); return createMap(builder); }
/** * Returns a builder that creates immutable sorted maps whose keys are ordered by the reverse of * their natural ordering. */ public static <K extends Comparable<?>, V> Builder<K, V> reverseOrder() { return new Builder<>(Ordering.natural().reverse()); }
/** * Returns a builder that creates immutable sorted maps with an explicit comparator. If the * comparator has a more general type than the map's keys, such as creating a {@code * SortedMap<Integer, String>} with a {@code Comparator<Number>}, use the {@link Builder} * constructor instead. * * @throws NullPointerException if {@code comparator} is null */ public static <K, V> Builder<K, V> orderedBy(Comparator<K> comparator) { return new Builder<>(comparator); }
/** * Returns a builder that creates immutable sorted maps whose keys are ordered by their natural * ordering. The sorted maps use {@link Ordering#natural()} as the comparator. */ public static <K extends Comparable<?>, V> Builder<K, V> naturalOrder() { return new Builder<>(Ordering.natural()); }
@Override Object readResolve() { Builder<Object, Object> builder = new Builder<>(comparator); return createMap(builder); }
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()); }
@Override Object readResolve() { Builder<Object, Object> builder = new Builder<>(comparator); return createMap(builder); }
/** * Returns a builder that creates immutable sorted maps with an explicit comparator. If the * comparator has a more general type than the map's keys, such as creating a {@code * SortedMap<Integer, String>} with a {@code Comparator<Number>}, use the {@link Builder} * constructor instead. * * @throws NullPointerException if {@code comparator} is null */ public static <K, V> Builder<K, V> orderedBy(Comparator<K> comparator) { return new Builder<>(comparator); }
/** * Returns a builder that creates immutable sorted maps whose keys are ordered by the reverse of * their natural ordering. */ public static <K extends Comparable<?>, V> Builder<K, V> reverseOrder() { return new Builder<>(Ordering.natural().reverse()); }