android - ThreadEnforcer usage in Event Bus register method -
can simplify code register object able listen events posted event bus? also, need wrap post call in handler contact ui thread ?? main question thread enforcer doing here
public void register(object object) { if (object == null) { throw new nullpointerexception("object register must not null."); } enforcer.enforce(this); map<class<?>, eventproducer> foundproducers = handlerfinder.findallproducers(object); (class<?> type : foundproducers.keyset()) { final eventproducer producer = foundproducers.get(type); eventproducer previousproducer = producersbytype.putifabsent(type, producer); //checking if previous producer existed if (previousproducer != null) { throw new illegalargumentexception("producer method type " + type + " found on type " + producer.target.getclass() + ", registered type " + previousproducer.target.getclass() + "."); } set<eventhandler> handlers = handlersbytype.get(type); if (handlers != null && !handlers.isempty()) { (eventhandler handler: handlers) { dispatchproducerresulttohandler(handler, producer); } } } map<class<?>, set<eventhandler>> foundhandlersmap = handlerfinder.findallsubscribers(object); (class<?> type : foundhandlersmap.keyset()) { set<eventhandler> handlers = handlersbytype.get(type); if (handlers == null) { //concurrent put if absent set<eventhandler> handlerscreation = new copyonwritearrayset<eventhandler>(); handlers = handlersbytype.putifabsent(type, handlerscreation); if (handlers == null) { handlers = handlerscreation; } } final set<eventhandler> foundhandlers = foundhandlersmap.get(type); handlers.addall(foundhandlers); } (map.entry<class<?>, set<eventhandler>> entry : foundhandlersmap.entryset()) { class<?> type = entry.getkey(); eventproducer producer = producersbytype.get(type); if (producer != null && producer.isvalid()) { set<eventhandler> foundhandlers = entry.getvalue(); (eventhandler foundhandler : foundhandlers) { if (!producer.isvalid()) { break; } if (foundhandler.isvalid()) { dispatchproducerresulttohandler(foundhandler, producer); } } } } }
Comments
Post a Comment