summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java
diff options
context:
space:
mode:
Diffstat (limited to 'projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java')
-rw-r--r--projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java70
1 files changed, 39 insertions, 31 deletions
diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java
index cf0af53..862d454 100644
--- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java
+++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java
@@ -60,13 +60,13 @@ public class NSNotificationCenter {
/**
* A Map of (name,object) pairs to a List of (observer,selector) pairs.
*/
- private Hashtable observers; // thread-safe
+ private Hashtable<CompoundKey, List<CompoundValue>> observers; // thread-safe
/**
* Default constructor creates a new notification center.
*/
public NSNotificationCenter() {
- observers = new Hashtable();
+ observers = new Hashtable<>();
}
/**
@@ -81,7 +81,7 @@ public class NSNotificationCenter {
}
/**
- * Addes the specified observer to the notification queue for notifications with
+ * Adds the specified observer to the notification queue for notifications with
* the specified name or the specified object or both.
*
* @param anObserver The observer that wishes to be notified.
@@ -105,12 +105,12 @@ public class NSNotificationCenter {
if (anObject == null) {
anObject = NullMarker;
}
- Object key = new CompoundKey(name, anObject);
- Object value = new CompoundValue(anObserver, aSelector);
- List list = (List) observers.get(key);
+ CompoundKey key = new CompoundKey(name, anObject);
+ CompoundValue value = new CompoundValue(anObserver, aSelector);
+ List<CompoundValue> list = observers.get(key);
if (list == null) {
// create new list with value and put it in map
- list = new Vector(); // thread-safe
+ list = new Vector<>(); // thread-safe
list.add(value);
observers.put(new CompoundKey(name, anObject, keyQueue), list);
} else {
@@ -129,8 +129,9 @@ public class NSNotificationCenter {
* selector.
*/
public void postNotification(NSNotification aNotification) {
- List mergedList = new LinkedList();
- Object key, observerList;
+ List<CompoundValue> mergedList = new LinkedList<>();
+ CompoundKey key;
+ List<CompoundValue> observerList;
Object name = aNotification.name();
Object object = aNotification.object();
@@ -139,39 +140,39 @@ public class NSNotificationCenter {
if (object != null) { // both are specified
observerList = observers.get(new CompoundKey(name, object));
if (observerList != null) {
- mergedList.addAll((List) observerList);
+ mergedList.addAll(observerList);
}
observerList = observers.get(new CompoundKey(name, NullMarker));
if (observerList != null) {
- mergedList.addAll((List) observerList);
+ mergedList.addAll(observerList);
}
observerList = observers.get(new CompoundKey(NullMarker, object));
if (observerList != null) {
- mergedList.addAll((List) observerList);
+ mergedList.addAll(observerList);
}
} else { // object is null
observerList = observers.get(new CompoundKey(name, NullMarker));
if (observerList != null) {
- mergedList.addAll((List) observerList);
+ mergedList.addAll(observerList);
}
}
} else if (object != null) { // name is null
observerList = observers.get(new CompoundKey(NullMarker, object));
if (observerList != null) {
- mergedList.addAll((List) observerList);
+ mergedList.addAll(observerList);
}
}
key = new CompoundKey(NullMarker, NullMarker);
observerList = observers.get(key);
if (observerList != null) {
- mergedList.addAll((List) observerList);
+ mergedList.addAll(observerList);
}
CompoundValue value;
- Iterator it = mergedList.iterator();
+ Iterator<CompoundValue> it = mergedList.iterator();
while (it.hasNext()) {
- value = (CompoundValue) it.next();
+ value = it.next();
if (value.get() == null) {
it.remove();
} else {
@@ -182,7 +183,7 @@ public class NSNotificationCenter {
"Error notifying object: " + value.get() + " : " + aNotification, exc);
// throw w;
w.printStackTrace();
- postNotification("Error notifying object", this, new NSDictionary("exception", w));
+ postNotification("Error notifying object", this, new NSDictionary<>("exception", w));
}
}
}
@@ -212,7 +213,7 @@ public class NSNotificationCenter {
* originator of the notification and that may be of
* interest to a knowledgable observer.
*/
- public void postNotification(String notificationName, Object anObject, Map userInfo) {
+ public void postNotification(String notificationName, Object anObject, Map<String, Object> userInfo) {
postNotification(new NSNotification(notificationName, anObject, userInfo));
}
@@ -226,7 +227,7 @@ public class NSNotificationCenter {
// remove freed objects
processKeyQueue();
- Iterator it = new LinkedList(observers.keySet()).iterator();
+ Iterator<CompoundKey> it = new LinkedList<>(observers.keySet()).iterator();
while (it.hasNext()) {
removeObserver(anObserver, it.next());
}
@@ -253,10 +254,10 @@ public class NSNotificationCenter {
processKeyQueue();
// get key matches
- List keys = matchingKeys(notificationName, anObject);
+ List<CompoundKey> keys = matchingKeys(notificationName, anObject);
// remove specified observer from each matching key
- Iterator it = keys.iterator();
+ Iterator<CompoundKey> it = keys.iterator();
while (it.hasNext()) {
removeObserver(anObserver, it.next());
}
@@ -267,12 +268,12 @@ public class NSNotificationCenter {
* null parameters are considered wildcards. Pass NullMarkers if you want to
* explicitly match nulls.
*/
- private List matchingKeys(String name, Object object) {
- List result = new LinkedList();
+ private List<CompoundKey> matchingKeys(String name, Object object) {
+ List<CompoundKey> result = new LinkedList<>();
boolean willAdd;
CompoundKey key;
- Iterator it = observers.keySet().iterator();
+ Iterator<CompoundKey> it = observers.keySet().iterator();
while (it.hasNext()) {
key = (CompoundKey) it.next();
willAdd = false;
@@ -299,15 +300,15 @@ public class NSNotificationCenter {
return;
}
- List list = (List) observers.get(key);
+ List<CompoundValue> list = observers.get(key);
if (list == null)
return;
// remove specified observer from list
Object observer;
- Iterator it = list.iterator();
+ Iterator<CompoundValue> it = list.iterator();
while (it.hasNext()) {
- observer = ((CompoundValue) it.next()).get();
+ observer = it.next().get();
if ((observer == null) || (anObserver == observer)) {
// remove if match or freed object
it.remove();
@@ -321,7 +322,7 @@ public class NSNotificationCenter {
}
/* Reference queues for cleared WeakKeys */
- private ReferenceQueue keyQueue = new ReferenceQueue();
+ private ReferenceQueue keyQueue = new ReferenceQueue<>();
/**
* Removes any keys whose object has been garbage collected. (Garbage collected
@@ -339,7 +340,7 @@ public class NSNotificationCenter {
* Key combining a name with an object. The object is weakly referenced, and
* keys are deallocated by reference queue. equals() compares by reference.
*/
- private static class CompoundKey extends WeakReference {
+ private static class CompoundKey extends WeakReference<Object> {
private Object name;
private int hashCode;
@@ -357,6 +358,7 @@ public class NSNotificationCenter {
* Creates compound key with queue. Neither name nor object may be null. Use
* NullMarker to represent null in either name or object.
*/
+ @SuppressWarnings({ "rawtypes", "unchecked" })
public CompoundKey(Object aName, Object anObject, ReferenceQueue aQueue) {
super(anObject, aQueue);
name = aName;
@@ -367,10 +369,12 @@ public class NSNotificationCenter {
return name;
}
+ @Override
public int hashCode() {
return hashCode;
}
+ @Override
public boolean equals(Object anObject) {
if (this == anObject)
return true;
@@ -388,6 +392,7 @@ public class NSNotificationCenter {
return false;
}
+ @Override
public String toString() {
return "[CompoundKey:" + name() + ":" + get() + "]";
}
@@ -397,7 +402,7 @@ public class NSNotificationCenter {
* Value combining an object with a selector. The object is weakly referenced,
* and null values are not allowed.
*/
- private static class CompoundValue extends WeakReference {
+ private static class CompoundValue extends WeakReference<Object> {
private NSSelector selector;
private int hashCode;
@@ -411,10 +416,12 @@ public class NSNotificationCenter {
return selector;
}
+ @Override
public int hashCode() {
return hashCode;
}
+ @Override
public boolean equals(Object anObject) {
if (this == anObject)
return true;
@@ -431,6 +438,7 @@ public class NSNotificationCenter {
return false;
}
+ @Override
public String toString() {
return "[CompoundValue:" + get() + ":" + selector().name() + "]";
}