package eu.siacs.conversations.utils; import java.lang.ref.WeakReference; import java.util.HashSet; import java.util.Iterator; public class WeakReferenceSet extends HashSet> { public void removeWeakReferenceTo(T reference) { for (Iterator> iterator = iterator(); iterator.hasNext(); ) { if (reference == iterator.next().get()) { iterator.remove(); } } } public void addWeakReferenceTo(T reference) { for (WeakReference weakReference : this) { if (reference == weakReference.get()) { return; } } this.add(new WeakReference<>(reference)); } }