Class WeakReferenceList<E>
- java.lang.Object
-
- com.biomatters.geneious.publicapi.utilities.WeakReferenceList<E>
-
- All Implemented Interfaces:
java.lang.Iterable<E>
public class WeakReferenceList<E> extends java.lang.Object implements java.lang.Iterable<E>
Manages a list ofWeakReferences
, 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 Summary
Constructors Constructor Description WeakReferenceList()
Constructs an empty WeakReferenceListWeakReferenceList(WeakReferenceList<E> weakReferenceListToCopy)
Constructs a copy of another WeakReferenceList
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(E objectToAddReferenceTo)
Adds a weak reference to the given object.java.util.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 usingadd(Object)
.java.util.List<E>
toList()
Creates a list of all objects added to this class that are still strongly referenced.
-
-
-
Constructor Detail
-
WeakReferenceList
public WeakReferenceList(WeakReferenceList<E> weakReferenceListToCopy)
Constructs a copy of another WeakReferenceList- Parameters:
weakReferenceListToCopy
- the WeakReferenceList to copy
-
WeakReferenceList
public WeakReferenceList()
Constructs an empty WeakReferenceList
-
-
Method Detail
-
add
public void add(E objectToAddReferenceTo)
Adds a weak reference to the given object.- Parameters:
objectToAddReferenceTo
- the object to add a weak reference to.
-
remove
public void remove(E objectToRemoveReferenceTo)
Removes a reference to an object previously added usingadd(Object)
.- Parameters:
objectToRemoveReferenceTo
- the object to remove a reference to
-
iterator
public java.util.Iterator<E> iterator()
Gets an iterator over all objects added to this class that are still strongly referenced.- Specified by:
iterator
in interfacejava.lang.Iterable<E>
- Returns:
- an iterator over all objects added to this class that are still strongly referenced.
-
toList
public java.util.List<E> toList()
Creates a list of all objects added to this class that are still strongly referenced.- Returns:
- a list of all objects added to this class that are still strongly referenced.
-
-