blob: 0b67b620e348118c16a3033b3dc17316343da1da [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
Madan Jampanif95290a2016-01-27 21:06:11 -080016package org.onosproject.store.primitives;
Madan Jampanie1065222015-08-17 16:21:51 -070017
18import java.util.Collection;
19import java.util.Map;
20import java.util.Set;
21import java.util.function.BiConsumer;
22import java.util.function.BiFunction;
23import java.util.function.Function;
24import java.util.stream.Collectors;
25
26import org.onosproject.store.service.ConsistentMap;
27import org.onosproject.store.service.Versioned;
28
29import com.google.common.collect.Collections2;
30import com.google.common.collect.Maps;
31
32/**
Madan Jampanifa242182016-01-22 13:42:54 -080033 * Standard java {@link Map} backed by a {@link ConsistentMap}.
Madan Jampanie1065222015-08-17 16:21:51 -070034 *
35 * @param <K> key type
36 * @param <V> value type
37 */
38public final class ConsistentMapBackedJavaMap<K, V> implements Map<K, V> {
39
40 private final ConsistentMap<K, V> backingMap;
41
42 public ConsistentMapBackedJavaMap(ConsistentMap<K, V> backingMap) {
43 this.backingMap = backingMap;
44 }
45
46 @Override
47 public int size() {
48 return backingMap.size();
49 }
50
51 @Override
52 public boolean isEmpty() {
53 return backingMap.isEmpty();
54 }
55
56 @Override
57 public boolean containsKey(Object key) {
58 return backingMap.containsKey((K) key);
59 }
60
61 @Override
62 public boolean containsValue(Object value) {
63 return backingMap.containsValue((V) value);
64 }
65
66 @Override
67 public V get(Object key) {
Madan Jampanifa242182016-01-22 13:42:54 -080068 return Versioned.valueOrNull(backingMap.get((K) key));
Madan Jampanie1065222015-08-17 16:21:51 -070069 }
70
71 @Override
72 public V getOrDefault(Object key, V defaultValue) {
73 return Versioned.valueOrElse(backingMap.get((K) key), defaultValue);
74 }
75
76 @Override
77 public V put(K key, V value) {
Madan Jampanifa242182016-01-22 13:42:54 -080078 return Versioned.valueOrNull(backingMap.put(key, value));
Madan Jampanie1065222015-08-17 16:21:51 -070079 }
80
81 @Override
82 public V putIfAbsent(K key, V value) {
Madan Jampanifa242182016-01-22 13:42:54 -080083 return Versioned.valueOrNull(backingMap.putIfAbsent(key, value));
Madan Jampanie1065222015-08-17 16:21:51 -070084 }
85
86 @Override
87 public V remove(Object key) {
Madan Jampanifa242182016-01-22 13:42:54 -080088 return Versioned.valueOrNull(backingMap.remove((K) key));
Madan Jampanie1065222015-08-17 16:21:51 -070089 }
90
91 @Override
92 public boolean remove(Object key, Object value) {
93 return backingMap.remove((K) key, (V) value);
94 }
95
96 @Override
97 public V replace(K key, V value) {
Madan Jampanifa242182016-01-22 13:42:54 -080098 return Versioned.valueOrNull(backingMap.replace(key, value));
Madan Jampanie1065222015-08-17 16:21:51 -070099 }
100
101 @Override
102 public boolean replace(K key, V oldValue, V newValue) {
103 return backingMap.replace(key, oldValue, newValue);
104 }
105
106 @Override
107 public void putAll(Map<? extends K, ? extends V> m) {
108 m.forEach((k, v) -> {
109 backingMap.put(k, v);
110 });
111 }
112
113 @Override
114 public void clear() {
115 backingMap.clear();
116 }
117
118 @Override
119 public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanifa242182016-01-22 13:42:54 -0800120 return Versioned.valueOrNull(backingMap.compute(key, remappingFunction));
Madan Jampanie1065222015-08-17 16:21:51 -0700121 }
122
123 @Override
124 public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
Madan Jampanifa242182016-01-22 13:42:54 -0800125 return Versioned.valueOrNull(backingMap.computeIfAbsent(key, mappingFunction));
Madan Jampanie1065222015-08-17 16:21:51 -0700126 }
127
128 @Override
129 public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanifa242182016-01-22 13:42:54 -0800130 return Versioned.valueOrNull(backingMap.computeIfPresent(key, remappingFunction));
Madan Jampanie1065222015-08-17 16:21:51 -0700131 }
132
133 @Override
134 public Set<K> keySet() {
135 return backingMap.keySet();
136 }
137
138 @Override
139 public Collection<V> values() {
140 return Collections2.transform(backingMap.values(), v -> v.value());
141 }
142
143 @Override
144 public Set<java.util.Map.Entry<K, V>> entrySet() {
145 return backingMap.entrySet()
146 .stream()
147 .map(entry -> Maps.immutableEntry(entry.getKey(), entry.getValue().value()))
148 .collect(Collectors.toSet());
149 }
150
151 @Override
152 public void forEach(BiConsumer<? super K, ? super V> action) {
153 entrySet().forEach(e -> action.accept(e.getKey(), e.getValue()));
154 }
155
156 @Override
157 public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
158 return computeIfPresent(key, (k, v) -> v == null ? value : remappingFunction.apply(v, value));
159 }
160}