Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | import java.util.function.BiConsumer; |
| 22 | import java.util.function.BiFunction; |
| 23 | import java.util.function.Function; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | import org.onosproject.store.service.ConsistentMap; |
| 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
| 29 | import com.google.common.collect.Collections2; |
| 30 | import com.google.common.collect.Maps; |
| 31 | |
| 32 | /** |
| 33 | * Standard java Map backed by a ConsistentMap. |
| 34 | * |
| 35 | * @param <K> key type |
| 36 | * @param <V> value type |
| 37 | */ |
| 38 | public 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) { |
| 68 | return Versioned.valueOrElse(backingMap.get((K) key), null); |
| 69 | } |
| 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) { |
| 78 | return Versioned.valueOrElse(backingMap.put(key, value), null); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public V putIfAbsent(K key, V value) { |
| 83 | return Versioned.valueOrElse(backingMap.putIfAbsent(key, value), null); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public V remove(Object key) { |
| 88 | return Versioned.valueOrElse(backingMap.remove((K) key), null); |
| 89 | } |
| 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) { |
| 98 | throw new UnsupportedOperationException(); |
| 99 | } |
| 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) { |
| 120 | return Versioned.valueOrElse(backingMap.compute(key, remappingFunction), null); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { |
| 125 | return Versioned.valueOrElse(backingMap.computeIfAbsent(key, mappingFunction), null); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 130 | return Versioned.valueOrElse(backingMap.computeIfPresent(key, remappingFunction), null); |
| 131 | } |
| 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 | } |