Hi Dennis
Not sure if you got anywhere with this, but here's another example:
public final class EventLoop {
public static final class Event {
private final String key;
private final Object data;
public Event(String key, Object data) {
this.key = key;
this.data = data;
}
}
private final ConcurrentLinkedDeque<Event> events = new ConcurrentLinkedDeque<>();
private final ConcurrentHashMap<String, Consumer<Object>> handlers = new ConcurrentHashMap<>();
public EventLoop on(String key, Consumer<Object> handler) {
handlers.put(key, handler);
return this;
}
public void dispatch(Event event) {
events.add(event);
}
public void run() {
while (!(events.isEmpty() && Thread.interrupted())) {
if (!events.isEmpty()) {
Event event = events.pop();
if (handlers.containsKey(event.key)) {
handlers.get(event.key).accept(event.data);
} else {
System.err.println("No handler for key " + event.key);
}
In the run() method, event.key and event.data are both unidentified symbols.
Thanks.
There also seems to be an issue identifying lambda parameters:
eventLoop
.on("hello", s -> System.out.println("hello " + s))
.on("tick", n -> System.out.println("tick #" + n))
.on("stop", v -> eventLoop.stop())
.run();
s and v are unidentified and n is identified as an index variable used for a for loop earlier in the code.