~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to collections/src/main/java/com/gs/collections/impl/map/mutable/ConcurrentMutableHashMap.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package com.gs.collections.impl.map.mutable;
 
18
 
 
19
import java.io.Serializable;
 
20
import java.util.AbstractSet;
 
21
import java.util.Collection;
 
22
import java.util.Iterator;
 
23
import java.util.Map;
 
24
import java.util.Set;
 
25
import java.util.concurrent.ConcurrentHashMap;
 
26
import java.util.concurrent.ConcurrentMap;
 
27
 
 
28
import com.gs.collections.api.block.function.Function;
 
29
import com.gs.collections.api.block.function.Function0;
 
30
import com.gs.collections.api.block.function.Function2;
 
31
import com.gs.collections.api.block.procedure.Procedure;
 
32
import com.gs.collections.api.block.procedure.Procedure2;
 
33
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
 
34
import com.gs.collections.api.map.ConcurrentMutableMap;
 
35
import com.gs.collections.api.map.ImmutableMap;
 
36
import com.gs.collections.api.map.MutableMap;
 
37
import com.gs.collections.api.tuple.Pair;
 
38
import com.gs.collections.impl.block.procedure.MapEntryToProcedure2;
 
39
import com.gs.collections.impl.factory.Maps;
 
40
import com.gs.collections.impl.tuple.ImmutableEntry;
 
41
import com.gs.collections.impl.utility.Iterate;
 
42
import com.gs.collections.impl.utility.internal.IterableIterate;
 
43
 
 
44
/**
 
45
 * A simple concurrent implementation of MutableMap which uses java.util.concurrent.ConcurrentHashMap for its underlying
 
46
 * concurrent Map implementation.
 
47
 *
 
48
 * @see com.gs.collections.impl.map.mutable.ConcurrentHashMap
 
49
 * @deprecated since 2.0
 
50
 */
 
51
@Deprecated
 
52
public final class ConcurrentMutableHashMap<K, V>
 
53
        extends AbstractMutableMap<K, V>
 
54
        implements ConcurrentMutableMap<K, V>, Serializable
 
55
{
 
56
    private static final long serialVersionUID = 1L;
 
57
    private static final String JAVA_SPECIFICATION_VERSION = System.getProperty("java.specification.version");
 
58
 
 
59
    private final ConcurrentMap<K, V> delegate;
 
60
 
 
61
    private ConcurrentMutableHashMap()
 
62
    {
 
63
        this(new ConcurrentHashMap<K, V>());
 
64
    }
 
65
 
 
66
    public ConcurrentMutableHashMap(ConcurrentMap<K, V> delegate)
 
67
    {
 
68
        this.delegate = delegate;
 
69
    }
 
70
 
 
71
    public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap()
 
72
    {
 
73
        return new ConcurrentMutableHashMap<NK, NV>();
 
74
    }
 
75
 
 
76
    public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap(int initialCapacity)
 
77
    {
 
78
        return new ConcurrentMutableHashMap<NK, NV>(new ConcurrentHashMap<NK, NV>(initialCapacity));
 
79
    }
 
80
 
 
81
    public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap(int initialCapacity, float loadFactor, int concurrencyLevel)
 
82
    {
 
83
        return new ConcurrentMutableHashMap<NK, NV>(new ConcurrentHashMap<NK, NV>(initialCapacity, loadFactor, concurrencyLevel));
 
84
    }
 
85
 
 
86
    public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap(Map<NK, NV> map)
 
87
    {
 
88
        return new ConcurrentMutableHashMap<NK, NV>(new ConcurrentHashMap<NK, NV>(map));
 
89
    }
 
90
 
 
91
    @Override
 
92
    public ConcurrentMutableHashMap<K, V> withKeyValue(K key, V value)
 
93
    {
 
94
        return (ConcurrentMutableHashMap<K, V>) super.withKeyValue(key, value);
 
95
    }
 
96
 
 
97
    @Override
 
98
    public ConcurrentMutableHashMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues)
 
99
    {
 
100
        return (ConcurrentMutableHashMap<K, V>) super.withAllKeyValues(keyValues);
 
101
    }
 
102
 
 
103
    @Override
 
104
    public ConcurrentMutableHashMap<K, V> withAllKeyValueArguments(Pair<? extends K, ? extends V>... keyValues)
 
105
    {
 
106
        return (ConcurrentMutableHashMap<K, V>) super.withAllKeyValueArguments(keyValues);
 
107
    }
 
108
 
 
109
    @Override
 
110
    public ConcurrentMutableHashMap<K, V> withoutKey(K key)
 
111
    {
 
112
        return (ConcurrentMutableHashMap<K, V>) super.withoutKey(key);
 
113
    }
 
114
 
 
115
    @Override
 
116
    public ConcurrentMutableHashMap<K, V> withoutAllKeys(Iterable<? extends K> keys)
 
117
    {
 
118
        return (ConcurrentMutableHashMap<K, V>) super.withoutAllKeys(keys);
 
119
    }
 
120
 
 
121
    @Override
 
122
    public String toString()
 
123
    {
 
124
        return this.delegate.toString();
 
125
    }
 
126
 
 
127
    @Override
 
128
    public MutableMap<K, V> clone()
 
129
    {
 
130
        return ConcurrentMutableHashMap.newMap(this.delegate);
 
131
    }
 
132
 
 
133
    @Override
 
134
    public <K, V> MutableMap<K, V> newEmpty(int capacity)
 
135
    {
 
136
        return ConcurrentMutableHashMap.newMap();
 
137
    }
 
138
 
 
139
    @Override
 
140
    public boolean notEmpty()
 
141
    {
 
142
        return !this.delegate.isEmpty();
 
143
    }
 
144
 
 
145
    @Override
 
146
    public void forEach(Procedure<? super V> procedure)
 
147
    {
 
148
        IterableIterate.forEach(this.delegate.values(), procedure);
 
149
    }
 
150
 
 
151
    @Override
 
152
    public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
 
153
    {
 
154
        Iterate.forEachWithIndex(this.delegate.values(), objectIntProcedure);
 
155
    }
 
156
 
 
157
    public int size()
 
158
    {
 
159
        return this.delegate.size();
 
160
    }
 
161
 
 
162
    @Override
 
163
    public boolean isEmpty()
 
164
    {
 
165
        return this.delegate.isEmpty();
 
166
    }
 
167
 
 
168
    @Override
 
169
    public Iterator<V> iterator()
 
170
    {
 
171
        return this.delegate.values().iterator();
 
172
    }
 
173
 
 
174
    public V remove(Object key)
 
175
    {
 
176
        return this.delegate.remove(key);
 
177
    }
 
178
 
 
179
    public Set<K> keySet()
 
180
    {
 
181
        return this.delegate.keySet();
 
182
    }
 
183
 
 
184
    public Collection<V> values()
 
185
    {
 
186
        return this.delegate.values();
 
187
    }
 
188
 
 
189
    public Set<Entry<K, V>> entrySet()
 
190
    {
 
191
        if ("1.5".equals(JAVA_SPECIFICATION_VERSION))
 
192
        {
 
193
            return new SafeEntrySetAdapter<K, V>(this.delegate.entrySet());
 
194
        }
 
195
        return this.delegate.entrySet();
 
196
    }
 
197
 
 
198
    public void clear()
 
199
    {
 
200
        this.delegate.clear();
 
201
    }
 
202
 
 
203
    public MutableMap<K, V> newEmpty()
 
204
    {
 
205
        return ConcurrentMutableHashMap.newMap();
 
206
    }
 
207
 
 
208
    @Override
 
209
    public void forEachValue(Procedure<? super V> procedure)
 
210
    {
 
211
        IterableIterate.forEach(this.delegate.values(), procedure);
 
212
    }
 
213
 
 
214
    @Override
 
215
    public void forEachKey(Procedure<? super K> procedure)
 
216
    {
 
217
        IterableIterate.forEach(this.delegate.keySet(), procedure);
 
218
    }
 
219
 
 
220
    public void forEachKeyValue(Procedure2<? super K, ? super V> procedure)
 
221
    {
 
222
        IterableIterate.forEach(this.delegate.entrySet(), new MapEntryToProcedure2<K, V>(procedure));
 
223
    }
 
224
 
 
225
    public V get(Object key)
 
226
    {
 
227
        return this.delegate.get(key);
 
228
    }
 
229
 
 
230
    public V put(K key, V value)
 
231
    {
 
232
        return this.delegate.put(key, value);
 
233
    }
 
234
 
 
235
    public void putAll(Map<? extends K, ? extends V> map)
 
236
    {
 
237
        this.delegate.putAll(map);
 
238
    }
 
239
 
 
240
    public <E> MutableMap<K, V> collectKeysAndValues(
 
241
            Iterable<E> iterable,
 
242
            Function<? super E, ? extends K> keyFunction,
 
243
            Function<? super E, ? extends V> valueFunction)
 
244
    {
 
245
        Iterate.addToMap(iterable, keyFunction, valueFunction, this.delegate);
 
246
        return this;
 
247
    }
 
248
 
 
249
    public V removeKey(K key)
 
250
    {
 
251
        return this.delegate.remove(key);
 
252
    }
 
253
 
 
254
    public boolean containsKey(Object key)
 
255
    {
 
256
        return this.delegate.containsKey(key);
 
257
    }
 
258
 
 
259
    public boolean containsValue(Object value)
 
260
    {
 
261
        return this.delegate.containsValue(value);
 
262
    }
 
263
 
 
264
    @Override
 
265
    public V getIfAbsentPut(K key, Function0<? extends V> function)
 
266
    {
 
267
        V result = this.delegate.get(key);
 
268
        if (result == null)
 
269
        {
 
270
            V blockValue = function.value();
 
271
            V putResult = this.delegate.putIfAbsent(key, blockValue);
 
272
            return putResult == null ? blockValue : putResult;
 
273
        }
 
274
        return result;
 
275
    }
 
276
 
 
277
    @Override
 
278
    public V getIfAbsentPut(K key, V value)
 
279
    {
 
280
        V result = this.delegate.get(key);
 
281
        if (result == null)
 
282
        {
 
283
            V putResult = this.delegate.putIfAbsent(key, value);
 
284
            return putResult == null ? value : putResult;
 
285
        }
 
286
        return result;
 
287
    }
 
288
 
 
289
    @Override
 
290
    public <P> V getIfAbsentPutWith(K key, Function<? super P, ? extends V> function, P parameter)
 
291
    {
 
292
        V result = this.delegate.get(key);
 
293
        if (result == null)
 
294
        {
 
295
            V functionValue = function.valueOf(parameter);
 
296
            V putResult = this.delegate.putIfAbsent(key, functionValue);
 
297
            return putResult == null ? functionValue : putResult;
 
298
        }
 
299
        return result;
 
300
    }
 
301
 
 
302
    @Override
 
303
    public V getIfAbsent(K key, Function0<? extends V> function)
 
304
    {
 
305
        V result = this.delegate.get(key);
 
306
        if (result == null)
 
307
        {
 
308
            return function.value();
 
309
        }
 
310
        return result;
 
311
    }
 
312
 
 
313
    @Override
 
314
    public V getIfAbsentValue(K key, V value)
 
315
    {
 
316
        V result = this.delegate.get(key);
 
317
        if (result == null)
 
318
        {
 
319
            return value;
 
320
        }
 
321
        return result;
 
322
    }
 
323
 
 
324
    @Override
 
325
    public <P> V getIfAbsentWith(
 
326
            K key,
 
327
            Function<? super P, ? extends V> function,
 
328
            P parameter)
 
329
    {
 
330
        V result = this.delegate.get(key);
 
331
        if (result == null)
 
332
        {
 
333
            return function.valueOf(parameter);
 
334
        }
 
335
        return result;
 
336
    }
 
337
 
 
338
    public V getIfAbsentPut(K key, Function<? super K, ? extends V> factory)
 
339
    {
 
340
        throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".getIfAbsentPut() not implemented yet");
 
341
    }
 
342
 
 
343
    @Override
 
344
    public <A> A ifPresentApply(K key, Function<? super V, ? extends A> function)
 
345
    {
 
346
        V result = this.delegate.get(key);
 
347
        return result == null ? null : function.valueOf(result);
 
348
    }
 
349
 
 
350
    @Override
 
351
    public boolean equals(Object o)
 
352
    {
 
353
        return this.delegate.equals(o);
 
354
    }
 
355
 
 
356
    @Override
 
357
    public int hashCode()
 
358
    {
 
359
        return this.delegate.hashCode();
 
360
    }
 
361
 
 
362
    @Override
 
363
    public <P> void forEachWith(Procedure2<? super V, ? super P> procedure, P parameter)
 
364
    {
 
365
        Iterate.forEachWith(this.delegate.values(), procedure, parameter);
 
366
    }
 
367
 
 
368
    public V putIfAbsent(K key, V value)
 
369
    {
 
370
        return this.delegate.putIfAbsent(key, value);
 
371
    }
 
372
 
 
373
    public boolean remove(Object key, Object value)
 
374
    {
 
375
        return this.delegate.remove(key, value);
 
376
    }
 
377
 
 
378
    public boolean replace(K key, V oldValue, V newValue)
 
379
    {
 
380
        return this.delegate.replace(key, oldValue, newValue);
 
381
    }
 
382
 
 
383
    public V replace(K key, V value)
 
384
    {
 
385
        return this.delegate.replace(key, value);
 
386
    }
 
387
 
 
388
    private static final class SafeEntrySetAdapter<K, V>
 
389
            extends AbstractSet<Entry<K, V>>
 
390
    {
 
391
        private final Set<Entry<K, V>> delegate;
 
392
 
 
393
        private SafeEntrySetAdapter(Set<Entry<K, V>> newDelegate)
 
394
        {
 
395
            this.delegate = newDelegate;
 
396
        }
 
397
 
 
398
        @Override
 
399
        public Iterator<Entry<K, V>> iterator()
 
400
        {
 
401
            return new Iterator<Entry<K, V>>()
 
402
            {
 
403
                private final Iterator<Entry<K, V>> entryIterator = SafeEntrySetAdapter.this.delegate.iterator();
 
404
 
 
405
                public boolean hasNext()
 
406
                {
 
407
                    return this.entryIterator.hasNext();
 
408
                }
 
409
 
 
410
                public Entry<K, V> next()
 
411
                {
 
412
                    Entry<K, V> superNext = this.entryIterator.next();
 
413
                    return ImmutableEntry.of(superNext.getKey(), superNext.getValue());
 
414
                }
 
415
 
 
416
                public void remove()
 
417
                {
 
418
                    this.entryIterator.remove();
 
419
                }
 
420
            };
 
421
        }
 
422
 
 
423
        @Override
 
424
        public int size()
 
425
        {
 
426
            return this.delegate.size();
 
427
        }
 
428
    }
 
429
 
 
430
    @Override
 
431
    public V updateValue(K key, Function0<? extends V> factory, Function<? super V, ? extends V> function)
 
432
    {
 
433
        while (true)
 
434
        {
 
435
            V originalValue = this.delegate.get(key);
 
436
            if (originalValue == null)
 
437
            {
 
438
                V zero = factory.value();
 
439
                V newValue = function.valueOf(zero);
 
440
                if (this.delegate.putIfAbsent(key, newValue) == null)
 
441
                {
 
442
                    return newValue;
 
443
                }
 
444
            }
 
445
            else
 
446
            {
 
447
                V newValue = function.valueOf(originalValue);
 
448
                if (this.delegate.replace(key, originalValue, newValue))
 
449
                {
 
450
                    return newValue;
 
451
                }
 
452
            }
 
453
        }
 
454
    }
 
455
 
 
456
    @Override
 
457
    public <P> V updateValueWith(K key, Function0<? extends V> factory, Function2<? super V, ? super P, ? extends V> function, P parameter)
 
458
    {
 
459
        while (true)
 
460
        {
 
461
            V originalValue = this.delegate.get(key);
 
462
            if (originalValue == null)
 
463
            {
 
464
                V zero = factory.value();
 
465
                V newValue = function.value(zero, parameter);
 
466
                if (this.delegate.putIfAbsent(key, newValue) == null)
 
467
                {
 
468
                    return newValue;
 
469
                }
 
470
            }
 
471
            else
 
472
            {
 
473
                V newValue = function.value(originalValue, parameter);
 
474
                if (this.delegate.replace(key, originalValue, newValue))
 
475
                {
 
476
                    return newValue;
 
477
                }
 
478
            }
 
479
        }
 
480
    }
 
481
 
 
482
    @Override
 
483
    public ImmutableMap<K, V> toImmutable()
 
484
    {
 
485
        return Maps.immutable.ofMap(this);
 
486
    }
 
487
}