ImmutableMapRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

Disable all rules by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$).* as compiler argument.

Table of contents
  1. ImmutableMapBuilder
  2. ImmutableMapBuilderBuildOrThrow
  3. EntryToImmutableMap
  4. IterableToImmutableMap
  5. EntryIterableToImmutableMap
  6. StreamOfMapEntriesToImmutableMap
  7. IndexIterableToImmutableMap
  8. TransformMapValuesToImmutableMap
  9. ImmutableMapOf
  10. ImmutableMapOf1
  11. ImmutableMapOf2
  12. ImmutableMapOf3
  13. ImmutableMapOf4
  14. ImmutableMapOf5
  15. ImmutableMapCopyOfMapsFilterKeys
  16. ImmutableMapCopyOfMapsFilterValues
  17. ImmutableMapOfEntries

ImmutableMapBuilder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapBuilder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapBuilder).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableMap.Builder<String, Integer> testImmutableMapBuilder() {
-  return new ImmutableMap.Builder<>();
+  return ImmutableMap.builder();
 }

ImmutableMapBuilderBuildOrThrow

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapBuilderBuildOrThrow") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapBuilderBuildOrThrow).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableMap<Object, Object> testImmutableMapBuilderBuildOrThrow() {
-  return ImmutableMap.builder().build();
+  return ImmutableMap.builder().buildOrThrow();
 }

EntryToImmutableMap

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("EntryToImmutableMap") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$EntryToImmutableMap).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<ImmutableMap<String, Integer>> testEntryToImmutableMap() {
   return ImmutableSet.of(
-      ImmutableMap.<String, Integer>builder().put(Map.entry("foo", 1)).buildOrThrow(),
-      Stream.of(Map.entry("foo", 1))
-          .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)));
+      ImmutableMap.of(Map.entry("foo", 1).getKey(), Map.entry("foo", 1).getValue()),
+      ImmutableMap.of(Map.entry("foo", 1).getKey(), Map.entry("foo", 1).getValue()));
 }

IterableToImmutableMap

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("IterableToImmutableMap") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$IterableToImmutableMap).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<ImmutableMap<Integer, Integer>> testIterableToImmutableMap() {
   return ImmutableSet.of(
-      ImmutableList.of(1).stream().collect(toImmutableMap(identity(), n -> n * 2)),
-      ImmutableList.of(2).stream().collect(toImmutableMap(k -> k, n -> n * 2)),
+      Maps.toMap(ImmutableList.of(1), n -> n * 2),
+      Maps.toMap(ImmutableList.of(2), n -> n * 2),
       ImmutableList.of(3).stream().collect(toImmutableMap(k -> 0, n -> n * 2)),
-      Streams.stream(ImmutableList.of(4)::iterator)
-          .collect(toImmutableMap(identity(), Integer::valueOf)),
-      Streams.stream(ImmutableList.of(5)::iterator)
-          .collect(toImmutableMap(k -> k, Integer::valueOf)),
+      Maps.toMap(ImmutableList.of(4)::iterator, Integer::valueOf),
+      Maps.toMap(ImmutableList.of(5)::iterator, Integer::valueOf),
       Streams.stream(ImmutableList.of(6)::iterator)
           .collect(toImmutableMap(k -> 0, Integer::valueOf)),
-      Streams.stream(ImmutableList.of(7).iterator())
-          .collect(toImmutableMap(identity(), n -> n.intValue())),
-      Streams.stream(ImmutableList.of(8).iterator())
-          .collect(toImmutableMap(k -> k, n -> n.intValue())),
+      Maps.toMap(ImmutableList.of(7).iterator(), n -> n.intValue()),
+      Maps.toMap(ImmutableList.of(8).iterator(), n -> n.intValue()),
       Streams.stream(ImmutableList.of(9).iterator())
           .collect(toImmutableMap(k -> 0, n -> n.intValue())),
-      ImmutableMap.copyOf(Maps.asMap(ImmutableSet.of(10), Integer::valueOf)));
+      Maps.toMap(ImmutableSet.of(10), Integer::valueOf));
 }

EntryIterableToImmutableMap

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("EntryIterableToImmutableMap") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$EntryIterableToImmutableMap).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, Integer>> testEntryIterableToImmutableMap() {
   return ImmutableSet.of(
+      ImmutableMap.copyOf(ImmutableMap.of("foo", 1)),
+      ImmutableMap.copyOf(ImmutableMap.of("foo", 1)),
+      ImmutableMap.copyOf(ImmutableMap.of("foo", 1)),
       ImmutableMap.copyOf(ImmutableMap.of("foo", 1).entrySet()),
-      ImmutableMap.<String, Integer>builder().putAll(ImmutableMap.of("foo", 1)).buildOrThrow(),
-      Map.copyOf(ImmutableMap.of("foo", 1)),
-      ImmutableMap.<String, Integer>builder()
-          .putAll(ImmutableMap.of("foo", 1).entrySet())
-          .buildOrThrow(),
-      ImmutableMap.of("foo", 1).entrySet().stream()
-          .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)),
-      Streams.stream(Iterables.cycle(Map.entry("foo", 1)))
-          .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)));
+      ImmutableMap.copyOf(ImmutableMap.of("foo", 1).entrySet()),
+      ImmutableMap.copyOf(Iterables.cycle(Map.entry("foo", 1))));
 }

StreamOfMapEntriesToImmutableMap

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StreamOfMapEntriesToImmutableMap") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$StreamOfMapEntriesToImmutableMap).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableMap<Integer, String> testStreamOfMapEntriesToImmutableMap() {
-  return Stream.of(1, 2, 3)
-      .map(n -> Map.entry(n, n.toString()))
-      .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
+  return Stream.of(1, 2, 3).collect(toImmutableMap(n -> n, n -> n.toString()));
 }

IndexIterableToImmutableMap

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("IndexIterableToImmutableMap") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$IndexIterableToImmutableMap).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<ImmutableMap<Integer, Integer>> testIndexIterableToImmutableMap() {
   return ImmutableSet.of(
-      ImmutableList.of(1).stream().collect(toImmutableMap(n -> n * 2, identity())),
-      ImmutableList.of(2).stream().collect(toImmutableMap(n -> n * 2, v -> v)),
+      Maps.uniqueIndex(ImmutableList.of(1), n -> n * 2),
+      Maps.uniqueIndex(ImmutableList.of(2), n -> n * 2),
       ImmutableList.of(3).stream().collect(toImmutableMap(n -> n * 2, v -> 0)),
-      Streams.stream(ImmutableList.of(4)::iterator)
-          .collect(toImmutableMap(Integer::valueOf, identity())),
-      Streams.stream(ImmutableList.of(5)::iterator)
-          .collect(toImmutableMap(Integer::valueOf, v -> v)),
+      Maps.uniqueIndex(ImmutableList.of(4)::iterator, Integer::valueOf),
+      Maps.uniqueIndex(ImmutableList.of(5)::iterator, Integer::valueOf),
       Streams.stream(ImmutableList.of(6)::iterator)
           .collect(toImmutableMap(Integer::valueOf, v -> 0)),
-      Streams.stream(ImmutableList.of(7).iterator())
-          .collect(toImmutableMap(n -> n.intValue(), identity())),
-      Streams.stream(ImmutableList.of(8).iterator())
-          .collect(toImmutableMap(n -> n.intValue(), v -> v)),
+      Maps.uniqueIndex(ImmutableList.of(7).iterator(), n -> n.intValue()),
+      Maps.uniqueIndex(ImmutableList.of(8).iterator(), n -> n.intValue()),
       Streams.stream(ImmutableList.of(9).iterator())
           .collect(toImmutableMap(n -> n.intValue(), v -> 0)));
 }

TransformMapValuesToImmutableMap

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("TransformMapValuesToImmutableMap") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$TransformMapValuesToImmutableMap).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<ImmutableMap<String, Integer>> testTransformMapValuesToImmutableMap() {
   return ImmutableSet.of(
-      ImmutableMap.of("foo", 1L).entrySet().stream()
-          .collect(toImmutableMap(Map.Entry::getKey, e -> Math.toIntExact(e.getValue()))),
-      Maps.toMap(
-          ImmutableMap.of("bar", 2L).keySet(),
-          k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k))));
+      ImmutableMap.copyOf(
+          Maps.transformValues(ImmutableMap.of("foo", 1L), v -> Math.toIntExact(v))),
+      ImmutableMap.copyOf(
+          Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v))));
 }

ImmutableMapOf

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOf") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOf).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, String>> testImmutableMapOf() {
   return ImmutableSet.of(
-      ImmutableMap.<String, String>builder().buildOrThrow(),
-      ImmutableMap.ofEntries(),
-      Collections.<String, String>emptyMap(),
-      Map.<String, String>of());
+      ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of());
 }

ImmutableMapOf1

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOf1") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOf1).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, String>> testImmutableMapOf1() {
   return ImmutableSet.of(
-      ImmutableMap.<String, String>builder().put("k1", "v1").buildOrThrow(),
-      ImmutableMap.ofEntries(Map.entry("k1", "v1")),
-      Collections.singletonMap("k1", "v1"),
-      Map.of("k1", "v1"));
+      ImmutableMap.of("k1", "v1"),
+      ImmutableMap.of("k1", "v1"),
+      ImmutableMap.of("k1", "v1"),
+      ImmutableMap.of("k1", "v1"));
 }

ImmutableMapOf2

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOf2") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOf2).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, String>> testImmutableMapOf2() {
   return ImmutableSet.of(
-      ImmutableMap.ofEntries(Map.entry("k1", "v1"), Map.entry("k2", "v2")),
-      Map.of("k1", "v1", "k2", "v2"));
+      ImmutableMap.of("k1", "v1", "k2", "v2"), ImmutableMap.of("k1", "v1", "k2", "v2"));
 }

ImmutableMapOf3

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOf3") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOf3).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, String>> testImmutableMapOf3() {
   return ImmutableSet.of(
-      ImmutableMap.ofEntries(Map.entry("k1", "v1"), Map.entry("k2", "v2"), Map.entry("k3", "v3")),
-      Map.of("k1", "v1", "k2", "v2", "k3", "v3"));
+      ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
+      ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"));
 }

ImmutableMapOf4

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOf4") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOf4).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, String>> testImmutableMapOf4() {
   return ImmutableSet.of(
-      ImmutableMap.ofEntries(
-          Map.entry("k1", "v1"),
-          Map.entry("k2", "v2"),
-          Map.entry("k3", "v3"),
-          Map.entry("k4", "v4")),
-      Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"));
+      ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"),
+      ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"));
 }

ImmutableMapOf5

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOf5") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOf5).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, String>> testImmutableMapOf5() {
   return ImmutableSet.of(
-      ImmutableMap.ofEntries(
-          Map.entry("k1", "v1"),
-          Map.entry("k2", "v2"),
-          Map.entry("k3", "v3"),
-          Map.entry("k4", "v4"),
-          Map.entry("k5", "v5")),
-      Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"));
+      ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"),
+      ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"));
 }

ImmutableMapCopyOfMapsFilterKeys

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapCopyOfMapsFilterKeys") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapCopyOfMapsFilterKeys).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableMap<String, Integer> testImmutableMapCopyOfMapsFilterKeys() {
-  return ImmutableMap.of("foo", 1).entrySet().stream()
-      .filter(entry -> entry.getKey().length() > 1)
-      .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
+  return ImmutableMap.copyOf(Maps.filterKeys(ImmutableMap.of("foo", 1), k -> k.length() > 1));
 }

ImmutableMapCopyOfMapsFilterValues

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapCopyOfMapsFilterValues") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapCopyOfMapsFilterValues).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableMap<String, Integer> testImmutableMapCopyOfMapsFilterValues() {
-  return ImmutableMap.of("foo", 1).entrySet().stream()
-      .filter(entry -> entry.getValue() > 0)
-      .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
+  return ImmutableMap.copyOf(Maps.filterValues(ImmutableMap.of("foo", 1), v -> v > 0));
 }

ImmutableMapOfEntries

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ImmutableMapOfEntries") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!ImmutableMapRules\$ImmutableMapOfEntries).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Map<String, Integer>> testImmutableMapOfEntries() {
   return ImmutableSet.of(
-      Map.ofEntries(),
-      Map.ofEntries(Map.entry("foo", 1)),
-      Map.ofEntries(Map.entry("bar", 2), Map.entry("baz", 3)));
+      ImmutableMap.ofEntries(),
+      ImmutableMap.ofEntries(Map.entry("foo", 1)),
+      ImmutableMap.ofEntries(Map.entry("bar", 2), Map.entry("baz", 3)));
 }

Copyright © 2017-2024 Picnic Technologies BV