Convert java lambda expression to version 1.6

I have code with java 1.8, and I would like to convert it so that it works with 1.6.

This code is as follows:

private void handleCanNotConnect(Throwable cause) {
    isConnected = false;
    fireAll(l -> l.connectionFailed(cause));
}

private void fireAll(Consumer<MyListener> action){
    action.accept(listener);
}

How should I convert it?

Hello!

+4
source share
1 answer

The back of the lambda functions, we will create anonymous classes for implementing interfaces on the fly.

Here is the interface as it is Consumer<MyListener>unavailable:

public interface MyConsumer {
    public void accept(MyListener l);
}

Then you can use:

private void handleCanNotConnect(final Throwable cause) {
    isConnected = false;
    fireAll(new MyConsumer() {
        @Override
        public void accept(MyListener l) {
            l.connectionFailed(cause);
        }
    });
}

private void fireAll(MyConsumer action){
    action.accept(listener);
}

Note that causemarked as final.

+4
source

Source: https://habr.com/ru/post/1678354/


All Articles