From 2268d28bb43c2b3d7b4dfd96d35c0a844390c5bd Mon Sep 17 00:00:00 2001
From: endian11 <Dana_Lee1016@126.com>
Date: 星期四, 02 一月 2020 14:56:09 +0800
Subject: [PATCH] 完成时间分发库的编写,测试正常
---
app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventObjPool.java | 25 +++++
app/src/main/java/safeluck/drive/evaluation/cEventCenter/ObjectPool.java | 30 +++++
app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventCenter.java | 202 ++++++++++++++++++++++++++++++++++++++++
app/src/main/java/safeluck/drive/evaluation/cEventCenter/ICEventListener.java | 23 ++++
4 files changed, 276 insertions(+), 4 deletions(-)
diff --git a/app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventCenter.java b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventCenter.java
new file mode 100644
index 0000000..f87b93c
--- /dev/null
+++ b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventCenter.java
@@ -0,0 +1,202 @@
+package safeluck.drive.evaluation.cEventCenter;
+
+import android.text.TextUtils;
+import android.util.Log;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * MyApplication2
+ * Created by lzw on 2020/1/2. 14:03:04
+ * 閭锛�632393724@qq.com
+ * All Rights Saved! Chongqing AnYun Tech co. LTD
+ */
+public class CEventCenter {
+
+ private static final String TAG = CEventCenter.class.getSimpleName();
+
+ /**
+ * 鐩戝惉鍣ㄥ垪琛紝鏀寔涓�瀵瑰瀛樺偍
+ */
+ private static final HashMap<String , Object> LISTENER_MAP = new HashMap<>();
+
+ /**
+ * 鐩戝惉鍣ㄥ垪琛ㄩ攣
+ */
+ private static final Object LISTENER_LOCK = new Object();
+
+ /**
+ * 浜嬩欢瀵硅薄姹�
+ */
+ private static final CEventObjPool POOL = new CEventObjPool(5);
+
+ /**
+ * 娉ㄥ唽/娉ㄩ攢鐩戝惉鍣�
+ * @param toBind true娉ㄥ唽 false娉ㄩ攢
+ * @param listener 鐩戝惉鍣�
+ * @param topic 鍗曚釜涓婚
+ */
+ public static void onBindEvent(boolean toBind,ICEventListener listener,String topic){
+ onBindEvent(toBind,listener,new String[]{topic});
+ }
+
+ /**
+ *
+ * @param toBind
+ * @param listener
+ * @param topics 澶氫釜涓婚
+ */
+ public static void onBindEvent(boolean toBind,ICEventListener listener,String[] topics){
+ if (toBind){
+ registerEventListener(listener, topics);
+ }else{
+ unregisterEventListener(listener, topics);
+ }
+ }
+
+ /**
+ * 娉ㄥ唽鐩戝惉鍣�
+ * @param listener
+ * @param topic 鍗曚釜涓婚
+ */
+ public static void registerEventListener(ICEventListener listener, String topic){
+ registerEventListener(listener,new String[]{topic});
+ }
+
+ /**
+ * 娉ㄩ攢鐩戝惉鍣�
+ * @param listener
+ * @param topic 鍗曚釜涓富棰�
+ */
+ private static void unregisterEventListener(ICEventListener listener, String topic) {
+ unregisterEventListener(listener,new String[]{topic});
+ }
+
+ /**
+ * 娉ㄩ攢鐩戝惉鍣�
+ * @param listener
+ * @param topics 澶氫釜涓婚
+ */
+ private static void unregisterEventListener(ICEventListener listener, String[] topics) {
+ if (null == listener || null == topics){
+ return;
+ }
+ synchronized (LISTENER_LOCK){
+ for (String topic :
+ topics) {
+ if (TextUtils.isEmpty(topic)) {
+ continue;
+ }
+ Object obj = LISTENER_MAP.get(topic);
+ if (null == obj){
+ continue;
+ }else if (obj instanceof ICEventListener){
+ //鏈変竴涓洃鍚櫒
+ if (obj == listener){
+ LISTENER_MAP.remove(topic);
+ }
+ }else if (obj instanceof List){
+ //鏈夊涓洃鍚櫒
+ LinkedList<ICEventListener> listeners = (LinkedList<ICEventListener>)obj;
+ listeners.remove(listener);
+ }
+ }
+ }
+ }
+ /**
+ * 娉ㄥ唽鐩戝惉鍣�
+ * @param listener
+ * @param topics 澶氫釜涓婚
+ */
+ private static void registerEventListener(ICEventListener listener, String[] topics) {
+ if (null == listener || null == topics){
+ return;
+ }
+ synchronized (LISTENER_LOCK){
+ for (String topic :
+ topics) {
+ if (TextUtils.isEmpty(topic)){
+ continue;
+ }
+ Object obj = LISTENER_MAP.get(topic);
+ if (null == obj){
+ //杩樻病鏈夌洃鍚櫒锛岀洿鎺ユ斁鍒癕ap闆嗗悎
+ LISTENER_MAP.put(topic,listener);
+ }else if (obj instanceof ICEventListener){
+ //鏈変竴涓洃鍚櫒
+ ICEventListener oldListener = (ICEventListener)obj;
+ if (listener == oldListener){
+ //鍘婚噸
+ continue;
+ }
+
+ LinkedList<ICEventListener> list = new LinkedList<>();
+ list.add(oldListener);
+ list.add(listener);
+ LISTENER_MAP.put(topic,list);
+ }else if (obj instanceof List){
+ //鏈夊涓洃鍚櫒
+ LinkedList<ICEventListener> listeners = (LinkedList<ICEventListener>)obj;
+ if (listeners.indexOf(listener) >=0){
+ //鍘婚噸
+ continue;
+ }
+ listeners.add(listener);
+ }
+ }
+ }
+ }
+
+ public static void dispatchEvent(String topic, int msgCode, int resultCode, Object object){
+ if (!TextUtils.isEmpty(topic)){
+ CEvent event = POOL.get();
+ event.setMsgCode(msgCode);
+ event.setTopic(topic);
+ event.setResultCode(resultCode);
+ event.setObj(object);
+
+ dispatchEvent(event);
+ }
+ }
+
+ private static void dispatchEvent(CEvent event){
+ if (LISTENER_MAP.size() == 0){
+ return;
+ }
+ if (null != event && !TextUtils.isEmpty(event.getTopic())){
+ String topic = event.getTopic();
+
+ //閫氱煡浜嬩欢鐩戝惉鍣ㄥ鐞嗕簨浠�
+ ICEventListener listener = null;
+ LinkedList<ICEventListener> listeners = null;
+ synchronized (LISTENER_LOCK){
+ Log.d(TAG, "dispatchEvent | topic="+topic+"\tmsgCode="+event.getMsgCode()+"\tresultCode="+event.getResultCode()
+ +"\tobj="+event.getObj());
+ Object obj = LISTENER_MAP.get(topic);
+ if (obj == null){
+ return;
+ }
+ if (obj instanceof ICEventListener){
+ listener = (ICEventListener)obj;
+ }else if (obj instanceof List){
+ listeners = (LinkedList<ICEventListener>)((LinkedList)obj).clone();
+ }
+ }
+
+ if (null != listener){
+ listener.onCEvent(topic,event.getMsgCode(),event.getResultCode(),event.getObj());
+ }else if (null != listeners && listeners.size()>0){
+ for (ICEventListener
+ l:
+ listeners) {
+ l.onCEvent(topic, event.getMsgCode(), event.getResultCode(), event.getObj());
+ }
+ }
+ //鎶婂璞℃斁鍥炴睜閲�
+ POOL.returnObj(event);
+ }
+ }
+
+}
diff --git a/app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventObjPool.java b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventObjPool.java
new file mode 100644
index 0000000..ce514ea
--- /dev/null
+++ b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/CEventObjPool.java
@@ -0,0 +1,25 @@
+package safeluck.drive.evaluation.cEventCenter;
+
+/**
+ * 浜嬩欢瀵硅薄姹�
+ * MyApplication2
+ * Created by lzw on 2020/1/2. 13:58:00
+ * 閭锛�632393724@qq.com
+ * All Rights Saved! Chongqing AnYun Tech co. LTD
+ */
+public class CEventObjPool extends ObjectPool<CEvent>{
+
+ public CEventObjPool(int capacity) {
+ super(capacity);
+ }
+
+ @Override
+ protected CEvent[] createObjPool(int capacity) {
+ return new CEvent[capacity];
+ }
+
+ @Override
+ protected CEvent createNewObj() {
+ return new CEvent();
+ }
+}
diff --git a/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ICEventListener.java b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ICEventListener.java
new file mode 100644
index 0000000..e24ab31
--- /dev/null
+++ b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ICEventListener.java
@@ -0,0 +1,23 @@
+package safeluck.drive.evaluation.cEventCenter;
+
+/**
+ * 浜嬩欢鐩戝惉鍣�
+ * MyApplication2
+ * Created by lzw on 2020/1/2. 14:00:14
+ * 閭锛�632393724@qq.com
+ * All Rights Saved! Chongqing AnYun Tech co. LTD
+ */
+public interface ICEventListener {
+
+ /**
+ * 浜嬩欢鍥炶皟鍑芥暟
+ *
+ * 濡傛灉obj浣跨敤浜嗗璞℃睜
+ * 閭d箞浜嬩欢瀹屾垚鍚庯紝obj鍗宠嚜鍔ㄤ細鍙楀埌瀵硅薄姹狅紝璇蜂笉瑕佸啀鍏朵粬绾跨▼缁х画浣跨敤锛屽惁鍒欏彲鑳戒細瀵艰嚧鏁版嵁涓嶆甯�
+ * @param topic
+ * @param msgCode
+ * @param resultCode
+ * @param obj
+ */
+ void onCEvent(String topic, int msgCode, int resultCode, Object obj);
+}
diff --git a/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ObjectPool.java b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ObjectPool.java
index 3eafce0..acaa4e5 100644
--- a/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ObjectPool.java
+++ b/app/src/main/java/safeluck/drive/evaluation/cEventCenter/ObjectPool.java
@@ -8,12 +8,20 @@
* All Rights Saved! Chongqing AnYun Tech co. LTD
*/
public abstract class ObjectPool<T extends PooledObject> {
+ /**
+ * 瀵硅薄瀹瑰櫒
+ */
+ private T[] mContainer;
- private T[] mContainer;//瀵硅薄瀹瑰櫒
+ /**
+ * 瀵硅薄閿�
+ */
+ private final Object LOCK = new Object();
- private final Object LOCK = new Object();//瀵硅薄閿�
-
- private int length;//姣忔杩斿洖瀵硅薄閮芥斁鍒版暟鎹湯绔紝length琛ㄧず鍓嶉潰鍙敤瀵硅薄鏁�
+ /***
+ * 姣忔杩斿洖瀵硅薄閮芥斁鍒版暟鎹湯绔紝length琛ㄧず鍓嶉潰鍙敤瀵硅薄鏁�
+ */
+ private int length;
public ObjectPool(int capacity) {
mContainer = createObjPool(capacity);
@@ -60,4 +68,18 @@
}
return obj;
}
+
+ /**
+ * 鎶婂璞℃斁鍥炴睜閲岄潰
+ * @param obj
+ */
+ public final void returnObj(T obj){
+ synchronized (LOCK){
+ int size = mContainer.length;
+ if (length < size){
+ mContainer[length] = obj;
+ length++;
+ }
+ }
+ }
}
--
Gitblit v1.8.0