Skip to main content Link Search Menu Expand Document (external link)

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. EntryToImmutableMap
  3. IterableToImmutableMap
  4. EntryIterableToImmutableMap
  5. StreamOfMapEntriesToImmutableMap
  6. IndexIterableToImmutableMap
  7. TransformMapValuesToImmutableMap
  8. ImmutableMapOf
  9. ImmutableMapOf1
  10. ImmutableMapOf2
  11. ImmutableMapOf3
  12. ImmutableMapOf4
  13. ImmutableMapOf5

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();
   }

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)).build(),
-        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)),
-        Streams.stream(ImmutableList.of(2)::iterator)
-            .collect(toImmutableMap(n -> n, Integer::valueOf)),
-        Streams.stream(ImmutableList.of(3).iterator())
-            .collect(toImmutableMap(identity(), n -> n.intValue())),
-        ImmutableMap.copyOf(Maps.asMap(ImmutableSet.of(4), Integer::valueOf)));
+        Maps.toMap(ImmutableList.of(1), n -> n * 2),
+        Maps.toMap(ImmutableList.of(2)::iterator, Integer::valueOf),
+        Maps.toMap(ImmutableList.of(3).iterator(), n -> n.intValue()),
+        Maps.toMap(ImmutableSet.of(4), 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<ImmutableMap<String, Integer>> testEntryIterableToImmutableMap() {
     return ImmutableSet.of(
+        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)).build(),
-        ImmutableMap.<String, Integer>builder()
-            .putAll(ImmutableMap.of("foo", 1).entrySet())
-            .build(),
-        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())),
-        Streams.stream(ImmutableList.of(2)::iterator)
-            .collect(toImmutableMap(Integer::valueOf, n -> n)),
-        Streams.stream(ImmutableList.of(3).iterator())
-            .collect(toImmutableMap(n -> n.intValue(), identity())));
+        Maps.uniqueIndex(ImmutableList.of(1), n -> n * 2),
+        Maps.uniqueIndex(ImmutableList.of(2)::iterator, Integer::valueOf),
+        Maps.uniqueIndex(ImmutableList.of(3).iterator(), n -> n.intValue()));
   }

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().build(),
-        Collections.<String, String>emptyMap(),
-        Map.<String, String>of());
+    return ImmutableSet.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").build(),
-        Collections.singletonMap("k1", "v1"),
-        Map.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.

 Map<String, String> testImmutableMapOf2() {
-    return Map.of("k1", "v1", "k2", "v2");
+    return 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.

 Map<String, String> testImmutableMapOf3() {
-    return Map.of("k1", "v1", "k2", "v2", "k3", "v3");
+    return 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.

 Map<String, String> testImmutableMapOf4() {
-    return Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4");
+    return 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.

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

Copyright © 2017-2023 Picnic Technologies BV