Fixed warning in WeakConcurrentSet

This commit is contained in:
nathan 2015-02-04 02:36:28 +01:00
parent e0b9d8addb
commit d46a773286

View File

@ -23,25 +23,26 @@ public class WeakConcurrentSet<T> extends AbstractConcurrentSet<T>{
super(new WeakHashMap<T, ISetEntry<T>>()); super(new WeakHashMap<T, ISetEntry<T>>());
} }
@Override
public Iterator<T> iterator() { public Iterator<T> iterator() {
return new Iterator<T>() { return new Iterator<T>() {
// the current listelement of this iterator // the current listelement of this iterator
// used to keep track of the iteration process // used to keep track of the iteration process
private ISetEntry<T> current = head; private ISetEntry<T> current = WeakConcurrentSet.this.head;
// this method will remove all orphaned entries // this method will remove all orphaned entries
// until it finds the first entry whose value has not yet been garbage collected // until it finds the first entry whose value has not yet been garbage collected
// the method assumes that the current element is already orphaned and will remove it // the method assumes that the current element is already orphaned and will remove it
private void removeOrphans(){ private void removeOrphans(){
Lock writelock = lock.writeLock(); Lock writelock = WeakConcurrentSet.this.lock.writeLock();
try{ try{
writelock.lock(); writelock.lock();
do { do {
ISetEntry orphaned = current; ISetEntry<T> orphaned = this.current;
current = current.next(); this.current = this.current.next();
orphaned.remove(); orphaned.remove();
} while(current != null && current.getValue() == null); } while(this.current != null && this.current.getValue() == null);
} }
finally { finally {
writelock.unlock(); writelock.unlock();
@ -49,40 +50,45 @@ public class WeakConcurrentSet<T> extends AbstractConcurrentSet<T>{
} }
@Override
public boolean hasNext() { public boolean hasNext() {
if (current == null) return false; if (this.current == null) {
if (current.getValue() == null) { return false;
}
if (this.current.getValue() == null) {
// trigger removal of orphan references // trigger removal of orphan references
// because a null value indicates that the value has been garbage collected // because a null value indicates that the value has been garbage collected
removeOrphans(); removeOrphans();
return current != null; // if any entry is left then it will have a value return this.current != null; // if any entry is left then it will have a value
} else { } else {
return true; return true;
} }
} }
@Override
public T next() { public T next() {
if (current == null) { if (this.current == null) {
return null; return null;
} }
T value = current.getValue(); T value = this.current.getValue();
if (value == null) { // auto-removal of orphan references if (value == null) { // auto-removal of orphan references
removeOrphans(); removeOrphans();
return next(); return next();
} else { } else {
current = current.next(); this.current = this.current.next();
return value; return value;
} }
} }
@Override
public void remove() { public void remove() {
//throw new UnsupportedOperationException("Explicit removal of set elements is only allowed via the controlling set. Sorry!"); //throw new UnsupportedOperationException("Explicit removal of set elements is only allowed via the controlling set. Sorry!");
if (current == null) { if (this.current == null) {
return; return;
} }
ISetEntry<T> newCurrent = current.next(); ISetEntry<T> newCurrent = this.current.next();
WeakConcurrentSet.this.remove(current.getValue()); WeakConcurrentSet.this.remove(this.current.getValue());
current = newCurrent; this.current = newCurrent;
} }
}; };
} }
@ -109,7 +115,7 @@ public class WeakConcurrentSet<T> extends AbstractConcurrentSet<T>{
@Override @Override
public T getValue() { public T getValue() {
return value.get(); return this.value.get();
} }