From b7692f2d8591eb93a4e2cae342e289ff56ab18fe Mon Sep 17 00:00:00 2001 From: Yuri Orlov Date: Sat, 25 Jan 2014 20:14:37 +0300 Subject: [PATCH] simplification and code cleanup --- .../messenger/NotificationCenter.java | 71 +++++++------------ 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java b/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java index 66ab8f68b..7128cafc4 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java @@ -10,58 +10,57 @@ package org.telegram.messenger; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; public class NotificationCenter { - final private HashMap> observers = new HashMap>(); - final private HashMap memCache = new HashMap(); - final private HashMap memCacheString = new HashMap(); + + public static NotificationCenter Instance = new NotificationCenter(); + + private final Map> observers = new HashMap>(); + + private Map memCache = new HashMap(); + private Map removeAfterBroadcast = new HashMap(); + private boolean broadcasting = false; - final private HashMap removeAfterBroadcast = new HashMap(); public interface NotificationCenterDelegate { public abstract void didReceivedNotification(int id, Object... args); } - public static NotificationCenter Instance = new NotificationCenter(); - public void addToMemCache(int id, Object object) { - memCache.put(id, object); + addToMemCache(String.valueOf(id), object); } public void addToMemCache(String id, Object object) { - memCacheString.put(id, object); + memCache.put(id, object); } public Object getFromMemCache(int id) { - Object obj = memCache.get(id); - if (obj != null) { - memCache.remove(id); - } - return obj; + return getFromMemCache(String.valueOf(id), null); } public Object getFromMemCache(String id, Object defaultValue) { - Object obj = memCacheString.get(id); + Object obj = memCache.get(id); if (obj != null) { - memCacheString.remove(id); - } else { - return defaultValue; + memCache.remove(id); + return obj; } - return obj; + return defaultValue; } public void postNotificationName(int id, Object... args) { synchronized (observers) { broadcasting = true; - ArrayList objects = observers.get(id); + List objects = observers.get(id); if (objects != null) { for (Object obj : objects) { - ((NotificationCenterDelegate)obj).didReceivedNotification(id, args); + ((NotificationCenterDelegate) obj).didReceivedNotification(id, args); } } broadcasting = false; if (!removeAfterBroadcast.isEmpty()) { - for (HashMap.Entry entry : removeAfterBroadcast.entrySet()) { + for (Map.Entry entry : removeAfterBroadcast.entrySet()) { removeObserver(entry.getValue(), entry.getKey()); } removeAfterBroadcast.clear(); @@ -71,44 +70,26 @@ public class NotificationCenter { public void addObserver(Object observer, int id) { synchronized (observers) { - ArrayList objects = observers.get(id); - if (objects == null) { - objects = new ArrayList(); - observers.put(id, objects); + if (!observers.containsKey(id)) { + observers.put(id, new ArrayList()); } + + List objects = observers.get(id); if (objects.contains(observer)) { return; } + objects.add(observer); } } -// public void removeObserver(Object observer) { -// synchronized (observers) { -// if (broadcasting) { -// removeAfterBroadcast.put(-1, observer); -// return; -// } -// Integer[] keyArr = (Integer[])observers.keySet().toArray(); -// for (int a = 0; a < observers.size(); a++) { -// Integer id = keyArr[a]; -// ArrayList objects = observers.get(id); -// objects.remove(observer); -// if (objects.size() == 0) { -// observers.remove(id); -// a--; -// } -// } -// } -// } - public void removeObserver(Object observer, int id) { synchronized (observers) { if (broadcasting) { removeAfterBroadcast.put(id, observer); return; } - ArrayList objects = observers.get(id); + List objects = observers.get(id); if (objects != null) { objects.remove(observer); if (objects.size() == 0) {