I have got this Java Interface with only one method.
// Java Interface
public interface AuditorAware {
Auditor getCurrentAuditor();
}
I was using Java 8 Lambda expression to create insance of AuditorAware
as following.
// Java 8 Lambda to create instance of AuditorAware
public AuditorAware currentAuditor() {
return () -> AuditorContextHolder.getAuditor();
}
I am trying to write above Java implementation in Groovy.
I see there are many ways to implement interfaces in groovy as shown in this Groovy ways to implement interfaces documentation.
I have implemented above Java code to groovy equivalent by using implement interfaces with a map as shown in above mentioned documentation.
// Groovy Equivalent by "implement interfaces with a map" method
AuditorAware currentAuditor() {
[getCurrentAuditor: AuditorContextHolder.auditor] as AuditorAware
}
But Implement interfaces with a closure method seems more concise as shown in documentation example. However, when I try to implement as follows, IntelliJ
shows errors saying Ambiguous Code Block.
// Groovy Equivalent by "implement interfaces with a closure" method ???
AuditorAware currentAuditor() {
{AuditorContextHolder.auditor} as AuditorAware
}
How can I change a Java 8 lambda implementation to groovy equivalent by using "implement interfaces with a closure" method?
Groovy compiler does not accept java 8 Lambdas.
The Groovy syntax doesn't support the lambda expressions, but we can rely on closure coersion to use Groovy closures as Java lambda expressions in our code. In the following sample we use the Java Streams API. Instead of lambda expressions for the filter and map methods we use Groovy closures.
Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
How to replace lambda expression with method reference in Java 8. If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference.
As commented by Dylan Bijnagte, following code worked.
// Groovy Equivalent by "implement interfaces with a closure" method
AuditorAware currentAuditor() {
{ -> AuditorContextHolder.auditor} as AuditorAware
}
Section Paramter Notes of Documentation on Groovy Closure explain this.
You can use the .&
operator to get a method reference:
class Auditor {
String name
}
interface AuditorAware {
Auditor getCurrentAuditor()
}
class AuditorContextHolder {
static getAuditor() { new Auditor(name: "joe") }
}
AuditorAware currentAuditor() {
AuditorContextHolder.&getAuditor
}
assert currentAuditor().currentAuditor.name == "joe"
In Java 8 you can use the ::
for method references:
AuditorAware currentAuditor() {
return AuditorContextHolder::getAuditor;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With