CollectionRules
SUGGESTION
Simplification
Suppression
Disable all rules by adding
-XepOpt:Refaster:NamePattern=^(?!CollectionRules\$).*
as compiler argument.
Table of contents
- CollectionIsEmpty
- CollectionSize
- CollectionContains
- CollectionAddAllToCollectionExpression
- CollectionAddAllToCollectionBlock
- CollectionRemoveAllFromCollectionExpression
- CollectionRemoveAllFromCollectionBlock
- SetStream
- NewArrayListFromCollection
- ImmutableCollectionStream
- ImmutableCollectionAsList
- ImmutableCollectionContains
- ImmutableCollectionParallelStream
- ImmutableCollectionToString
- ArraysAsList
- CollectionToArray
- ImmutableCollectionToArrayWithArray
- ImmutableCollectionToArrayWithGenerator
- CollectionIterator
- OptionalFirstCollectionElement
- OptionalFirstQueueElement
- RemoveOptionalFirstNavigableSetElement
- RemoveOptionalFirstQueueElement
- 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).stream().findAny().isEmpty(),
- ImmutableSet.of(9).stream().findFirst().isEmpty(),
- ImmutableSet.of(10).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(),
+ ImmutableSet.of(9).isEmpty(),
+ ImmutableSet.of(10).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());
}
CollectionContains
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("CollectionContains")
to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!CollectionRules\$CollectionContains).*
as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
boolean testCollectionContains() {
- return ImmutableSet.of("foo").stream().anyMatch("bar"::equals);
+ return ImmutableSet.of("foo").contains("bar");
}
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"));
}
CollectionRemoveAllFromCollectionBlock
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("CollectionRemoveAllFromCollectionBlock")
to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!CollectionRules\$CollectionRemoveAllFromCollectionBlock).*
as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
void testCollectionRemoveAllFromCollectionBlock() {
- 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));
}
SetStream
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("SetStream")
to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!CollectionRules\$SetStream).*
as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Stream<Integer> testSetStream() {
- return ImmutableSet.of(1).stream().distinct();
+ return ImmutableSet.of(1).stream();
}
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();
}
ArraysAsList
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("ArraysAsList")
to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!CollectionRules\$ArraysAsList).*
as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
List<String> testArraysAsList() {
- return Arrays.stream(new String[0]).toList();
+ return Arrays.asList(new String[0]);
}
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);
}
CollectionIterator
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("CollectionIterator")
to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!CollectionRules\$CollectionIterator).*
as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Iterator<Integer>> testCollectionIterator() {
- return ImmutableSet.of(
- ImmutableSet.of(1).stream().iterator(), ImmutableSet.of(2).asList().iterator());
+ return ImmutableSet.of(ImmutableSet.of(1).iterator(), ImmutableSet.of(2).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);
}