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

CollectionRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

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

Table of contents
  1. CollectionIsEmpty
  2. CollectionSize
  3. CollectionAddAllToCollectionExpression
  4. CollectionAddAllToCollectionBlock
  5. CollectionRemoveAllFromCollectionExpression
  6. SetRemoveAllCollection
  7. NewArrayListFromCollection
  8. ImmutableCollectionStream
  9. ImmutableCollectionAsList
  10. ImmutableCollectionContains
  11. ImmutableCollectionParallelStream
  12. ImmutableCollectionToString
  13. CollectionToArray
  14. ImmutableCollectionToArrayWithArray
  15. ImmutableCollectionToArrayWithGenerator
  16. ImmutableCollectionIterator
  17. OptionalFirstCollectionElement
  18. OptionalFirstQueueElement
  19. RemoveOptionalFirstNavigableSetElement
  20. RemoveOptionalFirstQueueElement
  21. CollectionForEach

CollectionIsEmpty

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Boolean> testCollectionIsEmpty() {
     return ImmutableSet.of(
-        ImmutableSet.of(1).size() == 0,
-        ImmutableSet.of(2).size() <= 0,
-        ImmutableSet.of(3).size() < 1,
-        ImmutableSet.of(4).size() != 0,
-        ImmutableSet.of(5).size() > 0,
-        ImmutableSet.of(6).size() >= 1,
-        Iterables.isEmpty(ImmutableSet.of(7)),
-        ImmutableSet.of(8).asList().isEmpty());
+        ImmutableSet.of(1).isEmpty(),
+        ImmutableSet.of(2).isEmpty(),
+        ImmutableSet.of(3).isEmpty(),
+        !ImmutableSet.of(4).isEmpty(),
+        !ImmutableSet.of(5).isEmpty(),
+        !ImmutableSet.of(6).isEmpty(),
+        ImmutableSet.of(7).isEmpty(),
+        ImmutableSet.of(8).isEmpty());
   }

CollectionSize

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Integer> testCollectionSize() {
-    return ImmutableSet.of(Iterables.size(ImmutableSet.of(1)), ImmutableSet.of(2).asList().size());
+    return ImmutableSet.of(ImmutableSet.of(1).size(), ImmutableSet.of(2).size());
   }

CollectionAddAllToCollectionExpression

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 boolean testCollectionAddAllToCollectionExpression() {
-    return Iterables.addAll(new ArrayList<>(), ImmutableSet.of("foo"));
+    return new ArrayList<>().addAll(ImmutableSet.of("foo"));
   }

CollectionAddAllToCollectionBlock

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 void testCollectionAddAllToCollectionBlock() {
-    ImmutableSet.of("foo").forEach(new ArrayList<>()::add);
-    for (Number element : ImmutableSet.of(1)) {
-      new ArrayList<Number>().add(element);
-    }
-    for (Integer element : ImmutableSet.of(2)) {
-      new ArrayList<Number>().add(element);
-    }
+    new ArrayList<>().addAll(ImmutableSet.of("foo"));
+    new ArrayList<Number>().addAll(ImmutableSet.of(1));
+    new ArrayList<Number>().addAll(ImmutableSet.of(2));
   }

CollectionRemoveAllFromCollectionExpression

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 boolean testCollectionRemoveAllFromCollectionExpression() {
-    return Iterables.removeAll(new ArrayList<>(), ImmutableSet.of("foo"));
+    return new ArrayList<>().removeAll(ImmutableSet.of("foo"));
   }

SetRemoveAllCollection

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 void testSetRemoveAllCollection() {
-    ImmutableSet.of("foo").forEach(new HashSet<>()::remove);
-    for (Number element : ImmutableList.of(1)) {
-      new HashSet<Number>().remove(element);
-    }
-    for (Integer element : ImmutableSet.of(2)) {
-      new HashSet<Number>().remove(element);
-    }
+    new HashSet<>().removeAll(ImmutableSet.of("foo"));
+    new HashSet<Number>().removeAll(ImmutableList.of(1));
+    new HashSet<Number>().removeAll(ImmutableSet.of(2));
   }

NewArrayListFromCollection

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ArrayList<String> testNewArrayListFromCollection() {
-    return Lists.newArrayList(ImmutableList.of("foo"));
+    return new ArrayList<>(ImmutableList.of("foo"));
   }

ImmutableCollectionStream

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 Stream<Integer> testImmutableCollectionStream() {
-    return ImmutableSet.of(1).asList().stream();
+    return ImmutableSet.of(1).stream();
   }

ImmutableCollectionAsList

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableList<Integer> testImmutableCollectionAsList() {
-    return ImmutableList.copyOf(ImmutableSet.of(1));
+    return ImmutableSet.of(1).asList();
   }

ImmutableCollectionContains

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 boolean testImmutableCollectionContains() {
-    return ImmutableSet.of(1).asList().contains("foo");
+    return ImmutableSet.of(1).contains("foo");
   }

ImmutableCollectionParallelStream

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 Stream<Integer> testImmutableCollectionParallelStream() {
-    return ImmutableSet.of(1).asList().parallelStream();
+    return ImmutableSet.of(1).parallelStream();
   }

ImmutableCollectionToString

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 String testImmutableCollectionToString() {
-    return ImmutableSet.of(1).asList().toString();
+    return ImmutableSet.of(1).toString();
   }

CollectionToArray

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Object[]> testCollectionToArray() {
     return ImmutableSet.of(
-        ImmutableSet.of(1).toArray(new Object[1]),
-        ImmutableSet.of(2).toArray(Object[]::new),
-        ImmutableSet.of(3).asList().toArray());
+        ImmutableSet.of(1).toArray(), ImmutableSet.of(2).toArray(), ImmutableSet.of(3).toArray());
   }

ImmutableCollectionToArrayWithArray

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 Integer[] testImmutableCollectionToArrayWithArray() {
-    return ImmutableSet.of(1).asList().toArray(new Integer[0]);
+    return ImmutableSet.of(1).toArray(new Integer[0]);
   }

ImmutableCollectionToArrayWithGenerator

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 Integer[] testImmutableCollectionToArrayWithGenerator() {
-    return ImmutableSet.of(1).asList().toArray(Integer[]::new);
+    return ImmutableSet.of(1).toArray(Integer[]::new);
   }

ImmutableCollectionIterator

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 Iterator<Integer> testImmutableCollectionIterator() {
-    return ImmutableSet.of(1).asList().iterator();
+    return ImmutableSet.of(1).iterator();
   }

OptionalFirstCollectionElement

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Optional<Integer>> testOptionalFirstCollectionElement() {
     return ImmutableSet.of(
-        ImmutableSet.of(0).stream().findAny(),
-        ImmutableSet.of(1).isEmpty()
-            ? Optional.empty()
-            : Optional.of(ImmutableSet.of(1).iterator().next()),
-        ImmutableList.of(2).isEmpty() ? Optional.empty() : Optional.of(ImmutableList.of(2).get(0)),
-        ImmutableSortedSet.of(3).isEmpty()
-            ? Optional.empty()
-            : Optional.of(ImmutableSortedSet.of(3).first()),
-        !ImmutableSet.of(1).isEmpty()
-            ? Optional.of(ImmutableSet.of(1).iterator().next())
-            : Optional.empty(),
-        !ImmutableList.of(2).isEmpty() ? Optional.of(ImmutableList.of(2).get(0)) : Optional.empty(),
-        !ImmutableSortedSet.of(3).isEmpty()
-            ? Optional.of(ImmutableSortedSet.of(3).first())
-            : Optional.empty());
+        ImmutableSet.of(0).stream().findFirst(),
+        ImmutableSet.of(1).stream().findFirst(),
+        ImmutableList.of(2).stream().findFirst(),
+        ImmutableSortedSet.of(3).stream().findFirst(),
+        ImmutableSet.of(1).stream().findFirst(),
+        ImmutableList.of(2).stream().findFirst(),
+        ImmutableSortedSet.of(3).stream().findFirst());
   }

OptionalFirstQueueElement

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Optional<String>> testOptionalFirstQueueElement() {
     return ImmutableSet.of(
-        new LinkedList<String>().stream().findFirst(),
-        new LinkedList<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.of(new LinkedList<String>().peek()),
-        new LinkedList<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.ofNullable(new LinkedList<String>().peek()),
-        !new LinkedList<String>().isEmpty()
-            ? Optional.of(new LinkedList<String>().peek())
-            : Optional.empty(),
-        !new LinkedList<String>().isEmpty()
-            ? Optional.ofNullable(new LinkedList<String>().peek())
-            : Optional.empty());
+        Optional.ofNullable(new LinkedList<String>().peek()),
+        Optional.ofNullable(new LinkedList<String>().peek()),
+        Optional.ofNullable(new LinkedList<String>().peek()),
+        Optional.ofNullable(new LinkedList<String>().peek()),
+        Optional.ofNullable(new LinkedList<String>().peek()));
   }

RemoveOptionalFirstNavigableSetElement

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Optional<String>> testRemoveOptionalFirstNavigableSetElement() {
     return ImmutableSet.of(
-        new TreeSet<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.of(new TreeSet<String>().pollFirst()),
-        new TreeSet<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.ofNullable(new TreeSet<String>().pollFirst()),
-        !new TreeSet<String>().isEmpty()
-            ? Optional.of(new TreeSet<String>().pollFirst())
-            : Optional.empty(),
-        !new TreeSet<String>().isEmpty()
-            ? Optional.ofNullable(new TreeSet<String>().pollFirst())
-            : Optional.empty());
+        Optional.ofNullable(new TreeSet<String>().pollFirst()),
+        Optional.ofNullable(new TreeSet<String>().pollFirst()),
+        Optional.ofNullable(new TreeSet<String>().pollFirst()),
+        Optional.ofNullable(new TreeSet<String>().pollFirst()));
   }

RemoveOptionalFirstQueueElement

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<Optional<String>> testRemoveOptionalFirstQueueElement() {
     return ImmutableSet.of(
-        new LinkedList<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.of(new LinkedList<String>().poll()),
-        new LinkedList<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.of(new LinkedList<String>().remove()),
-        new LinkedList<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.ofNullable(new LinkedList<String>().poll()),
-        new LinkedList<String>().isEmpty()
-            ? Optional.empty()
-            : Optional.ofNullable(new LinkedList<String>().remove()),
-        !new LinkedList<String>().isEmpty()
-            ? Optional.of(new LinkedList<String>().poll())
-            : Optional.empty(),
-        !new LinkedList<String>().isEmpty()
-            ? Optional.of(new LinkedList<String>().remove())
-            : Optional.empty(),
-        !new LinkedList<String>().isEmpty()
-            ? Optional.ofNullable(new LinkedList<String>().poll())
-            : Optional.empty(),
-        !new LinkedList<String>().isEmpty()
-            ? Optional.ofNullable(new LinkedList<String>().remove())
-            : Optional.empty());
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()),
+        Optional.ofNullable(new LinkedList<String>().poll()));
   }

CollectionForEach

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 void testCollectionForEach() {
-    ImmutableSet.of(1).stream().forEach(String::valueOf);
+    ImmutableSet.of(1).forEach(String::valueOf);
   }

Copyright © 2017-2023 Picnic Technologies BV