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>>());
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
// the current listelement of this iterator
// 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
// 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
private void removeOrphans(){
Lock writelock = lock.writeLock();
Lock writelock = WeakConcurrentSet.this.lock.writeLock();
try{
writelock.lock();
do {
ISetEntry orphaned = current;
current = current.next();
ISetEntry<T> orphaned = this.current;
this.current = this.current.next();
orphaned.remove();
} while(current != null && current.getValue() == null);
} while(this.current != null && this.current.getValue() == null);
}
finally {
writelock.unlock();
@ -49,40 +50,45 @@ public class WeakConcurrentSet<T> extends AbstractConcurrentSet<T>{
}
@Override
public boolean hasNext() {
if (current == null) return false;
if (current.getValue() == null) {
if (this.current == null) {
return false;
}
if (this.current.getValue() == null) {
// trigger removal of orphan references
// because a null value indicates that the value has been garbage collected
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 {
return true;
}
}
@Override
public T next() {
if (current == null) {
if (this.current == null) {
return null;
}
T value = current.getValue();
T value = this.current.getValue();
if (value == null) { // auto-removal of orphan references
removeOrphans();
return next();
} else {
current = current.next();
this.current = this.current.next();
return value;
}
}
@Override
public void remove() {
//throw new UnsupportedOperationException("Explicit removal of set elements is only allowed via the controlling set. Sorry!");
if (current == null) {
if (this.current == null) {
return;
}
ISetEntry<T> newCurrent = current.next();
WeakConcurrentSet.this.remove(current.getValue());
current = newCurrent;
ISetEntry<T> newCurrent = this.current.next();
WeakConcurrentSet.this.remove(this.current.getValue());
this.current = newCurrent;
}
};
}
@ -109,7 +115,7 @@ public class WeakConcurrentSet<T> extends AbstractConcurrentSet<T>{
@Override
public T getValue() {
return value.get();
return this.value.get();
}