blob: 33757e390f81a5d5091202054ad66533a2fdafdf [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
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;
HIGUCHI Yutadc4394c2016-01-29 15:35:10 -080019import java.util.Iterator;
Madan Jampanie1065222015-08-17 16:21:51 -070020import java.util.Map;
21import java.util.Set;
22import java.util.function.BiConsumer;
23import java.util.function.BiFunction;
24import java.util.function.Function;
25import java.util.stream.Collectors;
26
27import org.onosproject.store.service.ConsistentMap;
28import org.onosproject.store.service.Versioned;
29
30import com.google.common.collect.Collections2;
31import com.google.common.collect.Maps;
32
33/**
Madan Jampanifa242182016-01-22 13:42:54 -080034 * Standard java {@link Map} backed by a {@link ConsistentMap}.
Madan Jampanie1065222015-08-17 16:21:51 -070035 *
36 * @param <K> key type
37 * @param <V> value type
38 */
39public final class ConsistentMapBackedJavaMap<K, V> implements Map<K, V> {
40
41 private final ConsistentMap<K, V> backingMap;
42
43 public ConsistentMapBackedJavaMap(ConsistentMap<K, V> backingMap) {
44 this.backingMap = backingMap;
45 }
46
47 @Override
48 public int size() {
49 return backingMap.size();
50 }
51
52 @Override
53 public boolean isEmpty() {
54 return backingMap.isEmpty();
55 }
56
57 @Override
58 public boolean containsKey(Object key) {
59 return backingMap.containsKey((K) key);
60 }
61
62 @Override
63 public boolean containsValue(Object value) {
64 return backingMap.containsValue((V) value);
65 }
66
67 @Override
68 public V get(Object key) {
Madan Jampanifa242182016-01-22 13:42:54 -080069 return Versioned.valueOrNull(backingMap.get((K) key));
Madan Jampanie1065222015-08-17 16:21:51 -070070 }
71
72 @Override
73 public V getOrDefault(Object key, V defaultValue) {
74 return Versioned.valueOrElse(backingMap.get((K) key), defaultValue);
75 }
76
77 @Override
78 public V put(K key, V value) {
Madan Jampanifa242182016-01-22 13:42:54 -080079 return Versioned.valueOrNull(backingMap.put(key, value));
Madan Jampanie1065222015-08-17 16:21:51 -070080 }
81
82 @Override
83 public V putIfAbsent(K key, V value) {
Madan Jampanifa242182016-01-22 13:42:54 -080084 return Versioned.valueOrNull(backingMap.putIfAbsent(key, value));
Madan Jampanie1065222015-08-17 16:21:51 -070085 }
86
87 @Override
88 public V remove(Object key) {
Madan Jampanifa242182016-01-22 13:42:54 -080089 return Versioned.valueOrNull(backingMap.remove((K) key));
Madan Jampanie1065222015-08-17 16:21:51 -070090 }
91
92 @Override
93 public boolean remove(Object key, Object value) {
94 return backingMap.remove((K) key, (V) value);
95 }
96
97 @Override
98 public V replace(K key, V value) {
Madan Jampanifa242182016-01-22 13:42:54 -080099 return Versioned.valueOrNull(backingMap.replace(key, value));
Madan Jampanie1065222015-08-17 16:21:51 -0700100 }
101
102 @Override
103 public boolean replace(K key, V oldValue, V newValue) {
104 return backingMap.replace(key, oldValue, newValue);
105 }
106
107 @Override
108 public void putAll(Map<? extends K, ? extends V> m) {
109 m.forEach((k, v) -> {
110 backingMap.put(k, v);
111 });
112 }
113
114 @Override
115 public void clear() {
116 backingMap.clear();
117 }
118
119 @Override
120 public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanifa242182016-01-22 13:42:54 -0800121 return Versioned.valueOrNull(backingMap.compute(key, remappingFunction));
Madan Jampanie1065222015-08-17 16:21:51 -0700122 }
123
124 @Override
125 public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
Madan Jampanifa242182016-01-22 13:42:54 -0800126 return Versioned.valueOrNull(backingMap.computeIfAbsent(key, mappingFunction));
Madan Jampanie1065222015-08-17 16:21:51 -0700127 }
128
129 @Override
130 public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanifa242182016-01-22 13:42:54 -0800131 return Versioned.valueOrNull(backingMap.computeIfPresent(key, remappingFunction));
Madan Jampanie1065222015-08-17 16:21:51 -0700132 }
133
134 @Override
135 public Set<K> keySet() {
136 return backingMap.keySet();
137 }
138
139 @Override
140 public Collection<V> values() {
141 return Collections2.transform(backingMap.values(), v -> v.value());
142 }
143
144 @Override
145 public Set<java.util.Map.Entry<K, V>> entrySet() {
146 return backingMap.entrySet()
147 .stream()
148 .map(entry -> Maps.immutableEntry(entry.getKey(), entry.getValue().value()))
149 .collect(Collectors.toSet());
150 }
151
152 @Override
HIGUCHI Yutadc4394c2016-01-29 15:35:10 -0800153 public String toString() {
154 // Map like output
155 StringBuilder sb = new StringBuilder();
156 sb.append('{');
157 Iterator<Entry<K, Versioned<V>>> it = backingMap.entrySet().iterator();
158 while (it.hasNext()) {
159 Entry<K, Versioned<V>> entry = it.next();
160 sb.append(entry.getKey()).append('=').append(entry.getValue().value());
161 if (it.hasNext()) {
162 sb.append(',').append(' ');
163 }
164 }
165 sb.append('}');
166 return sb.toString();
167 }
168
169 @Override
Madan Jampanie1065222015-08-17 16:21:51 -0700170 public void forEach(BiConsumer<? super K, ? super V> action) {
171 entrySet().forEach(e -> action.accept(e.getKey(), e.getValue()));
172 }
173
174 @Override
175 public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
176 return computeIfPresent(key, (k, v) -> v == null ? value : remappingFunction.apply(v, value));
177 }
178}