diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-05-27 11:38:33 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-05-27 11:38:33 -0400 |
| commit | 7c279747beb43c7e88633a6228a155a30e6834f7 (patch) | |
| tree | 511176048944fa7332dc1a163a6148c46e7c61b3 /israfil-foundation-notification | |
Initial import
Diffstat (limited to 'israfil-foundation-notification')
4 files changed, 467 insertions, 0 deletions
diff --git a/israfil-foundation-notification/pom.xml b/israfil-foundation-notification/pom.xml new file mode 100644 index 0000000..344acda --- /dev/null +++ b/israfil-foundation-notification/pom.xml @@ -0,0 +1,28 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.israfil.foundation</groupId> + <artifactId>israfil-foundation-all</artifactId> + <version>5-SNAPSHOT</version> + </parent> + <artifactId>israfil-foundation-notification</artifactId> + <name>Israfil Foundation Notification System</name> + <version>1.0.1-SNAPSHOT</version> + <url>http://www.israfil.net/projects/foundation/${artifactId}</url> + <inceptionYear>2003</inceptionYear> + <licenses> + <license> + <name>BSD</name> + <distribution>repo</distribution> + <url>http://www.israfil.net/israfil-license-bsd.txt</url> + <comments>A simple open-source license with minimal restrictions</comments> + </license> + </licenses> + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>israfil-foundation-dynamic</artifactId> + <version>1.0.1</version> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/israfil-foundation-notification/src/main/java/net/israfil/foundation/notification/Notification.java b/israfil-foundation-notification/src/main/java/net/israfil/foundation/notification/Notification.java new file mode 100644 index 0000000..acf60b8 --- /dev/null +++ b/israfil-foundation-notification/src/main/java/net/israfil/foundation/notification/Notification.java @@ -0,0 +1,59 @@ +/*
+ * Copyright (c) 2003, 2004, 2005, 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2003, 2004, 2005, 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Notification.java 13 2006-01-27 23:45:36Z cgruber $
+ */
+package net.israfil.foundation.notification;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * An encapsulated notification, used within a NotificationCentre
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class Notification {
+ private static Map<String,Object> EMPTY_INFO = Collections.unmodifiableMap(new HashMap<String,Object>());
+
+ public final Object sender;
+ public final String notification;
+ public final Map<String,Object> info;
+ public Notification(Object sender, String notification) {
+ this(sender,notification,EMPTY_INFO);
+ }
+ public Notification(Object sender, String notification, Map<String,Object> info) {
+ this.sender = sender;
+ this.notification = notification;
+ this.info = info;
+ }
+}
diff --git a/israfil-foundation-notification/src/main/java/net/israfil/foundation/notification/NotificationCentre.java b/israfil-foundation-notification/src/main/java/net/israfil/foundation/notification/NotificationCentre.java new file mode 100644 index 0000000..8261d12 --- /dev/null +++ b/israfil-foundation-notification/src/main/java/net/israfil/foundation/notification/NotificationCentre.java @@ -0,0 +1,200 @@ +/*
+ * Copyright (c) 2003, 2004, 2005, 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2003, 2004, 2005, 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: NotificationCentre.java 125 2006-11-09 15:51:38Z cgruber $
+ */
+package net.israfil.foundation.notification;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import net.israfil.foundation.dynamic.DynamicUtil;
+
+/**
+ * A nextstep-like central notification system.
+ * @author cgruber
+ */
+public class NotificationCentre {
+
+ private static NotificationCentre _shared = new NotificationCentre();
+
+ private Set<ObservationSpec> observers = new HashSet<ObservationSpec>();
+
+ protected NotificationCentre() {
+ }
+
+ public static NotificationCentre defaultCentre() {
+ return _shared;
+ }
+
+ /**
+ * This registers an object as an observer of notifications. The object
+ * registers interest in either a particular named notification, or a
+ * particular object who may issue the notifications, or the union of the
+ * two. The following table lists the possible combinations:
+ * <table>
+ * <tr>
+ * <th>Notification Name</th>
+ * <th>Sender Object</th>
+ * <th>Effect</th>
+ * </tr>
+ * <tr>
+ * <td>null</td>
+ * <td>a sender</td>
+ * <th>Notifications (of any name) from this sender are observed</th>
+ * </tr>
+ * <tr>
+ * <td>notification string</td>
+ * <td>null</td>
+ * <th>Notifications with this name (from any sender) are observed.</th>
+ * </tr>
+ * <tr>
+ * <td>notification string</td>
+ * <td>a sender</td>
+ * <th>Only notifications with this name and originating from this sender are observed</th>
+ * </tr>
+ * <tr>
+ * <td>null</td>
+ * <td>null</td>
+ * <th>All Notifications from any source will be sent (big performance drag)</th>
+ * </tr>
+ * </table>
+ * @param observer The object instance that wants to be registered
+ * @param callbackSignature Must be a method that takes a single Object parameter which will be a Notification at run-time, or a single Notification parameter
+ * @param notification The name of the notification to watch for (if any)
+ * @param sender The name of the object to whose notifications should be paid attention. (if any)
+ */
+ public void addObserver(Object observer,
+ String callbackSignature,
+ String notification,
+ Object sender){
+ if (observer == null) return;
+ if (callbackSignature == null)
+ throw new IllegalArgumentException("Reciever did not pass in a callback method.");
+ if (!checkSignature(observer,callbackSignature))
+ throw new IllegalArgumentException("Reciever does not implement specified callback method.");
+ observers.add(new ObservationSpec(sender,notification, callbackSignature, observer));
+
+ }
+ /** Post a notification with the given nam and notifier. */
+ public void postNotification(Notification notification) {
+ for (ObservationSpec obs : this.observers) {
+ obs.postNotification(notification);
+ }
+ }
+ /** Post a notification with the given name, notifier, and with additional
+ * context information presented in a java.util.Map. */
+ public void postNotification(Object sender,String notification) {
+ postNotification(new Notification(sender,notification));
+ }
+ /** Post a notification with the given name, notifier, and with additional
+ * context information presented in a java.util.Map. */
+ public void postNotification(Object sender,String notification, Map<String,Object> info) {
+ postNotification(new Notification(sender,notification,info));
+ }
+ /** Removes an observer from any notofications with either or both a
+ * provided notification string, or notifier
+ */
+ public void removeObserver(Object observer, String notification, Object sender) {
+ for (ObservationSpec obs : observers) {
+ if (obs.observer == observer &&
+ obs.sender == sender &&
+ (obs.notification == null || obs.notification.equals(notification))){
+ observers.remove(obs);
+ }
+ }
+ }
+ public void clearObservers() {
+ observers.clear();
+ }
+
+ private boolean checkSignature(Object receiver,String selector) {
+ if (!DynamicUtil.respondsTo(receiver,selector)) return false;
+ StringTokenizer st = new StringTokenizer(selector,":");
+ if (st.countTokens() != 2) return false;
+ st.nextToken();
+ String parameter = st.nextToken();
+ if (Notification.class.getName().equals(parameter)) return true;
+ if (Object.class.getName().equals(parameter)) return true;
+ return false;
+ }
+
+ /**
+ * TODO Use WeakReferences
+ */
+ public class ObservationSpec {
+ public final Object sender;
+ public final String notification;
+ public final String callback;
+ public final Object observer;
+ private final int _hashCode;
+ public ObservationSpec(Object sender,String notification,String callback, Object observer) {
+ if (observer == null)
+ throw new IllegalArgumentException("Both sender and notification cannot be null");
+ this.sender = sender;
+ this.notification = notification;
+ this.callback = callback;
+ this.observer = observer;
+
+ // Pre-calculate hash
+ int code = 13;
+ if (sender != null) code *= sender.hashCode();
+ if (notification != null) code *= notification.hashCode();
+ code *= observer.hashCode();
+ this._hashCode = code;
+ }
+ public void postNotification(Notification notification) {
+ if (this.sender == null && this.notification == null) {
+ _post(notification);
+ } else if (this.sender == null) {
+ if (notification.notification.equals(this.notification))
+ _post(notification);
+ } else if (this.notification == null) {
+ if (notification.sender.equals(this.sender))
+ _post(notification);
+ } else {
+ if (notification.notification.equals(this.notification) && notification.sender.equals(this.sender))
+ _post(notification);
+ }
+ }
+ private void _post(Notification notification) {
+ DynamicUtil.performOn(this.observer,callback,new Object[]{notification} );
+ }
+
+ @Override
+ public int hashCode() {
+ return _hashCode;
+ }
+ }
+
+}
diff --git a/israfil-foundation-notification/src/test/java/net/israfil/foundation/notification/NotificationCentreTest.java b/israfil-foundation-notification/src/test/java/net/israfil/foundation/notification/NotificationCentreTest.java new file mode 100644 index 0000000..beb310e --- /dev/null +++ b/israfil-foundation-notification/src/test/java/net/israfil/foundation/notification/NotificationCentreTest.java @@ -0,0 +1,180 @@ +/*
+ * Copyright (c) 2003, 2004, 2005, 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2003, 2004, 2005, 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: NotificationCentreTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.notification;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import net.israfil.foundation.core.Copyright;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author cgruber
+ *
+ * To change the template for this generated type comment go to
+ * Window>Preferences>Java>Code Generation>Code and Comments
+ */
+@Copyright(years={"2003","2004","2005","2006"},owner="Israfil Consulting Services Corporation",license="BSD")
+@Test
+public class NotificationCentreTest{
+
+ private static String NOTIFICATION_1 = "notification1";
+ private static String NOTIFICATION_2 = "notification2";
+ private static String NOTIFICATION_3 = "notification3";
+
+ private static String INFO_KEY = "key";
+ private static String INFO_VALUE = "value";
+
+
+ public boolean didReceiveNotifyWithNotification = false;
+ public boolean didReceiveNotifyWithObject = false;
+ public boolean didReceiveNotifyOtherWithObject = false;
+
+ public Map<String,Object> info = null;
+
+ NotificationCentre centre = null;
+ ObserverFixture observer = null;
+
+ @BeforeMethod
+ protected void setUp() throws Exception {
+ centre = NotificationCentre.defaultCentre();
+ observer = new ObserverFixture();
+ didReceiveNotifyWithNotification = false;
+ didReceiveNotifyWithObject = false;
+ didReceiveNotifyOtherWithObject = false;
+ info = new HashMap<String,Object>();
+ info.put(INFO_KEY,INFO_VALUE);
+ }
+
+ @AfterMethod
+ protected void tearDown() throws Exception {
+ centre.clearObservers();
+ centre = null;
+ observer = null;
+ info = null;
+ }
+
+ /*
+ * Test for void postNotification(Notification)
+ */
+ public void testPostNotificationNotification() {
+ Notification n1 = new Notification(this,NOTIFICATION_1);
+ Notification n2 = new Notification(this,NOTIFICATION_2);
+ Notification n3 = new Notification(this,NOTIFICATION_3);
+ centre.postNotification(n1);
+ Assert.assertFalse(didReceiveNotifyWithNotification);
+ Assert.assertFalse(observer.notification == n1);
+ // register as global
+ centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
+ centre.postNotification(n2);
+ Assert.assertTrue(didReceiveNotifyWithNotification);
+ Assert.assertTrue(observer.notification == n2);
+
+ // register as global
+ centre.removeObserver(observer,null,null);
+ centre.postNotification(n3);
+ Assert.assertFalse(observer.notification == n3);
+ }
+
+ /*
+ * Test for void postNotification(Object, String)
+ */
+ public void testPostNotificationObjectString() {
+ centre.postNotification(this,NOTIFICATION_1);
+ Assert.assertFalse(didReceiveNotifyWithNotification);
+ Assert.assertEquals(null,observer.notification);
+ // register as global
+ centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
+ centre.postNotification(this,NOTIFICATION_2);
+ Assert.assertTrue(didReceiveNotifyWithNotification);
+ Assert.assertEquals(NOTIFICATION_2,observer.notification.notification);
+ }
+
+ /*
+ * Test for void postNotification(Object, String, Map)
+ */
+ public void testPostNotificationObjectStringMap() {
+ centre.postNotification(this,NOTIFICATION_1,info);
+ Assert.assertFalse(didReceiveNotifyWithNotification);
+ Assert.assertEquals(null,observer.notification);
+ // register as global
+ centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
+ centre.postNotification(this,NOTIFICATION_2,info);
+ Assert.assertTrue(didReceiveNotifyWithNotification);
+ Assert.assertEquals(NOTIFICATION_2,observer.notification.notification);
+ Assert.assertEquals(INFO_VALUE,observer.notification.info.get(INFO_KEY));
+ }
+
+ /*
+ * Test for void removeObserver(Object, String, Object)
+ */
+ public void testRemoveObserverObjectStringObject() {
+ // register as global
+ centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
+ // remove right away (we know addObserver works from other tests.
+ centre.removeObserver(observer,null,null);
+ centre.postNotification(this,NOTIFICATION_1,info);
+ Assert.assertEquals(null,observer.notification);
+ }
+
+ public class ObserverFixture {
+ public Notification notification = null;
+ public void notify(Notification n) {
+ NotificationCentreTest sender = (NotificationCentreTest)n.sender;
+ sender.didReceiveNotifyWithNotification = true;
+ this.notification = n;
+ }
+ public void notify(Object o) {
+ Notification n = (Notification)o;
+ NotificationCentreTest sender = (NotificationCentreTest)n.sender;
+ sender.didReceiveNotifyWithObject = true;
+ this.notification = n;
+ }
+ public void notifyOther(Object o) {
+ Notification n = (Notification)o;
+ NotificationCentreTest sender = (NotificationCentreTest)n.sender;
+ sender.didReceiveNotifyWithObject = true;
+ this.notification = n;
+ }
+ public void finalize() throws Throwable {
+ notification = null;
+ super.finalize();
+ }
+ }
+
+}
|
