Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy equivalent for Java 8 Lambda Expression

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?

like image 235
TheKojuEffect Avatar asked Oct 20 '14 02:10

TheKojuEffect


People also ask

Does Groovy support lambda?

Groovy compiler does not accept java 8 Lambdas.

How do I use lambda expression in Groovy?

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.

Does Java 8 have lambda?

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.

Which can be used instead of lambda expression?

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.


2 Answers

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.

like image 193
TheKojuEffect Avatar answered Oct 24 '22 00:10

TheKojuEffect


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;
  }
like image 20
Will Avatar answered Oct 24 '22 00:10

Will