Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import org.onosproject.store.service.AsyncConsistentMapAdapter; |
| 19 | import org.onosproject.store.service.MapEventListener; |
| 20 | import org.onosproject.store.service.Versioned; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Arrays; |
| 24 | import java.util.Collection; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Objects; |
| 29 | import java.util.Set; |
| 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.Executor; |
| 32 | import java.util.function.BiFunction; |
| 33 | import java.util.function.Predicate; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
| 36 | /*Simple template for asynchronous map |
| 37 | Serializer must be defined independently |
| 38 | */ |
| 39 | public class AsyncConsistentMapMock<K, V> extends AsyncConsistentMapAdapter<K, V> { |
| 40 | private final Map<K, V> baseMap = new HashMap<>(); |
| 41 | private final List<MapEventListener<K, V>> listeners; |
| 42 | |
| 43 | Versioned<V> makeVersioned(V v) { |
| 44 | return new Versioned<>(v, 1, 0); |
| 45 | } |
| 46 | |
| 47 | public AsyncConsistentMapMock() { |
| 48 | this.listeners = new ArrayList<>(); |
| 49 | } |
| 50 | |
| 51 | public CompletableFuture<Integer> size() { |
| 52 | return CompletableFuture.completedFuture(baseMap.size()); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public CompletableFuture<Boolean> containsKey(K key) { |
| 57 | return CompletableFuture.completedFuture(baseMap.containsKey(key)); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Boolean> containsValue(V value) { |
| 62 | return CompletableFuture.completedFuture(baseMap.values() |
| 63 | .stream() |
| 64 | .anyMatch(v -> { |
| 65 | if (v instanceof byte[] && value instanceof byte[]) { |
| 66 | return Arrays.equals((byte[]) v, (byte[]) value); |
| 67 | } else { |
| 68 | return Objects.equals(v, value); |
| 69 | } |
| 70 | })); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public CompletableFuture<Versioned<V>> get(K key) { |
| 75 | return CompletableFuture.completedFuture(makeVersioned(baseMap.get(key))); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue) { |
| 80 | return CompletableFuture.completedFuture(makeVersioned(baseMap.getOrDefault(key, defaultValue))); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<Versioned<V>> |
| 85 | computeIf(K key, Predicate<? super V> condition, |
| 86 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 87 | |
| 88 | V value = baseMap.get(key); |
| 89 | |
| 90 | if (condition.test(value)) { |
| 91 | value = baseMap.compute(key, remappingFunction); |
| 92 | } |
| 93 | return CompletableFuture.completedFuture(makeVersioned(value)); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 98 | return CompletableFuture.completedFuture(makeVersioned(baseMap.put(key, value))); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 103 | return CompletableFuture.completedFuture(makeVersioned(baseMap.put(key, value))); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 108 | return CompletableFuture.completedFuture(makeVersioned(baseMap.remove(key))); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public CompletableFuture<Void> clear() { |
| 113 | baseMap.clear(); |
| 114 | return CompletableFuture.allOf(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public CompletableFuture<Set<K>> keySet() { |
| 119 | return CompletableFuture.completedFuture(baseMap.keySet()); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public CompletableFuture<Collection<Versioned<V>>> values() { |
| 124 | Set<Versioned<V>> valuesAsVersionedCollection = |
| 125 | baseMap.values().stream().map(this::makeVersioned) |
| 126 | .collect(Collectors.toSet()); |
| 127 | return CompletableFuture.completedFuture(valuesAsVersionedCollection); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet() { |
| 132 | Map<K, Versioned<V>> valuesAsVersionedMap = new HashMap<>(); |
| 133 | baseMap.entrySet() |
| 134 | .forEach(e -> valuesAsVersionedMap.put(e.getKey(), |
| 135 | makeVersioned(e.getValue()))); |
| 136 | return CompletableFuture.completedFuture(valuesAsVersionedMap.entrySet()); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) { |
| 141 | return CompletableFuture.completedFuture(makeVersioned(baseMap.putIfAbsent(key, value))); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 146 | return CompletableFuture.completedFuture(baseMap.remove(key, value)); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 151 | Object value = baseMap.remove(key); |
| 152 | return CompletableFuture.completedFuture(value != null); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 157 | return CompletableFuture.completedFuture(makeVersioned(baseMap.replace(key, value))); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 162 | V current = baseMap.get(key); |
| 163 | if (current instanceof byte[] && oldValue instanceof byte[]) { |
| 164 | baseMap.put(key, newValue); |
| 165 | return CompletableFuture.completedFuture(Arrays.equals((byte[]) current, (byte[]) oldValue)); |
| 166 | } else { |
| 167 | return CompletableFuture.completedFuture(Objects.equals(current, oldValue)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
| 173 | return CompletableFuture.completedFuture(baseMap.replace(key, newValue) != null); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor e) { |
| 178 | listeners.add(listener); |
| 179 | return CompletableFuture.allOf(); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) { |
| 184 | listeners.remove(listener); |
| 185 | return CompletableFuture.allOf(); |
| 186 | } |
| 187 | } |
| 188 | |