/
ConcurrentListMultimap.java
222 lines (181 loc) · 5.24 KB
/
ConcurrentListMultimap.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package de.pxav.kelp.core.common;
import com.google.common.collect.*;
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ConcurrentListMultimap<K, V> implements ConcurrentMultimap<K, V> {
private final ConcurrentMap<K, ArrayList<V>> map;
public static <K, V> ConcurrentListMultimap<K, V> create() {
return new ConcurrentListMultimap<>();
}
public static <K, V> ConcurrentListMultimap<K, V> create(Multimap<K, V> source) {
ConcurrentListMultimap<K, V> multimap = new ConcurrentListMultimap<>();
multimap.putAll(source);
return multimap;
}
public static <K, V> ConcurrentListMultimap<K, V> create(Map<K, V> source) {
ConcurrentListMultimap<K, V> multimap = new ConcurrentListMultimap<>();
source.forEach(multimap::put);
multimap.putAll(source);
return multimap;
}
public ConcurrentListMultimap() {
this.map = new ConcurrentHashMap<>();
}
@Override
public int size() {
int size = 0;
for (Collection<V> value : this.map.values()) {
size += value.size();
}
return size;
}
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
@Override
public boolean containsKey(Object o) {
return this.map.containsKey(o);
}
@Override
public boolean containsValue(Object o) {
Iterator<Collection<V>> iterator = this.asMap().values().iterator();
Collection<V> collection;
do {
if (!iterator.hasNext()) {
return false;
}
collection = iterator.next();
} while(!collection.contains(o));
return true;
}
@Override
public boolean containsEntry(Object o, Object o1) {
Collection<V> collection = this.map.get(o);
return collection != null && collection.contains(o1);
}
@Override
public boolean put(K k, V v) {
if (this.get(k) == null) {
this.map.put(k, Lists.newArrayList());
}
return this.get(k).add(v);
}
@Override
public boolean remove(Object key, Object value) {
if (this.map.get(key) == null) {
return false;
}
return this.map.get(key).remove(value);
}
@Override
public boolean putAll(K k, Iterable<? extends V> iterable) {
if (iterable instanceof Collection) {
Collection<V> collection = (Collection<V>) iterable;
if (collection.isEmpty()) {
return false;
}
if (this.get(k) == null) {
this.map.put(k, Lists.newArrayList());
}
return this.get(k).addAll(collection);
} else {
Iterator<? extends V> valueItr = iterable.iterator();
if (!valueItr.hasNext()) {
return false;
}
if (this.get(k) == null) {
this.map.put(k, Lists.newArrayList());
}
return Iterators.addAll(this.get(k), valueItr);
}
}
@Override
public boolean putAll(Multimap<? extends K, ? extends V> multimap) {
for (Map.Entry<? extends K, ? extends V> entry : multimap.entries()) {
this.put(entry.getKey(), entry.getValue());
}
return false;
}
@Override
public void putAll(Map<K, V> newMap) {
for (Map.Entry<K, V> entry : newMap.entrySet()) {
this.put(entry.getKey(), entry.getValue());
}
}
@Override
public Collection<V> replaceValues(K k, Iterable<? extends V> iterable) {
Collection<V> result = this.removeAll(k);
this.putAll(k, iterable);
return result;
}
@Override
public Collection<V> removeAll(Object key) {
if (this.map.get(key) == null) {
return Lists.newArrayList();
}
return this.map.remove(key);
}
@Override
public void clear() {
this.map.clear();
}
@Override
public Collection<V> get(@Nullable K k) {
return this.map.get(k);
}
@Override
public Set<K> keySet() {
return this.map.keySet();
}
@Override
public Multiset<K> keys() {
return HashMultiset.create(this.map.keySet());
}
@Override
public Collection<V> values() {
Iterator<Collection<V>> iterator = this.asMap().values().iterator();
Collection<V> result = Lists.newArrayList();
while (iterator.hasNext()) {
result.addAll(iterator.next());
}
return result;
}
@Override
public Collection<Map.Entry<K, V>> entries() {
Collection<Map.Entry<K, V>> entries = Lists.newArrayList();
this.map.forEach((key, valueSet) -> valueSet.forEach(element -> {
AbstractMap.SimpleEntry<K, V> entry = new AbstractMap.SimpleEntry<>(key, element);
entries.add(entry);
}));
return entries;
}
@Override
public ConcurrentMap<K, Collection<V>> asMap() {
ConcurrentMap<K, Collection<V>> output = Maps.newConcurrentMap();
output.putAll(this.map);
return output;
}
@Override
public boolean containsValue(Collection<V> iterable) {
return this.map.containsValue(iterable);
}
@Override
public Collection<V> getOrDefault(K key, Collection<V> defaultCollection) {
return this.map.getOrDefault(key, Lists.newArrayList(defaultCollection));
}
@Override
public Collection<V> getOrEmpty(K key) {
return this.map.getOrDefault(key, Lists.newArrayList());
}
@Override
public void removeWithValue(V value) {
this.map.forEach((key, valueSet) -> valueSet.forEach(current -> {
if (current.equals(value)) {
this.remove(key, current);
}
}));
}
}