前端时间,由于出差在外,博客停更了很长一段时间,从今天开始,继续开始更新博客。
之前我们研究了Otto的其它几部分的源码,而我们在使用的过程中,最常用也是直接使用的其实是Bus这个类,今天就来从源码的角度看看Bus类。
Bus
分发事件给事件监听器,并且提供事件监听器的注册方法。下面我们从使用的角度来学习源码。
在我们使用的时候,首先是要构建Bus类,来看看构造方法。
/** Creates a new Bus named "default" that enforces actions on the main thread
* 创建默认的构造方法,传递一个默认的身份标识
*/
public Bus() {
this(DEFAULT_IDENTIFIER);
}
/**
* Creates a new Bus with the given {@code identifier} that enforces actions on the main thread.
* 创建一个运行在主线程的Bus
*
* @param identifier a brief name for this bus, for debugging purposes. Should be a valid Java identifier.
* identifier 的作用是一个对于bus的名称标识,作为调试用。
*/
public Bus(String identifier) {
this(ThreadEnforcer.MAIN, identifier);
}
/**
* Creates a new Bus named "default" with the given {@code enforcer} for actions.
* 创建一个运行在自定义线程的Bus
* @param enforcer Thread enforcer for register, unregister, and post actions.
*/
public Bus(ThreadEnforcer enforcer) {
this(enforcer, DEFAULT_IDENTIFIER);
}
/**
* Creates a new Bus with the given {@code enforcer} for actions and the given {@code identifier}.
*
* @param enforcer Thread enforcer for register, unregister, and post actions.
* @param identifier A brief name for this bus, for debugging purposes. Should be a valid Java identifier.
*/
public Bus(ThreadEnforcer enforcer, String identifier) {
this(enforcer, identifier, HandlerFinder.ANNOTATED);
}
/**
* Test constructor which allows replacing the default {@code HandlerFinder}.
*
* @param enforcer Thread enforcer for register, unregister, and post actions.
* @param identifier A brief name for this bus, for debugging purposes. Should be a valid Java identifier.
* @param handlerFinder Used to discover event handlers and producers when registering/unregistering an object.
*/
Bus(ThreadEnforcer enforcer, String identifier, HandlerFinder handlerFinder) {
this.enforcer = enforcer;
this.identifier = identifier;
this.handlerFinder = handlerFinder;
}
在上面的构造方法中,有两个参数
ThreadEnforcer -- 这个是确保bus运行在指定的线程。在前面已经讲过了,如果还没看过的,可以点击 ThreadEnforcer 进行查看。
HandlerFinder -- 这个是用来提前事件生产者和操作者的注册和取消注册的,之前也讲过,可以点击 查看
构造过后,我们需要在注册。
@Override
protected void onResume() {
super.onResume();
// Register ourselves so that we can provide the initial value.
BusProvider.getInstance().register(this);
}
其中用到了register,下面我们来看看Bus中的register:
public void register(Object object) {
if (object == null) {
throw new NullPointerException("Object to register must not be null.");
}
// 确保运行的线程
enforcer.enforce(this);
// 发现所有的生产者(@Producer注释的函数)
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);
}
}
}
// 获取所有的订阅者(@Subscribe)
Map<Class<?>, Set<EventHandler>> foundHandlersMap = handlerFinder.findAllSubscribers(object);
for (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);
if (!handlers.addAll(foundHandlers)) {
throw new IllegalArgumentException("Object already registered.");
}
}
// 分发生产者到处理器
for (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();
for (EventHandler foundHandler : foundHandlers) {
if (!producer.isValid()) {
break;
}
if (foundHandler.isValid()) {
dispatchProducerResultToHandler(foundHandler, producer);
}
}
}
}
}
思路其实也挺简单的,就是首先获取所有的@Producer注释的方法,然后获取所有的@Subscribe注释的函数,最后将两者关联起来,这样,在调用post()方法时,就能实现数据传递的效果了。
之后就是unregister方法:
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()) {
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);
}
}
在之前的register的时候,用的是put方法,现在调用的是remove方法。