1 |
torben |
2838 |
package dk.daoas.adressevedligehold.util;
|
2 |
|
|
|
3 |
|
|
import java.util.HashMap;
|
4 |
|
|
|
5 |
|
|
public class DeduplicateHelper<T> {
|
6 |
|
|
private HashMap<T,T> cache;
|
7 |
|
|
|
8 |
|
|
public DeduplicateHelper() {
|
9 |
|
|
cache = new HashMap<T,T>();
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
public DeduplicateHelper(int initialCapacity) {
|
13 |
|
|
cache = new HashMap<T,T>(initialCapacity);
|
14 |
|
|
}
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
public T getInstance(T newT) {
|
19 |
|
|
if (newT == null) {
|
20 |
|
|
return null;
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
T cachedT = cache.get(newT);
|
24 |
|
|
if ( cachedT == null ) {
|
25 |
|
|
cachedT = newT;
|
26 |
|
|
cache.put(newT, newT);
|
27 |
|
|
}
|
28 |
|
|
return cachedT;
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
public int size() {
|
32 |
|
|
return cache.size();
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
public void clear() {
|
36 |
|
|
cache.clear();
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
}
|