1 31 32 package org.antlr.xjlib.foundation.notification; 33 34 import java.util.*; 35 36 public class XJNotificationCenter { 37 38 private static XJNotificationCenter defaultCenter = new XJNotificationCenter(); 39 40 private Map<String ,List<XJNotificationObserver>> observers = new HashMap<String , List<XJNotificationObserver>>(); 41 42 public static XJNotificationCenter defaultCenter() { 43 return defaultCenter; 44 } 45 46 public void addObserver(XJNotificationObserver observer, String notifName) { 47 List<XJNotificationObserver> obs = observers.get(notifName); 48 if(obs == null) { 49 obs = new ArrayList<XJNotificationObserver>(); 50 observers.put(notifName, obs); 51 } 52 obs.add(observer); 53 } 54 55 public void removeObserver(XJNotificationObserver observer) { 56 for (Iterator<String > iterator = observers.keySet().iterator(); iterator.hasNext();) { 57 String notifName = iterator.next(); 58 List<XJNotificationObserver> obs = observers.get(notifName); 59 for(int i=obs.size()-1; i>=0; i--) { 60 XJNotificationObserver candidate = obs.get(i); 61 if(candidate.equals(observer)) { 62 obs.remove(i); 63 } 64 } 65 } 66 } 67 68 public void postNotification(Object source, String notifName) { 69 List<XJNotificationObserver> obs = observers.get(notifName); 70 if(obs == null) 71 return; 72 73 for (Iterator<XJNotificationObserver> iterator = obs.iterator(); iterator.hasNext();) { 74 XJNotificationObserver observer = iterator.next(); 75 observer.notificationFire(source, notifName); 76 } 77 } 78 } 79 | Popular Tags |