Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives; |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Collection; |
HIGUCHI Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 19 | import java.util.Iterator; |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | import java.util.function.BiConsumer; |
| 23 | import java.util.function.BiFunction; |
| 24 | import java.util.function.Function; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
| 27 | import org.onosproject.store.service.ConsistentMap; |
| 28 | import org.onosproject.store.service.Versioned; |
| 29 | |
| 30 | import com.google.common.collect.Collections2; |
| 31 | import com.google.common.collect.Maps; |
| 32 | |
| 33 | /** |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 34 | * Standard java {@link Map} backed by a {@link ConsistentMap}. |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 35 | * |
| 36 | * @param <K> key type |
| 37 | * @param <V> value type |
| 38 | */ |
| 39 | public 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 Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 69 | return Versioned.valueOrNull(backingMap.get((K) key)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 70 | } |
| 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 Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 79 | return Versioned.valueOrNull(backingMap.put(key, value)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public V putIfAbsent(K key, V value) { |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 84 | return Versioned.valueOrNull(backingMap.putIfAbsent(key, value)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public V remove(Object key) { |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 89 | return Versioned.valueOrNull(backingMap.remove((K) key)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 90 | } |
| 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 Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 99 | return Versioned.valueOrNull(backingMap.replace(key, value)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 100 | } |
| 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 Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 121 | return Versioned.valueOrNull(backingMap.compute(key, remappingFunction)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 126 | return Versioned.valueOrNull(backingMap.computeIfAbsent(key, mappingFunction)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 131 | return Versioned.valueOrNull(backingMap.computeIfPresent(key, remappingFunction)); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 132 | } |
| 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 Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 153 | 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 Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 170 | 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 | } |