1 package org.onosproject.store.consistent.impl;
3 import java.util.Collection;
6 import java.util.function.BiConsumer;
7 import java.util.function.BiFunction;
8 import java.util.function.Function;
9 import java.util.stream.Collectors;
11 import org.onosproject.store.service.ConsistentMap;
12 import org.onosproject.store.service.Versioned;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Maps;
18 * Standard java Map backed by a ConsistentMap.
21 * @param <V> value type
23 public final class ConsistentMapBackedJavaMap<K, V> implements Map<K, V> {
25 private final ConsistentMap<K, V> backingMap;
27 public ConsistentMapBackedJavaMap(ConsistentMap<K, V> backingMap) {
28 this.backingMap = backingMap;
33 return backingMap.size();
37 public boolean isEmpty() {
38 return backingMap.isEmpty();
42 public boolean containsKey(Object key) {
43 return backingMap.containsKey((K) key);
47 public boolean containsValue(Object value) {
48 return backingMap.containsValue((V) value);
52 public V get(Object key) {
53 return Versioned.valueOrElse(backingMap.get((K) key), null);
57 public V getOrDefault(Object key, V defaultValue) {
58 return Versioned.valueOrElse(backingMap.get((K) key), defaultValue);
62 public V put(K key, V value) {
63 return Versioned.valueOrElse(backingMap.put(key, value), null);
67 public V putIfAbsent(K key, V value) {
68 return Versioned.valueOrElse(backingMap.putIfAbsent(key, value), null);
72 public V remove(Object key) {
73 return Versioned.valueOrElse(backingMap.remove((K) key), null);
77 public boolean remove(Object key, Object value) {
78 return backingMap.remove((K) key, (V) value);
82 public V replace(K key, V value) {
83 throw new UnsupportedOperationException();
87 public boolean replace(K key, V oldValue, V newValue) {
88 return backingMap.replace(key, oldValue, newValue);
92 public void putAll(Map<? extends K, ? extends V> m) {
104 public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
105 return Versioned.valueOrElse(backingMap.compute(key, remappingFunction), null);
109 public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
110 return Versioned.valueOrElse(backingMap.computeIfAbsent(key, mappingFunction), null);
114 public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
115 return Versioned.valueOrElse(backingMap.computeIfPresent(key, remappingFunction), null);
119 public Set<K> keySet() {
120 return backingMap.keySet();
124 public Collection<V> values() {
125 return Collections2.transform(backingMap.values(), v -> v.value());
129 public Set<java.util.Map.Entry<K, V>> entrySet() {
130 return backingMap.entrySet()
132 .map(entry -> Maps.immutableEntry(entry.getKey(), entry.getValue().value()))
133 .collect(Collectors.toSet());
137 public void forEach(BiConsumer<? super K, ? super V> action) {
138 entrySet().forEach(e -> action.accept(e.getKey(), e.getValue()));
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));