43 lines
1.1 KiB
Plaintext
43 lines
1.1 KiB
Plaintext
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
|
|
|
|
|
|
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
|
|
|
|
|
|
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++8++``.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
myCollection.stream().map(new Mapper<String,String>() {
|
|
public String map(String input) {
|
|
return new StringBuilder(input).reverse().toString();
|
|
}
|
|
});
|
|
|
|
Predicate<String> isEmpty = new Predicate<String> {
|
|
boolean test(String myString) {
|
|
return myString.isEmpty();
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString());
|
|
|
|
Predicate<String> isEmpty = myString -> myString.isEmpty();
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|