Event Handlers with Method References / Lambdas

Ben Evans benjamin.john.evans at gmail.com
Sun Apr 1 06:59:41 PDT 2012


Hi,

This mail came out of a discussion with Conrad Winchester (copied) -
who should totally speak up if I'm misrepresenting his ideas / code ;)

Suppose we want to build a simple event handling system:

public interface EventDispatcher {
    void addEventHandler( String eventType, Block<Event> handler );
    void dispatchEvent( Event event );
}

with some EventDispatcherImpl implementation that e.g. holds a
List<EventDispatcher> and dispatches to each in turn. Of course, for
the reasons discussed in the lambda equality thread, in a real
implementation we'd want to return a handle object from
addEventHandler() so we can remove event handlers again later - but
let's not worry about that for now.

Then, suppose we have some sort of temperature monitoring code:

public class TemperatureModel extends EventDispatcherImpl {
    private double temperature;

    public void setTemperature(double value) {
        if ( value == temperature) return;

        temperature = value;
        dispatchEvent( new Event("temperatureChanged"));
    }

    public double getTemperature() {
        return temperature;
    }
}

and a corresponding handler:

public class TemperatureHandler {
    public TemperatureModel temperatureModel;

    public void init() {
        temperatureModel.addEventHandler("temperatureChange",
this::onTemperatureChange);
    }

    public void onTemperatureChange( Event event ) {
        System.out.println("The temperature is now "+
temperatureModel.getTemperature());
    }
}

Then I take it that this::onTemperatureChange is exactly equivalent to
the lambda: (e) -> { onTemperatureChange(e);}

My question is, I'm using Henri Gomez's most recent Lambda build for
OS X. I'm getting fairly regular (but not every time) crashes when
running javadoc over TemperatureHandler (and occasional javac crashes
as well) - is there a group that would be interested in the crash
reports, or are we still expecting javac / javadoc to be
inconsistently stable?

Thanks,

Ben


More information about the lambda-dev mailing list