aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/utils/WeakReferenceSet.java
blob: b6ccbd560652de775c584e17b4cbd2ee7152166c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package de.pixart.messenger.utils;

import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Iterator;

public class WeakReferenceSet<T> extends HashSet<WeakReference<T>> {

    public void removeWeakReferenceTo(T reference) {
        for (Iterator<WeakReference<T>> iterator = iterator(); iterator.hasNext(); ) {
            if (reference == iterator.next().get()) {
                iterator.remove();
            }
        }
    }


    public void addWeakReferenceTo(T reference) {
        for (WeakReference<T> weakReference : this) {
            if (reference == weakReference.get()) {
                return;
            }
        }
        this.add(new WeakReference<>(reference));
    }
}