EventBus源码阅读(17)-EventBusBuilder
来源:互联网 发布:淘宝怎么设置限购5件 编辑:程序博客网 时间:2024/06/03 18:33
我们看到 EventBus,EventBusBuilder,从名字就可以猜到这是建造者模式了。
代码都比较多,还好我们已经把外围的困难都扫平了(异常处理在此先忽略,不影响我们理解,最后的时候在阅读相关代码)
我们先看EventBusBuilder
源码:
/* * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.greenrobot.eventbus;import org.greenrobot.eventbus.meta.SubscriberInfoIndex;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * Creates EventBus instances with custom parameters and also allows to install a custom default EventBus instance. * Create a new builder using {@link EventBus#builder()}. */public class EventBusBuilder { private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();//这个就是我们的线程池了,异步任务,后台任务就要靠它来执行了。 boolean logSubscriberExceptions = true;//订阅者异常日志 boolean logNoSubscriberMessages = true;//不要订阅者消息日志 boolean sendSubscriberExceptionEvent = true;//是否发送订阅者异常 boolean sendNoSubscriberEvent = true;//是否发送无订阅者异常 boolean throwSubscriberException;//是否抛出订阅者异常 boolean eventInheritance = true;//是否继承事件 boolean ignoreGeneratedIndex;//是否忽略生成的索引 boolean strictMethodVerification;//是否严格执行方法验证 ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;//默认线程池 List<Class<?>> skipMethodVerificationForClasses;//需要跳过执行方法验证的类 List<SubscriberInfoIndex> subscriberInfoIndexes;//订阅者信息索引 EventBusBuilder() { } /** Default: true */ public EventBusBuilder logSubscriberExceptions(boolean logSubscriberExceptions) { this.logSubscriberExceptions = logSubscriberExceptions; return this; } /** Default: true */ public EventBusBuilder logNoSubscriberMessages(boolean logNoSubscriberMessages) { this.logNoSubscriberMessages = logNoSubscriberMessages; return this; } /** Default: true */ public EventBusBuilder sendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent) { this.sendSubscriberExceptionEvent = sendSubscriberExceptionEvent; return this; } /** Default: true */ public EventBusBuilder sendNoSubscriberEvent(boolean sendNoSubscriberEvent) { this.sendNoSubscriberEvent = sendNoSubscriberEvent; return this; } /** * Fails if an subscriber throws an exception (default: false). * <p/> * Tip: Use this with BuildConfig.DEBUG to let the app crash in DEBUG mode (only). This way, you won't miss * exceptions during development. */ public EventBusBuilder throwSubscriberException(boolean throwSubscriberException) { this.throwSubscriberException = throwSubscriberException; return this; } /** * By default, EventBus considers the event class hierarchy (subscribers to super classes will be notified). * Switching this feature off will improve posting of events. For simple event classes extending Object directly, * we measured a speed up of 20% for event posting. For more complex event hierarchies, the speed up should be * >20%. * <p/> * However, keep in mind that event posting usually consumes just a small proportion of CPU time inside an app, * unless it is posting at high rates, e.g. hundreds/thousands of events per second. */ public EventBusBuilder eventInheritance(boolean eventInheritance) { this.eventInheritance = eventInheritance; return this; } /** * Provide a custom thread pool to EventBus used for async and background event delivery. This is an advanced * setting to that can break things: ensure the given ExecutorService won't get stuck to avoid undefined behavior. */ public EventBusBuilder executorService(ExecutorService executorService) { this.executorService = executorService; return this; } /** * Method name verification is done for methods starting with onEvent to avoid typos; using this method you can * exclude subscriber classes from this check. Also disables checks for method modifiers (public, not static nor * abstract). */ public EventBusBuilder skipMethodVerificationFor(Class<?> clazz) { if (skipMethodVerificationForClasses == null) { skipMethodVerificationForClasses = new ArrayList<>(); } skipMethodVerificationForClasses.add(clazz); return this; } /** Forces the use of reflection even if there's a generated index (default: false). */ public EventBusBuilder ignoreGeneratedIndex(boolean ignoreGeneratedIndex) { this.ignoreGeneratedIndex = ignoreGeneratedIndex; return this; } /** Enables strict method verification (default: false). */ public EventBusBuilder strictMethodVerification(boolean strictMethodVerification) { this.strictMethodVerification = strictMethodVerification; return this; } /** Adds an index generated by EventBus' annotation preprocessor. */ public EventBusBuilder addIndex(SubscriberInfoIndex index) { if(subscriberInfoIndexes == null) { subscriberInfoIndexes = new ArrayList<>(); } subscriberInfoIndexes.add(index); return this; } /** * Installs the default EventBus returned by {@link EventBus#getDefault()} using this builders' values. Must be * done only once before the first usage of the default EventBus. * * @throws EventBusException if there's already a default EventBus instance in place */ public EventBus installDefaultEventBus() { synchronized (EventBus.class) { if (EventBus.defaultInstance != null) { throw new EventBusException("Default instance already exists." + " It may be only set once before it's used the first time to ensure consistent behavior."); } EventBus.defaultInstance = build(); return EventBus.defaultInstance; } } /** Builds an EventBus based on the current configuration. */ public EventBus build() { return new EventBus(this); }}
这个真么什么好说的了,看一遍,理解理解字面意思,知道有什么就行了
0 0
- EventBus源码阅读(17)-EventBusBuilder
- EventBus源码阅读(18)-EventBus-总括
- EventBus源码阅读(19)-EventBus初始化
- EventBus源码阅读(20)-EventBus注册
- EventBus源码阅读(21)-EventBus触发
- EventBus源码阅读(22)-EventBus注销
- EventBus源码阅读(23)-EventBus其它
- EventBus源码阅读(24)-EventBus-Sticky
- EventBus源码阅读(开篇)
- EventBus源码阅读(1)-ThreadMode
- EventBus阅读源码(2)-SubscriberMethod
- EventBus源码阅读(3)-Subscribe
- EventBus源码阅读(4)-Subscription
- EventBus源码阅读(5)-SubscriberInfo
- EventBus源码阅读(6)-AbstractSubscriberInfo
- EventBus源码阅读(7)-SimpleSubscriberInfo
- EventBus源码阅读(8)-SubscriberMethodInfo
- EventBus源码阅读(9)-SubscriberInfoIndex
- 图的最小生成树Prim算法朴素版(C++)
- 谈谈我对SQL 注入的理解
- Oledb,写入Excel 异常 标准表达式中数据类型不匹配。
- iOS 单击图片UIImage提取图片上的颜色UIColor (OC)
- hdu5810
- EventBus源码阅读(17)-EventBusBuilder
- 获取键盘高度
- 文件加密码
- HDU 2045不容易系列之(3)—— LELE的RPG难题
- ThinkPHP2.2框架执行原理、流程图在线手册
- 30左右的书籍都算经典
- 音视频开发——ffmpeg解码(四)
- ECMAScript-初回学习“函数”
- 移植uboot第一步:下载,编译,烧到板子上试验