public class WeakReferenceList<E> extends Object implements Iterable<E>
WeakReferences, and providing
an iterator over the objects which are still strongly referenced.
Useful for avoiding memory leaks of the type where a class (X)
adds itself to a listener of another class (Y, which might be a permanent class
which is never intended to be freed). When nothing else refers to
class X, it normally wouldn't be freed, because it is still a listener
of class Y. Using a weak reference list means that class X can be
freed if the only reference to it is a weak reference inside class Y's
listener list.| Constructor and Description |
|---|
WeakReferenceList()
Constructs an empty WeakReferenceList
|
WeakReferenceList(WeakReferenceList<E> weakReferenceListToCopy)
Constructs a copy of another WeakReferenceList
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(E objectToAddReferenceTo)
Adds a weak reference to the given object
|
Iterator<E> |
iterator()
Gets an iterator over all objects added to this class that are still strongly referenced.
|
void |
remove(E objectToRemoveReferenceTo)
Removes a reference to an object previously added using
add(Object). |
List<E> |
toList()
Creates a list of all objects added to this class that are still strongly referenced.
|
public WeakReferenceList(WeakReferenceList<E> weakReferenceListToCopy)
weakReferenceListToCopy - the WeakReferenceList to copypublic WeakReferenceList()
public void add(E objectToAddReferenceTo)
objectToAddReferenceTo - the object to add a weak reference to.public void remove(E objectToRemoveReferenceTo)
add(Object).objectToRemoveReferenceTo - the object to remove a reference topublic Iterator<E> iterator()