rspec/rules/S4034/java/rule.adoc

32 lines
981 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
When using the ``++Stream++`` API, call chains should be simplified as much as possible. Not only does it make the code easier to read, it also avoid creating unnecessary temporary objects.
This rule raises an issue when one of the following substitution is possible:
||Original||Preferred||
|``++stream.filter(predicate).findFirst().isPresent()++``|``++stream.anyMatch(predicate)++``|
|``++stream.filter(predicate).findAny().isPresent()++``|``++stream.anyMatch(predicate)++``|
|``++!stream.anyMatch(predicate)++``|``++stream.noneMatch(predicate)++``|
|``++!stream.anyMatch(x -> !(...))++``|``++stream.allMatch(...)++``|
|``++stream.map(mapper).anyMatch(Boolean::booleanValue)++``|``++stream.anyMatch(predicate)++``|
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
boolean hasRed = widgets.stream().filter(w -> w.getColor() == RED).findFirst().isPresent(); // Noncompliant
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
boolean hasRed = widgets.stream().anyMatch(w -> w.getColor() == RED);
----