rspec/rules/S4034/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

52 lines
1.3 KiB
Plaintext

== Why is this an issue?
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:
[frame=all]
[cols="^1,^1"]
|===
|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)++``
|===
=== Noncompliant code example
[source,java]
----
boolean hasRed = widgets.stream().filter(w -> w.getColor() == RED).findFirst().isPresent(); // Noncompliant
----
=== Compliant solution
[source,java]
----
boolean hasRed = widgets.stream().anyMatch(w -> w.getColor() == RED);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace this "xxx" chain with "yyy"
=== Highlighting
The chain to be replaced
endif::env-github,rspecator-view[]