-
Notifications
You must be signed in to change notification settings - Fork 587
/
AbstractMutableMap.java
343 lines (299 loc) · 11.7 KB
/
AbstractMutableMap.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.map.mutable;
import java.util.Collection;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.factory.primitive.BooleanBags;
import org.eclipse.collections.api.factory.primitive.ByteBags;
import org.eclipse.collections.api.factory.primitive.CharBags;
import org.eclipse.collections.api.factory.primitive.DoubleBags;
import org.eclipse.collections.api.factory.primitive.FloatBags;
import org.eclipse.collections.api.factory.primitive.IntBags;
import org.eclipse.collections.api.factory.primitive.LongBags;
import org.eclipse.collections.api.factory.primitive.ShortBags;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.multimap.set.MutableSetMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.PartitionPredicate2Procedure;
import org.eclipse.collections.impl.block.procedure.PartitionProcedure;
import org.eclipse.collections.impl.block.procedure.SelectInstancesOfProcedure;
import org.eclipse.collections.impl.list.fixed.ArrayAdapter;
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
import org.eclipse.collections.impl.partition.bag.PartitionHashBag;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.utility.MapIterate;
public abstract class AbstractMutableMap<K, V> extends AbstractMutableMapIterable<K, V>
implements MutableMap<K, V>
{
@Override
@SuppressWarnings("AbstractMethodOverridesAbstractMethod")
public abstract MutableMap<K, V> clone();
/**
* Creates a new instance of the same type, using the given capacity and the default growth parameters.
*/
public abstract <K, V> MutableMap<K, V> newEmpty(int capacity);
@Override
public MutableMap<K, V> asUnmodifiable()
{
return UnmodifiableMutableMap.of(this);
}
@Override
public ImmutableMap<K, V> toImmutable()
{
return Maps.immutable.withAll(this);
}
@Override
public MutableMap<K, V> asSynchronized()
{
return SynchronizedMutableMap.of(this);
}
@Override
public MutableSetMultimap<V, K> flip()
{
return MapIterate.flip(this);
}
@Override
public <R> MutableMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function)
{
return MapIterate.collectValues(this, function, this.newEmpty(this.size()));
}
@Override
public MutableMap<K, V> select(Predicate2<? super K, ? super V> predicate)
{
return MapIterate.selectMapOnEntry(this, predicate, this.newEmpty());
}
@Override
public MutableMap<K, V> reject(Predicate2<? super K, ? super V> predicate)
{
return MapIterate.rejectMapOnEntry(this, predicate, this.newEmpty());
}
@Override
public <R> MutableBag<R> collect(Function<? super V, ? extends R> function)
{
return this.collect(function, Bags.mutable.empty());
}
@Override
public MutableBooleanBag collectBoolean(BooleanFunction<? super V> booleanFunction)
{
return this.collectBoolean(booleanFunction, BooleanBags.mutable.empty());
}
@Override
public MutableByteBag collectByte(ByteFunction<? super V> byteFunction)
{
return this.collectByte(byteFunction, ByteBags.mutable.empty());
}
@Override
public MutableCharBag collectChar(CharFunction<? super V> charFunction)
{
return this.collectChar(charFunction, CharBags.mutable.empty());
}
@Override
public MutableDoubleBag collectDouble(DoubleFunction<? super V> doubleFunction)
{
return this.collectDouble(doubleFunction, DoubleBags.mutable.empty());
}
@Override
public MutableFloatBag collectFloat(FloatFunction<? super V> floatFunction)
{
return this.collectFloat(floatFunction, FloatBags.mutable.empty());
}
@Override
public MutableIntBag collectInt(IntFunction<? super V> intFunction)
{
return this.collectInt(intFunction, IntBags.mutable.empty());
}
@Override
public MutableLongBag collectLong(LongFunction<? super V> longFunction)
{
return this.collectLong(longFunction, LongBags.mutable.empty());
}
@Override
public MutableShortBag collectShort(ShortFunction<? super V> shortFunction)
{
return this.collectShort(shortFunction, ShortBags.mutable.empty());
}
@Override
public <P, VV> MutableBag<VV> collectWith(Function2<? super V, ? super P, ? extends VV> function, P parameter)
{
return this.collect(Functions.bind(function, parameter));
}
@Override
public <R> MutableBag<R> collectIf(Predicate<? super V> predicate, Function<? super V, ? extends R> function)
{
return this.collectIf(predicate, function, Bags.mutable.empty());
}
@Override
public <R> MutableBag<R> flatCollect(Function<? super V, ? extends Iterable<R>> function)
{
return this.flatCollect(function, Bags.mutable.empty());
}
@Override
public MutableBag<V> select(Predicate<? super V> predicate)
{
return this.select(predicate, Bags.mutable.empty());
}
@Override
public MutableMap<K, V> tap(Procedure<? super V> procedure)
{
this.forEach(procedure);
return this;
}
@Override
public <P> MutableBag<V> selectWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.select(Predicates.bind(predicate, parameter));
}
@Override
public MutableBag<V> reject(Predicate<? super V> predicate)
{
return this.reject(predicate, Bags.mutable.empty());
}
@Override
public <P> MutableBag<V> rejectWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.reject(Predicates.bind(predicate, parameter));
}
@Override
public PartitionMutableBag<V> partition(Predicate<? super V> predicate)
{
PartitionMutableBag<V> partitionMutableBag = new PartitionHashBag<>();
this.forEach(new PartitionProcedure<>(predicate, partitionMutableBag));
return partitionMutableBag;
}
@Override
public <P> PartitionMutableBag<V> partitionWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
PartitionMutableBag<V> partitionMutableBag = new PartitionHashBag<>();
this.forEach(new PartitionPredicate2Procedure<>(predicate, parameter, partitionMutableBag));
return partitionMutableBag;
}
@Override
public <S> MutableBag<S> selectInstancesOf(Class<S> clazz)
{
MutableBag<S> result = Bags.mutable.empty();
this.forEach(new SelectInstancesOfProcedure<>(clazz, result));
return result;
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
@Override
@Deprecated
public <S> MutableBag<Pair<V, S>> zip(Iterable<S> that)
{
return this.zip(that, Bags.mutable.empty());
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead.
*/
@Override
@Deprecated
public MutableSet<Pair<V, Integer>> zipWithIndex()
{
return this.zipWithIndex(new UnifiedSet<>(this.size()));
}
@Override
public MutableMap<K, V> withKeyValue(K key, V value)
{
this.put(key, value);
return this;
}
@Override
public MutableMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues)
{
for (Pair<? extends K, ? extends V> keyVal : keyValues)
{
this.put(keyVal.getOne(), keyVal.getTwo());
}
return this;
}
@Override
public MutableMap<K, V> withAllKeyValueArguments(Pair<? extends K, ? extends V>... keyValues)
{
return this.withAllKeyValues(ArrayAdapter.adapt(keyValues));
}
@Override
public MutableMap<K, V> withoutKey(K key)
{
this.removeKey(key);
return this;
}
@Override
public MutableMap<K, V> withoutAllKeys(Iterable<? extends K> keys)
{
for (K key : keys)
{
this.removeKey(key);
}
return this;
}
/**
* Trait-style class that is used to capture commonalities between ValuesCollection class implementations in order to
* avoid code duplication.
*/
protected abstract static class ValuesCollectionCommon<V> implements Collection<V>
{
@Override
public boolean add(V v)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAll(Collection<? extends V> collection)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
}
@Override
public <VV> MutableBagMultimap<VV, V> groupBy(Function<? super V, ? extends VV> function)
{
return this.groupBy(function, HashBagMultimap.newMultimap());
}
@Override
public <VV> MutableBagMultimap<VV, V> groupByEach(Function<? super V, ? extends Iterable<VV>> function)
{
return this.groupByEach(function, HashBagMultimap.newMultimap());
}
@Override
public <VV> MutableMap<VV, V> groupByUniqueKey(Function<? super V, ? extends VV> function)
{
return this.groupByUniqueKey(function, UnifiedMap.newMap(this.size()));
}
}