java 8 event listener/dispatcher using lambdas/method references - how to achieve certain things? -



java 8 event listener/dispatcher using lambdas/method references - how to achieve certain things? -

i'm trying write event engine in java using newly added lambdas. much if next code work:

public class test { public test() { eventengine.listen(eventtype.this, self::thiseventcallback); eventengine.listen(eventtype.that, self::thateventcallback); eventengine.listen(eventtype.other, (other) -> other.dox()); } private void thiseventcallback() { // whatever here } private boolean thateventcallback(someobject parameter) { homecoming parameter.somecheckorwhatever(); } }

as far understand, have define generic empty interface, example, public interface listener {// nil here}, , extend via various other interfaces each event type can specify different parameters , homecoming types necassary. obviously, require casting callbacks specific interface within eventengine's trigger method(s), have no problem that.

however, before need find out how reference these private methods have defined eventdispatcher.listen method. self::thiseventcallback doesn't work. there way in java 8 or possible in scala?

if not, suggest replacement not involve creating new object every listener/callback?

eventengine.listen(eventtype.this, this::thiseventcallback); eventengine.listen(eventtype.that, this::thateventcallback); eventengine.listen(eventtype.other, (other) -> other.dox());

so this instead of self.

and need functional interfaces one abstract method having same signature callback.

public interface thisinterface { public void thiseventcallback(); } public interface thatinterface { public boolean thateventcallback(someobject parameter) } class eventengine { public void listen(type t, thisinterfcace thiscallback) { thiscallback.thiseventcallback(); } public void listen(type t, thatinterfcace thatcallback) { boolean ok = thatcallback.thateventcallback(); } ... }

however there many functional interfaces predefined, should need learn. instance here, 1 not need own interfaces.

class eventengine { public void listen(type t, consumer<void> thiscallback) { thiscallback.accept(); } public void listen(type t, predicate<void> thatcallback) { boolean ok = thatcallback.test(); }

whether above correct, not sure (at moment deep in java 6 - sigh).

java lambda java-8

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -