Skip to content

Commit 896b893

Browse files
mrsimplemrsimple
authored andcommitted
remove config class
1 parent 53a8afb commit 896b893

File tree

5 files changed

+128
-125
lines changed

5 files changed

+128
-125
lines changed

Simple_Event_Test/src/org/simple/eventbus/test/BusTestSuite.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static Test suite() {
3030
.includePackages(
3131
"org.simple.eventbus.test.EventBusTest",
3232
"org.simple.eventbus.test.EventTypeTest",
33-
"org.simple.eventbus.test.ThreadModeTest")
33+
"org.simple.eventbus.test.ThreadModeTest",
34+
"org.simple.eventbus.testDefaultMatchPolicyTest")
3435
.build();
3536
}
3637
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2015 Umeng, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.simple.eventbus.test;
26+
27+
import android.test.AndroidTestCase;
28+
29+
import org.simple.eventbus.EventBus;
30+
import org.simple.eventbus.EventType;
31+
import org.simple.eventbus.Subcriber;
32+
import org.simple.eventbus.matchpolicy.DefaultMatchPolicy;
33+
import org.simple.eventbus.matchpolicy.MatchPolicy;
34+
import org.simple.eventbus.test.mock.User;
35+
36+
import java.util.List;
37+
38+
/**
39+
* 注意 : 在发布事件时会查找与该事件匹配的订阅函数,默认的查找器会构造该事件父类的EventType,发布一个事件可能会引起多个事件。
40+
*
41+
* @author mrsimple
42+
*/
43+
public class DefaultMatchPolicyTest extends AndroidTestCase {
44+
45+
protected void setUp() throws Exception {
46+
super.setUp();
47+
48+
EventBus.getDefault().register(this);
49+
}
50+
51+
protected void tearDown() throws Exception {
52+
super.tearDown();
53+
54+
EventBus.getDefault().unregister(this);
55+
}
56+
57+
@Subcriber
58+
private void whatEver(User user) {
59+
60+
}
61+
62+
@Subcriber
63+
private void singleObjectParam(Object obj) {
64+
65+
}
66+
67+
public void testFindMatchMethod() {
68+
MatchPolicy policy = new DefaultMatchPolicy();
69+
List<EventType> types = policy.findMatchEventTypes(new EventType(User.class,
70+
EventType.DEFAULT_TAG), new User("test"));
71+
assertEquals(2, types.size());
72+
73+
types.clear();
74+
75+
// 发布一个Object事件
76+
types = policy.findMatchEventTypes(new EventType(Object.class,
77+
EventType.DEFAULT_TAG), new Object());
78+
assertEquals(1, types.size());
79+
}
80+
}

Simple_eventbus_demo/src/org/simple/eventbus/demo/fragment/MenuFragment.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@
4646
* 注意 : 如果发布的事件的参数类型是订阅的事件参数的子类,订阅函数默认也会被执行。例如你在订阅函数中订阅的是List<String>类型的事件,
4747
* 但是在发布时发布的是ArrayList<String>的事件,
4848
* 因此List<String>是一个泛型抽象,而ArrayList<String>才是具体的实现
49-
* ,因此这种情况下订阅函数也会被执行。如果你需要订阅函数能够接收到的事件类型必须严格匹配 ,你可以构造一个EventBusConfig对象,
50-
* 然后设置MatchPolicy然后在使用事件总线之前使用该EventBusConfig来初始化事件总线. <code>
51-
* EventBusConfig config = new EventBusConfig();
52-
config.setMatchPolicy(new StrictMatchPolicy());
53-
EventBus.getDefault().initWithConfig(config);
54-
* </code>
49+
* ,因此这种情况下订阅函数也会被执行。如果你需要订阅函数能够接收到的事件类型必须严格匹配 , 然后通过事件总线{@see
50+
* EventBus#setMatchPolicy(org.simple.eventbus.matchpolicy.MatchPolicy)}设置匹配策略.
5551
*
5652
* @author mrsimple
5753
*/

src/org/simple/eventbus/EventBus.java

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.simple.eventbus;
1818

19-
import org.simple.eventbus.config.EventBusConfig;
19+
import org.simple.eventbus.handler.AsyncEventHandler;
20+
import org.simple.eventbus.handler.DefaultEventHandler;
2021
import org.simple.eventbus.handler.EventHandler;
22+
import org.simple.eventbus.handler.UIThreadEventHandler;
23+
import org.simple.eventbus.matchpolicy.DefaultMatchPolicy;
2124
import org.simple.eventbus.matchpolicy.MatchPolicy;
2225

2326
import java.util.List;
@@ -93,10 +96,6 @@ protected java.util.Queue<EventType> initialValue() {
9396
* The Default EventBus instance
9497
*/
9598
private static EventBus sDefaultBus;
96-
/**
97-
* the event bus config
98-
*/
99-
private EventBusConfig mBusConfig = new EventBusConfig();
10099

101100
/**
102101
* private Constructor
@@ -112,7 +111,6 @@ private EventBus() {
112111
*/
113112
public EventBus(String desc) {
114113
mDesc = desc;
115-
initWithConfig(mBusConfig);
116114
}
117115

118116
/**
@@ -129,18 +127,6 @@ public static EventBus getDefault() {
129127
return sDefaultBus;
130128
}
131129

132-
/**
133-
* init event bus with EventBusConfig
134-
*
135-
* @param config
136-
*/
137-
public void initWithConfig(EventBusConfig config) {
138-
mDispatcher.mMatchPolicy = config.getMatchPolicy();
139-
mDispatcher.mUIThreadEventHandler = config.getUIThreadEventHandler();
140-
mDispatcher.mPostThreadHandler = config.getPostThreadHandler();
141-
mDispatcher.mAsyncEventHandler = config.getAsyncEventHandler();
142-
}
143-
144130
/**
145131
* register a subscriber into the mSubcriberMap, the key is subscriber's
146132
* method's name and tag which annotated with {@see Subcriber}, the value is
@@ -191,6 +177,42 @@ public void post(Object event, String tag) {
191177
mDispatcher.dispatchEvents(event);
192178
}
193179

180+
/**
181+
* 设置订阅函数匹配策略
182+
*
183+
* @param policy 匹配策略
184+
*/
185+
public void setMatchPolicy(MatchPolicy policy) {
186+
mDispatcher.mMatchPolicy = policy;
187+
}
188+
189+
/**
190+
* 设置执行在UI线程的事件处理器
191+
*
192+
* @param handler
193+
*/
194+
public void setUIThreadEventHandler(EventHandler handler) {
195+
mDispatcher.mUIThreadEventHandler = handler;
196+
}
197+
198+
/**
199+
* 设置执行在post线程的事件处理器
200+
*
201+
* @param handler
202+
*/
203+
public void setPostThreadHandler(EventHandler handler) {
204+
mDispatcher.mPostThreadHandler = handler;
205+
}
206+
207+
/**
208+
* 设置执行在异步线程的事件处理器
209+
*
210+
* @param handler
211+
*/
212+
public void setAsyncEventHandler(EventHandler handler) {
213+
mDispatcher.mAsyncEventHandler = handler;
214+
}
215+
194216
/**
195217
* 返回订阅map
196218
*
@@ -236,17 +258,17 @@ private class EventDispatcher {
236258
/**
237259
* 将接收方法执行在UI线程
238260
*/
239-
EventHandler mUIThreadEventHandler;
261+
EventHandler mUIThreadEventHandler = new UIThreadEventHandler();
240262

241263
/**
242264
* 哪个线程执行的post,接收方法就执行在哪个线程
243265
*/
244-
EventHandler mPostThreadHandler;
266+
EventHandler mPostThreadHandler = new DefaultEventHandler();
245267

246268
/**
247269
* 异步线程中执行订阅方法
248270
*/
249-
EventHandler mAsyncEventHandler;
271+
EventHandler mAsyncEventHandler = new AsyncEventHandler();
250272

251273
/**
252274
* 缓存一个事件类型对应的可EventType列表
@@ -255,7 +277,7 @@ private class EventDispatcher {
255277
/**
256278
* 事件匹配策略,根据策略来查找对应的EventType集合
257279
*/
258-
MatchPolicy mMatchPolicy;
280+
MatchPolicy mMatchPolicy = new DefaultMatchPolicy();
259281

260282
/**
261283
* @param event

src/org/simple/eventbus/config/EventBusConfig.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)