Code polish

This commit is contained in:
nathan 2018-03-31 16:03:52 +02:00
parent 7e32a2ff5b
commit ab174e4e0e

View File

@ -102,19 +102,14 @@ class OnMessageReceivedManager<C extends Connection> {
final Class<?> type = identifyType(listener); final Class<?> type = identifyType(listener);
synchronized (this) { synchronized (this) {
// access a snapshot of the listeners (single-writer-principle)
final IdentityMap<Type, ConcurrentIterator> listeners = REF.get(this);
ConcurrentIterator subscribedListeners = listeners.get(type); ConcurrentIterator subscribedListeners = listeners.get(type);
if (subscribedListeners == null) { if (subscribedListeners == null) {
subscribedListeners = new ConcurrentIterator(); subscribedListeners = new ConcurrentIterator();
listeners.put(type, subscribedListeners); listeners.put(type, subscribedListeners);
} }
//noinspection unchecked
subscribedListeners.add(listener); subscribedListeners.add(listener);
// save this snapshot back to the original (single writer principle)
REF.lazySet(this, listeners);
} }
} }
@ -130,18 +125,14 @@ class OnMessageReceivedManager<C extends Connection> {
int size = -1; // default is "not found" int size = -1; // default is "not found"
synchronized (this) { synchronized (this) {
// access a snapshot of the listeners (single-writer-principle) // access a snapshot of the listeners (single-writer-principle)
final IdentityMap<Type, ConcurrentIterator> listeners = REF.get(this);
final ConcurrentIterator concurrentIterator = listeners.get(type); final ConcurrentIterator concurrentIterator = listeners.get(type);
if (concurrentIterator != null) { if (concurrentIterator != null) {
//noinspection unchecked
boolean removed = concurrentIterator.remove(listener); boolean removed = concurrentIterator.remove(listener);
if (removed) { if (removed) {
size = concurrentIterator.size(); size = concurrentIterator.size();
} }
} }
// save this snapshot back to the original (single writer principle)
REF.lazySet(this, listeners);
} }
return size; return size;
@ -237,11 +228,6 @@ class OnMessageReceivedManager<C extends Connection> {
return found; return found;
} }
public synchronized
void removeAll() {
listeners.clear();
}
/** /**
* @return true if the listener was removed, false otherwise * @return true if the listener was removed, false otherwise
*/ */
@ -249,24 +235,16 @@ class OnMessageReceivedManager<C extends Connection> {
boolean removeAll(final Class<?> classType) { boolean removeAll(final Class<?> classType) {
boolean found; boolean found;
// access a snapshot of the listeners (single-writer-principle)
final IdentityMap<Type, ConcurrentIterator> listeners = REF.get(this);
found = listeners.remove(classType) != null; found = listeners.remove(classType) != null;
// save this snapshot back to the original (single writer principle)
REF.lazySet(this, listeners);
return found; return found;
} }
/** /**
* called on shutdown * called on shutdown
*/ */
public public synchronized
void clear() { void clear() {
final IdentityMap<Type, ConcurrentIterator> listeners = REF.get(this);
// The iterators for this map are NOT THREAD SAFE! // The iterators for this map are NOT THREAD SAFE!
// using .entries() is what we are supposed to use! // using .entries() is what we are supposed to use!
final IdentityMap.Entries<Type, ConcurrentIterator> entries = listeners.entries(); final IdentityMap.Entries<Type, ConcurrentIterator> entries = listeners.entries();
@ -277,8 +255,5 @@ class OnMessageReceivedManager<C extends Connection> {
} }
listeners.clear(); listeners.clear();
// save this snapshot back to the original (single writer principle)
REF.lazySet(this, listeners);
} }
} }