Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Null Event #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ private void singleObjectParam(Object obj) {
public void testFindMatchMethod() {
MatchPolicy policy = new DefaultMatchPolicy();
List<EventType> types = policy.findMatchEventTypes(new EventType(User.class,
EventType.DEFAULT_TAG), new User("test"));
EventType.DEFAULT_TAG), User.class);
assertEquals(2, types.size());

types.clear();

// 发布一个Object事件
types = policy.findMatchEventTypes(new EventType(Object.class,
EventType.DEFAULT_TAG), new Object());
EventType.DEFAULT_TAG), Object.class);
assertEquals(1, types.size());

// 发布一个null事件
types = policy.findMatchEventTypes(new EventType(null,
EventType.DEFAULT_TAG), null);
assertEquals(1, types.size());
}
}
30 changes: 26 additions & 4 deletions Simple_Event_Test/src/org/simple/eventbus/test/EventBusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public void testRepeatRegister() {
assertEquals(2, getSubscriptions(new EventType(User.class)).size());
// Object类型的函数为1一个
assertEquals(1, getSubscriptions(new EventType(Object.class)).size());
// NULL类型的函数为2一个
assertEquals(2, getSubscriptions(new EventType(null)).size());
// NULL类型tag为tag_null的函数为1一个
assertEquals(1, getSubscriptions(new EventType(null, "tag_null")).size());
}

/**
Expand All @@ -105,6 +109,9 @@ public void testRepeatRegisterWithTag() {

// 类型为Person且tag为"another"的有效注册函数为1个.
assertEquals(1, getSubscriptions(new EventType(User.class, "another")).size());

// 类型为NULL且tag为"tag_null"的有效注册函数为1个.
assertEquals(1, getSubscriptions(new EventType(null, "tag_null")).size());
}

/**
Expand All @@ -115,6 +122,8 @@ public void testSubscribeAndPost() {
// 正常注册与发布
bus.register(mockActivity);
bus.post(new User("mr.simple"));
bus.post(User.class);
bus.post("tag_null");

// 移除对象
bus.unregister(mockActivity);
Expand All @@ -135,16 +144,26 @@ public void testUnRegisterNull() {

public void testStickyEvents() {
assertEquals(0, bus.getStickyEvents().size());
bus.postSticky("hello");
bus.postSticky("hello", EventType.DEFAULT_TAG);
assertEquals(1, bus.getStickyEvents().size());
final String tag = "sticky";
bus.postSticky("world", tag);
assertEquals(2, bus.getStickyEvents().size());

bus.postSticky(String.class);
assertEquals(3, bus.getStickyEvents().size());
bus.postSticky(tag);
assertEquals(4, bus.getStickyEvents().size());
bus.postSticky(tag, String.class);
assertEquals(5, bus.getStickyEvents().size());

bus.removeStickyEvent(String.class);
assertEquals(1, bus.getStickyEvents().size());
assertEquals(3, bus.getStickyEvents().size());

bus.removeStickyEvent(String.class, tag);
assertEquals(1, bus.getStickyEvents().size());

bus.removeStickyEvent(null, tag);
assertEquals(0, bus.getStickyEvents().size());
}

Expand All @@ -153,25 +172,28 @@ public void testStickySubscriber() {
assertEquals(null, subscriber.mStickyName);
assertEquals(null, subscriber.mStickyTag);
// 未注册时发布事件,那么对象接收不到事件
bus.post("hello");
bus.post("hello", EventType.DEFAULT_TAG);
// 普通注册形式
bus.register(subscriber);
assertEquals(null, subscriber.mStickyName);
assertEquals(null, subscriber.mStickyTag);
bus.unregister(subscriber);

// post sticky事件
bus.postSticky("simple");
bus.postSticky("simple", EventType.DEFAULT_TAG);
// 以sticky的形式注册
bus.registerSticky(subscriber);

// 接收到数据
assertEquals("simple", subscriber.mStickyName);
assertEquals(null, subscriber.mStickyTag);
bus.unregister(subscriber);

// post sticky事件, 含有tag, 这里需要注意
bus.postSticky("simple", "sticky");
bus.registerSticky(subscriber);
assertEquals("sticky", subscriber.mStickyTag);
bus.unregister(subscriber);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ void methodWithAnotherTag(User person) {
void onEvent(Object object) {
System.out.println("invoke onEvent(Person person) in " + this.getClass().getName());
}

@Subscriber
void onEvent() {
System.out.println("invoke onEvent() in " + this.getClass().getName());
}

@Subscriber(tag = "tag_null")
void methodWithTag() {
System.out.println("invoke methodWithTag() tag_null in " + this.getClass().getName());
}
}
67 changes: 48 additions & 19 deletions src/org/simple/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.simple.eventbus;

import android.util.Log;

import org.simple.eventbus.handler.AsyncEventHandler;
import org.simple.eventbus.handler.DefaultEventHandler;
import org.simple.eventbus.handler.EventHandler;
Expand Down Expand Up @@ -185,6 +183,18 @@ public void unregister(Object subscriber) {
public void post(Object event) {
post(event, EventType.DEFAULT_TAG);
}

public void post(Class<?> eventClass) {
post(null, EventType.DEFAULT_TAG, eventClass);
}

public void post(String tag) {
post(null, tag, null);
}

public void post(String tag, Class<?> eventClass) {
post(null, tag, eventClass);
}

/**
* 发布事件
Expand All @@ -193,11 +203,18 @@ public void post(Object event) {
* @param tag 事件的tag, 类似于BroadcastReceiver的action
*/
public void post(Object event, String tag) {
if (event == null) {
Log.e(this.getClass().getSimpleName(), "The event object is null");
return;
}
mLocalEvents.get().offer(new EventType(event.getClass(), tag));
post(event, tag, null == event ? null : event.getClass());
}

/**
* 发布事件
*
* @param event 要发布的事件
* @param tag 事件的tag, 类似于BroadcastReceiver的action
* @param eventClass 事件类类型
*/
public void post(Object event, String tag, Class<?> eventClass) {
mLocalEvents.get().offer(new EventType(eventClass, tag));
mDispatcher.dispatchEvents(event);
}

Expand All @@ -209,6 +226,18 @@ public void post(Object event, String tag) {
public void postSticky(Object event) {
postSticky(event, EventType.DEFAULT_TAG);
}

public void postSticky(Class<?> eventClass) {
postSticky(null, EventType.DEFAULT_TAG, eventClass);
}

public void postSticky(String tag) {
postSticky(null, tag, null);
}

public void postSticky(String tag, Class<?> eventClass) {
postSticky(null, tag, eventClass);
}

/**
* 发布含有tag的Sticky事件
Expand All @@ -217,14 +246,18 @@ public void postSticky(Object event) {
* @param tag 事件tag
*/
public void postSticky(Object event, String tag) {
if (event == null) {
Log.e(this.getClass().getSimpleName(), "The event object is null");
return;
}
EventType eventType = new EventType(event.getClass(), tag);
postSticky(event, tag, event == null ? null : event.getClass());
}

public void postSticky(Object event, String tag, Class<?> eventClass) {
EventType eventType = new EventType(eventClass, tag);
eventType.event = event;
mStickyEvents.add(eventType);
}

public void removeStickyEvent(String tag) {
removeStickyEvent(null, tag);
}

public void removeStickyEvent(Class<?> eventClass) {
removeStickyEvent(eventClass, EventType.DEFAULT_TAG);
Expand All @@ -239,8 +272,7 @@ public void removeStickyEvent(Class<?> eventClass, String tag) {
Iterator<EventType> iterator = mStickyEvents.iterator();
while (iterator.hasNext()) {
EventType eventType = iterator.next();
if (eventType.paramClass.equals(eventClass)
&& eventType.tag.equals(tag)) {
if (eventType.equals(eventClass, tag)) {
iterator.remove();
}
}
Expand Down Expand Up @@ -407,7 +439,7 @@ private List<EventType> getMatchedEventTypes(EventType type, Object aEvent) {
if (mCacheEventTypes.containsKey(type)) {
eventTypes = mCacheEventTypes.get(type);
} else {
eventTypes = mMatchPolicy.findMatchEventTypes(type, aEvent);
eventTypes = mMatchPolicy.findMatchEventTypes(type, type.getEventClass());
mCacheEventTypes.put(type, eventTypes);
}

Expand All @@ -431,8 +463,6 @@ private void handleStickyEvent(EventType eventType, Object subscriber) {
// 事件
Object event = eventType.event;
for (EventType foundEventType : eventTypes) {
Log.e("", "### 找到的类型 : " + foundEventType.paramClass.getSimpleName()
+ ", event class : " + event.getClass().getSimpleName());
final List<Subscription> subscriptions = mSubcriberMap.get(foundEventType);
if (subscriptions == null) {
continue;
Expand All @@ -443,8 +473,7 @@ private void handleStickyEvent(EventType eventType, Object subscriber) {
// 如果订阅者为空,那么该sticky事件分发给所有订阅者.否则只分发给该订阅者
if (isTarget(subItem, subscriber)
&& (subItem.eventType.equals(foundEventType)
|| subItem.eventType.paramClass
.isAssignableFrom(foundEventType.paramClass))) {
|| subItem.eventType.isAssignableFromEventClass(foundEventType))) {
// 处理事件
eventHandler.handleEvent(subItem, event);
}
Expand Down
40 changes: 31 additions & 9 deletions src/org/simple/eventbus/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class EventType {
/**
* 参数类型
*/
Class<?> paramClass;
private Class<?> eventClass;
/**
* 函数的tag
*/
Expand All @@ -46,21 +46,21 @@ public EventType(Class<?> aClass) {
this(aClass, DEFAULT_TAG);
}

public EventType(Class<?> aClass, String aTag) {
paramClass = aClass;
tag = aTag;
public EventType(Class<?> eventClass, String tag) {
this.eventClass = eventClass;
this.tag = tag;
}

@Override
public String toString() {
return "EventType [paramClass=" + paramClass.getName() + ", tag=" + tag + "]";
return "EventType [eventClass=" + eventClass.getName() + ", tag=" + tag + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((paramClass == null) ? 0 : paramClass.hashCode());
result = prime * result + ((eventClass == null) ? 0 : eventClass.hashCode());
result = prime * result + ((tag == null) ? 0 : tag.hashCode());
return result;
}
Expand All @@ -74,10 +74,10 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
EventType other = (EventType) obj;
if (paramClass == null) {
if (other.paramClass != null)
if (eventClass == null) {
if (other.eventClass != null)
return false;
} else if (!paramClass.equals(other.paramClass))
} else if (!eventClass.equals(other.eventClass))
return false;
if (tag == null) {
if (other.tag != null)
Expand All @@ -86,5 +86,27 @@ public boolean equals(Object obj) {
return false;
return true;
}

public final boolean equals(Class<?> eventClass, String tag) {
if(this.eventClass == null) {
return eventClass == null;
} else if(!this.eventClass.equals(eventClass)) {
return false;
}
if(this.tag == null) {
return tag == null;
} else if(!this.tag.equals(tag)) {
return false;
}
return true;
}

public final boolean isAssignableFromEventClass(EventType eventType) {
return eventClass == null ? eventClass == eventType.eventClass : eventClass.isAssignableFrom(eventType.eventClass);
}

public Class<?> getEventClass() {
return eventClass;
}

}
7 changes: 2 additions & 5 deletions src/org/simple/eventbus/SubsciberMethodHunter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package org.simple.eventbus;

import android.util.Log;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -75,8 +73,8 @@ public void findSubcribeMethods(Object subscriber) {
// 获取方法参数
Class<?>[] paramsTypeClass = method.getParameterTypes();
// 订阅函数只支持一个参数
if (paramsTypeClass != null && paramsTypeClass.length == 1) {
Class<?> paramType = convertType(paramsTypeClass[0]);
if (paramsTypeClass != null && paramsTypeClass.length <= 1) {
Class<?> paramType = (paramsTypeClass.length == 0 ? null : convertType(paramsTypeClass[0]));
EventType eventType = new EventType(paramType, annotation.tag());
TargetMethod subscribeMethod = new TargetMethod(method, eventType,
annotation.mode());
Expand Down Expand Up @@ -132,7 +130,6 @@ public void removeMethodsFromMap(Object subscriber) {
Object cacheObject = subscription.subscriber.get();
if (isObjectsEqual(cacheObject, subscriber)
|| cacheObject == null) {
Log.d("", "### 移除订阅 " + subscriber.getClass().getName());
foundSubscriptions.add(subscription);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/org/simple/eventbus/handler/DefaultEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.simple.eventbus.Subscription;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* 事件在哪个线程post,事件的接收就在哪个线程
Expand All @@ -34,12 +35,18 @@ public class DefaultEventHandler implements EventHandler {
*/
public void handleEvent(Subscription subscription, Object event) {
if (subscription == null
|| subscription.subscriber.get() == null) {
|| subscription.subscriber.get() == null
|| subscription.targetMethod == null) {
return;
}
try {
// 执行
subscription.targetMethod.invoke(subscription.subscriber.get(), event);
final Method method = subscription.targetMethod;
if(method.getParameterTypes().length == 0) {
method.invoke(subscription.subscriber.get());
} else {
method.invoke(subscription.subscriber.get(), event);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
Expand Down
Loading