1 | package com.renomad.minum.utils; | |
2 | ||
3 | import java.util.Collections; | |
4 | import java.util.Iterator; | |
5 | import java.util.concurrent.ConcurrentHashMap; | |
6 | ||
7 | /** | |
8 | * This uses a [ConcurrentHashMap] as its base. We store | |
9 | * the data in the keys only. We provide some syntactic sugar | |
10 | * so this seems similar to using a Set. | |
11 | * <p> | |
12 | * This is a thread-safe data structure. | |
13 | */ | |
14 | public final class ConcurrentSet<T> implements Iterable<T> { | |
15 | ||
16 | private final ConcurrentHashMap<T, NullEnum> map; | |
17 | ||
18 | public ConcurrentSet() { | |
19 | this.map = new ConcurrentHashMap<>(); | |
20 | } | |
21 | ||
22 | public void add(T element) { | |
23 | map.putIfAbsent(element, NullEnum.NULL); | |
24 | } | |
25 | ||
26 | public void remove(T element) { | |
27 | map.remove(element); | |
28 | } | |
29 | ||
30 | public int size() { | |
31 |
1
1. size : replaced int return with 0 for com/renomad/minum/utils/ConcurrentSet::size → KILLED |
return map.size(); |
32 | } | |
33 | ||
34 | @Override | |
35 | public Iterator<T> iterator() { | |
36 |
1
1. iterator : replaced return value with null for com/renomad/minum/utils/ConcurrentSet::iterator → KILLED |
return Collections.unmodifiableSet(map.keySet(NullEnum.NULL)).iterator(); |
37 | } | |
38 | ||
39 | private enum NullEnum { | |
40 | /** | |
41 | * This is just a token for the value in the ConcurrentHashMap, since | |
42 | * we are only using the keys, never the values. | |
43 | */ | |
44 | NULL | |
45 | } | |
46 | } | |
Mutations | ||
31 |
1.1 |
|
36 |
1.1 |