Changed removed behavior back to orig. Added isEmpty() method to satisfy requirement

This commit is contained in:
nathan 2015-02-07 22:31:09 +01:00
parent 45fbc56320
commit f1d8ae0e6a
4 changed files with 17 additions and 21 deletions

View File

@ -66,6 +66,10 @@ public abstract class AbstractConcurrentSet<T> implements IConcurrentSet<T> {
return this.entries.size();
}
public boolean isEmpty() {
return this.head == null;
}
@Override
public void addAll(Iterable<T> elements) {
Lock writeLock = this.lock.writeLock();
@ -82,22 +86,14 @@ public abstract class AbstractConcurrentSet<T> implements IConcurrentSet<T> {
}
/**
* The return on this is DIFFERENT than normal.
*
* @return TRUE if there are no more elements (aka: this set is now empty)
* @return TRUE if the element was successfully removed
*/
@Override
public boolean remove(T element) {
if (!contains(element)) {
// return quickly
Lock readLock = this.lock.readLock();
readLock.lock();
boolean headIsNull = this.head == null;
readLock.unlock();
return headIsNull;
return false;
} else {
boolean wasLastElement = false;
Lock writeLock = this.lock.writeLock();
try {
writeLock.lock();
@ -113,14 +109,10 @@ public abstract class AbstractConcurrentSet<T> implements IConcurrentSet<T> {
//oldHead.clear(); // optimize for GC not possible because of potentially running iterators
}
this.entries.remove(element);
if (this.head == null) {
wasLastElement = true;
}
} finally {
writeLock.unlock();
}
return wasLastElement;
return true;
}
}

View File

@ -14,12 +14,12 @@ public interface IConcurrentSet<T> extends Iterable<T> {
int size();
boolean isEmpty();
void addAll(Iterable<T> elements);
/**
* The return on this is DIFFERENT than normal.
*
* @return TRUE if there are no more elements (aka: this set is now empty)
* @return TRUE if the element was removed
*/
boolean remove(T element);
}

View File

@ -110,13 +110,16 @@ public class Subscription {
/**
* @return TRUE if there are no listeners subscribed
* @return TRUE if the element was removed
*/
public boolean unsubscribe(Object existingListener) {
// TRUE if there are no more elements (aka: this set is empty)
return this.listeners.remove(existingListener);
}
public boolean isEmpty() {
return this.listeners.isEmpty();
}
public int size() {
return this.listeners.size();
}

View File

@ -73,8 +73,9 @@ public class SubscriptionManager {
if (subscriptions != null) {
for (Subscription subscription : subscriptions) {
boolean isEmpty = subscription.unsubscribe(listener);
subscription.unsubscribe(listener);
boolean isEmpty = subscription.isEmpty();
if (isEmpty) {
// single or multi?
Class<?>[] handledMessageTypes = subscription.getHandledMessageTypes();