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(); return this.entries.size();
} }
public boolean isEmpty() {
return this.head == null;
}
@Override @Override
public void addAll(Iterable<T> elements) { public void addAll(Iterable<T> elements) {
Lock writeLock = this.lock.writeLock(); 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 the element was successfully removed
*
* @return TRUE if there are no more elements (aka: this set is now empty)
*/ */
@Override @Override
public boolean remove(T element) { public boolean remove(T element) {
if (!contains(element)) { if (!contains(element)) {
// return quickly // return quickly
Lock readLock = this.lock.readLock(); return false;
readLock.lock();
boolean headIsNull = this.head == null;
readLock.unlock();
return headIsNull;
} else { } else {
boolean wasLastElement = false;
Lock writeLock = this.lock.writeLock(); Lock writeLock = this.lock.writeLock();
try { try {
writeLock.lock(); 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 //oldHead.clear(); // optimize for GC not possible because of potentially running iterators
} }
this.entries.remove(element); this.entries.remove(element);
if (this.head == null) {
wasLastElement = true;
}
} finally { } finally {
writeLock.unlock(); writeLock.unlock();
} }
return wasLastElement; return true;
} }
} }

View File

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

View File

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