Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Lambda Expression in an Action Listener

We are using some socket API in Java that use extensively ActionListeners. For example:

SocketServer server = SocketFactory.create().addListener(8888, "localhost")
                .setHandler(new SocketHandler() {

                    public void handle(final ServerExchange exchange)
                            throws Exception {
                        exchange.send("Hello World");
                    }
                }).create();

I'd like to start simplifying part of this code using Lambda expressions. So I started rewriting the above example using:

SocketServer server = SocketFactory.create().addListener(8888, "localhost")
            .setHandler(new SocketHandler() {
               exchange -> {
                  exchange.send("Hello World");
               }

}).create();

However Eclipse (Luna with JDK1.8 support) complains that the method handle(..) is not implemented. Yet using the handle method is exactly what I'd like to avoid implementing.... Any idea how can I fix it ?
Thanks!

UPDATE: Thanks for your reply. Well SocketHandler is an interface, however I've wrapped it now in a functional interface as follows:

@FunctionalInterface

public interface SimpleFuncInterface extends  SocketHandler{

     void handle(ServerExchange exchange) throws Exception;

}

Now my code looks like this:

SocketServer  server = SocketFactory.create().addListener(8080, "localhost")
                .setHandler(doWork(() -> exchange.send("Hello World"))).create();    

        server.start();
}
private static void doWork(SimpleFuncInterface itf) {


}

The code seems error free now, however the compiler complains that Lambda expression signature does not match the signature of the functional interface method handle(ServerExchange). Any suggestion ?

like image 341
user2824073 Avatar asked May 12 '14 20:05

user2824073


People also ask

What is lambda listener?

Because the ActionListener interface defines only one method actionPerformed() , it is a functional interface which means there's a place to use Lambda expressions to replace the boilerplate code.

What is the benefit of using lambda expression for event handling?

Fewer Lines of Code − One of the most benefits of a lambda expression is to reduce the amount of code. We know that lambda expressions can be used only with a functional interface.

Is ActionListener a functional interface?

An ActionListener interface defines only one method actionPerformed(). It is a functional interface which means that there's a place to use lambda expressions to replace the code.

Can we use lambda expression in interface?

From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces.


1 Answers

It looks like you have a syntax error in your code. Your are trying to define the lambda expression within an anonymous inner class. However, you should use the lambda expression instead of the anonymous inner class. I guess the following works:

SocketServer server = SocketFactory.create().addListener(8888, "localhost")
    .setHandler(e -> { e.send("Hello World"); }).create();

If the method setHandler is overloaded, it might be necessary to provide more information, e.g. by adding a cast:

SocketServer server = SocketFactory.create().addListener(8888, "localhost")
    .setHandler((SocketHandler) e -> { e.send("Hello World"); }).create();
like image 52
nosid Avatar answered Sep 22 '22 12:09

nosid