标签:square 主线程 cep 接受 解释 ada type return article
这种方法的解释是这种。先看下英文
/** * Registers all handler methods on {@code object} to receive events and producer methods to provide events. * If any subscribers are registering for types which already have a producer they will be called immediately * with the result of calling that producer. * If any producers are registering for types which already have subscribers, each subscriber will be called with * the value from the result of calling the producer. * * @param object object whose handler methods should be registered. * @throws NullPointerException if the object is null. */
public void register(Object object) { if (object == null) { throw new NullPointerException("Object to register must not be null."); } //检查是否在主线程中进行了注冊的。默认必须在主线程中调用 enforcer.enforce(this); //key是事件的class对象 Map<Class<?>, EventProducer> foundProducers = handlerFinder.findAllProducers(object);
HandlerFinder ANNOTATED = new HandlerFinder() { @Override public Map<Class<?>, EventProducer> findAllProducers(Object listener) { return AnnotatedHandlerFinder.findAllProducers(listener); } @Override public Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener) { return AnnotatedHandlerFinder.findAllSubscribers(listener); } };
static Map<Class<?>, EventProducer> findAllProducers(Object listener) { final Class<?> listenerClass = listener.getClass(); Map<Class<?>, EventProducer> handlersInMethod = new HashMap<Class<?
>, EventProducer>(); //检查是否不存在此listenerClass的生产者方法。须要找出来放到PRODUCERS_CACHE中 //PRODUCERS_CACHE是一个map,能够是listener的class对象,值是全部的事件生产者方法 if (!PRODUCERS_CACHE.containsKey(listenerClass)) { loadAnnotatedMethods(listenerClass); } //以下的代码就是把全部的生产者方法封装起来。返回给调用者 Map<Class<?>, Method> methods = PRODUCERS_CACHE.get(listenerClass); if (!methods.isEmpty()) { for (Map.Entry<Class<?>, Method> e : methods.entrySet()) { EventProducer producer = new EventProducer(listener, e.getValue()); handlersInMethod.put(e.getKey(), producer); } } return handlersInMethod; }
//key是事件的class对象 Map<Class<?这行代码用于回调>, EventProducer> foundProducers = handlerFinder.findAllProducers(object); for (Class<?
> type : foundProducers.keySet()) { //以下几行代码用来检查,事件是否已经注冊过了,一个类一个事件仅仅能注冊一次 final EventProducer producer = foundProducers.get(type); EventProducer previousProducer = producersByType.putIfAbsent(type, producer); //checking if the previous producer existed if (previousProducer != null) { throw new IllegalArgumentException("Producer method for type " + type + " found on type " + producer.target.getClass() + ", but already registered by type " + previousProducer.target.getClass() + "."); } //检查一下注冊这个事件的注冊者是否存在,存在就回调一下注冊者 Set<EventHandler> handlers = handlersByType.get(type); if (handlers != null && !handlers.isEmpty()) { for (EventHandler handler : handlers) { dispatchProducerResultToHandler(handler, producer); } } }
//假设有处理此事件的注冊者,回调注冊者 Map<Class<?>, Set<EventHandler>> foundHandlersMap = handlerFinder.findAllSubscribers(object); for (Class<?> type : foundHandlersMap.keySet()) { //type变量是事件的class对象 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); }
。
public void post(Object event) { if (event == null) { throw new NullPointerException("Event to post must not be null."); } enforcer.enforce(this); //返回这个event的全部继承关系链的全部class对象(父类class对象和自己) Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass()); //对event家族的全部类的相关注冊方法进行调用 boolean dispatched = false; for (Class<?
> eventType : dispatchTypes) { //获得和eventType相关的全部注冊方法 Set<EventHandler> wrappers = getHandlersForEventType(eventType); //把须要回调处理的注冊方法的包装类塞进当前线程处理的队列中去 if (wrappers != null && !wrappers.isEmpty()) { dispatched = true; for (EventHandler wrapper : wrappers) { enqueueEvent(event, wrapper); } } } //没处理的。再分发一次。可是没有发现再次处理的逻辑 if (!dispatched && !(event instanceof DeadEvent)) { post(new DeadEvent(this, event)); } dispatchQueuedEvents(); }
public void unregister(Object object) { if (object == null) { throw new NullPointerException("Object to unregister must not be null."); } enforcer.enforce(this); Map<Class<?>, EventProducer> producersInListener = handlerFinder.findAllProducers(object); for (Map.Entry<Class<?最后总结一下,>, EventProducer> entry : producersInListener.entrySet()) { final Class<?
> key = entry.getKey(); EventProducer producer = getProducerForEventType(key); EventProducer value = entry.getValue(); if (value == null || !value.equals(producer)) { throw new IllegalArgumentException( "Missing event producer for an annotated method. Is " + object.getClass() + " registered?"); } producersByType.remove(key).invalidate(); } //返回当前对象的全部注冊的方法。置为无效并删除掉 Map<Class<?>, Set<EventHandler>> handlersInListener = handlerFinder.findAllSubscribers(object); for (Map.Entry<Class<?>, Set<EventHandler>> entry : handlersInListener.entrySet()) { //返回相应event的全部注冊方法的包装类 Set<EventHandler> currentHandlers = getHandlersForEventType(entry.getKey()); Collection<EventHandler> eventMethodsInListener = entry.getValue(); if (currentHandlers == null || !currentHandlers.containsAll(eventMethodsInListener)) { throw new IllegalArgumentException( "Missing event handler for an annotated method. Is " + object.getClass() + " registered?
"); } for (EventHandler handler : currentHandlers) { if (eventMethodsInListener.contains(handler)) { handler.invalidate(); } } currentHandlers.removeAll(eventMethodsInListener); } }
标签:square 主线程 cep 接受 解释 ada type return article
原文地址:http://www.cnblogs.com/cxchanpin/p/7050568.html