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
  14. ImmutableMapCopyOfMapsFilterKeys
  15. ImmutableMapCopyOfMapsFilterValues

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

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

Copyright © 2017-2024 Picnic Technologies BV