58aca31ac791cea86aebaea721a2e1c60c56c489
[onosfw.git] /
1 package org.onosproject.store.consistent.impl;
2
3 import java.util.Collection;
4 import java.util.Map;
5 import java.util.Set;
6 import java.util.function.BiConsumer;
7 import java.util.function.BiFunction;
8 import java.util.function.Function;
9 import java.util.stream.Collectors;
10
11 import org.onosproject.store.service.ConsistentMap;
12 import org.onosproject.store.service.Versioned;
13
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Maps;
16
17 /**
18  * Standard java Map backed by a ConsistentMap.
19  *
20  * @param <K> key type
21  * @param <V> value type
22  */
23 public final class ConsistentMapBackedJavaMap<K, V> implements Map<K, V> {
24
25     private final ConsistentMap<K, V> backingMap;
26
27     public ConsistentMapBackedJavaMap(ConsistentMap<K, V> backingMap) {
28         this.backingMap = backingMap;
29     }
30
31     @Override
32     public int size() {
33         return backingMap.size();
34     }
35
36     @Override
37     public boolean isEmpty() {
38         return backingMap.isEmpty();
39     }
40
41     @Override
42     public boolean containsKey(Object key) {
43         return backingMap.containsKey((K) key);
44     }
45
46     @Override
47     public boolean containsValue(Object value) {
48         return backingMap.containsValue((V) value);
49     }
50
51     @Override
52     public V get(Object key) {
53         return Versioned.valueOrElse(backingMap.get((K) key), null);
54     }
55
56     @Override
57     public V getOrDefault(Object key, V defaultValue) {
58         return Versioned.valueOrElse(backingMap.get((K) key), defaultValue);
59     }
60
61     @Override
62     public V put(K key, V value) {
63         return Versioned.valueOrElse(backingMap.put(key, value), null);
64     }
65
66     @Override
67     public V putIfAbsent(K key, V value) {
68         return Versioned.valueOrElse(backingMap.putIfAbsent(key, value), null);
69     }
70
71     @Override
72     public V remove(Object key) {
73         return Versioned.valueOrElse(backingMap.remove((K) key), null);
74     }
75
76     @Override
77     public boolean remove(Object key, Object value) {
78         return backingMap.remove((K) key, (V) value);
79     }
80
81     @Override
82     public V replace(K key, V value) {
83         throw new UnsupportedOperationException();
84     }
85
86     @Override
87     public boolean replace(K key, V oldValue, V newValue) {
88         return  backingMap.replace(key, oldValue, newValue);
89     }
90
91     @Override
92     public void putAll(Map<? extends K, ? extends V> m) {
93         m.forEach((k, v) -> {
94             backingMap.put(k, v);
95         });
96     }
97
98     @Override
99     public void clear() {
100         backingMap.clear();
101     }
102
103     @Override
104     public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
105         return Versioned.valueOrElse(backingMap.compute(key, remappingFunction), null);
106     }
107
108     @Override
109     public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
110         return Versioned.valueOrElse(backingMap.computeIfAbsent(key, mappingFunction), null);
111     }
112
113     @Override
114     public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
115         return Versioned.valueOrElse(backingMap.computeIfPresent(key, remappingFunction), null);
116     }
117
118     @Override
119     public Set<K> keySet() {
120         return backingMap.keySet();
121     }
122
123     @Override
124     public Collection<V> values() {
125         return Collections2.transform(backingMap.values(), v -> v.value());
126     }
127
128     @Override
129     public Set<java.util.Map.Entry<K, V>> entrySet() {
130         return backingMap.entrySet()
131                          .stream()
132                          .map(entry -> Maps.immutableEntry(entry.getKey(), entry.getValue().value()))
133                          .collect(Collectors.toSet());
134     }
135
136     @Override
137     public void forEach(BiConsumer<? super K, ? super V> action) {
138         entrySet().forEach(e -> action.accept(e.getKey(), e.getValue()));
139     }
140
141     @Override
142     public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
143         return computeIfPresent(key, (k, v) -> v == null ? value : remappingFunction.apply(v, value));
144     }
145 }