Package: ConcurrentHashMapWithOptionals
ConcurrentHashMapWithOptionals
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ConcurrentHashMapWithOptionals() |
|
|
|
|
|
||||||||||||||||||||
lambda$putIfAbsentAndGetNewKey$0(Object, Object) |
|
|
|
|
|
||||||||||||||||||||
putIfAbsentAndGetNewKey(Object, Object) |
|
|
|
|
|
||||||||||||||||||||
putIfAbsentAndGetNewKey(Optional, Object) |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.util;
27:
28: import javax.annotation.Nonnull;
29: import java.util.Optional;
30: import java.util.concurrent.ConcurrentHashMap;
31:
32: /***************************************************************************************************************************************************************
33: *
34: * A specialisation of {@link ConcurrentHashMap} with {@link Optional} support.
35: *
36: * @param <K> the type of the key
37: * @param <V> the type of the value
38: *
39: * @since 3.1-ALPHA-2
40: * @author Fabrizio Giudici
41: *
42: **************************************************************************************************************************************************************/
43: public class ConcurrentHashMapWithOptionals<K, V> extends ConcurrentHashMap<K, V>
44: {
45: private static final long serialVersionUID = -1771492882183193296L;
46:
47: /***********************************************************************************************************************************************************
48: * If the map doesn't contain the given key, put the new pair(key, value), and return the key itself. Otherwise,
49: * do nothing and return an empty {@link Optional}. The map manipulation is atomically performed by calling
50: * {@link #putIfAbsent(java.lang.Object, java.lang.Object)}.
51: *
52: * If {@code optionalKey} is empty, do nothing and return nothing.
53: *
54: * @param optionalKey the key
55: * @param value the value
56: * @return the new key, or nothing
57: **********************************************************************************************************************************************************/
58: @Nonnull
59: public Optional<K> putIfAbsentAndGetNewKey (@Nonnull final Optional<? extends K> optionalKey, @Nonnull final V value)
60: {
61: return optionalKey.flatMap(key -> putIfAbsentAndGetNewKey(key, value));
62: }
63:
64: /***********************************************************************************************************************************************************
65: * If the map doesn't contain the given key, put the new pair(key, value), and return the key itself. Otherwise,
66: * do nothing and return an empty {@link Optional}. The map manipulation is atomically performed by calling
67: * {@link #putIfAbsent(java.lang.Object, java.lang.Object)}.
68: *
69: * @param key the key
70: * @param value the value
71: * @return the new key, or nothing
72: **********************************************************************************************************************************************************/
73: @Nonnull
74: public Optional<K> putIfAbsentAndGetNewKey (@Nonnull final K key, @Nonnull final V value)
75: {
76:• return (putIfAbsent(key, value) == null) ? Optional.of(key) : Optional.empty();
77: }
78: }