Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | import java.util.concurrent.CompletableFuture; |
| 22 | import java.util.concurrent.Executor; |
| 23 | import java.util.function.BiFunction; |
| 24 | import java.util.function.Predicate; |
| 25 | |
| 26 | import org.onlab.util.Tools; |
| 27 | import org.onosproject.store.primitives.MapUpdate; |
| 28 | import org.onosproject.store.primitives.TransactionId; |
| 29 | import org.onosproject.store.service.AsyncConsistentMap; |
| 30 | import org.onosproject.store.service.MapEventListener; |
| 31 | import org.onosproject.store.service.TransactionLog; |
| 32 | import org.onosproject.store.service.Version; |
| 33 | import org.onosproject.store.service.Versioned; |
| 34 | |
| 35 | /** |
| 36 | * An {@link org.onosproject.store.service.AsyncConsistentMap} that completes asynchronous calls on a provided |
| 37 | * {@link Executor}. |
| 38 | */ |
| 39 | public class ExecutingAsyncConsistentMap<K, V> |
| 40 | extends ExecutingDistributedPrimitive implements AsyncConsistentMap<K, V> { |
| 41 | private final AsyncConsistentMap<K, V> delegateMap; |
| 42 | private final Executor executor; |
| 43 | |
| 44 | public ExecutingAsyncConsistentMap(AsyncConsistentMap<K, V> delegateMap, Executor executor) { |
| 45 | super(delegateMap, executor); |
| 46 | this.delegateMap = delegateMap; |
| 47 | this.executor = executor; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public CompletableFuture<Integer> size() { |
| 52 | return Tools.asyncFuture(delegateMap.size(), executor); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public CompletableFuture<Boolean> containsKey(K key) { |
| 57 | return Tools.asyncFuture(delegateMap.containsKey(key), executor); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Boolean> containsValue(V value) { |
| 62 | return Tools.asyncFuture(delegateMap.containsValue(value), executor); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<Versioned<V>> get(K key) { |
| 67 | return Tools.asyncFuture(delegateMap.get(key), executor); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue) { |
| 72 | return Tools.asyncFuture(delegateMap.getOrDefault(key, defaultValue), executor); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public CompletableFuture<Versioned<V>> computeIf( |
| 77 | K key, Predicate<? super V> condition, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 78 | return Tools.asyncFuture(delegateMap.computeIf(key, condition, remappingFunction), executor); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 83 | return Tools.asyncFuture(delegateMap.put(key, value), executor); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 88 | return Tools.asyncFuture(delegateMap.putAndGet(key, value), executor); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 93 | return Tools.asyncFuture(delegateMap.remove(key), executor); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Void> clear() { |
| 98 | return Tools.asyncFuture(delegateMap.clear(), executor); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public CompletableFuture<Set<K>> keySet() { |
| 103 | return Tools.asyncFuture(delegateMap.keySet(), executor); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public CompletableFuture<Collection<Versioned<V>>> values() { |
| 108 | return Tools.asyncFuture(delegateMap.values(), executor); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet() { |
| 113 | return Tools.asyncFuture(delegateMap.entrySet(), executor); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) { |
| 118 | return Tools.asyncFuture(delegateMap.putIfAbsent(key, value), executor); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 123 | return Tools.asyncFuture(delegateMap.remove(key, value), executor); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 128 | return Tools.asyncFuture(delegateMap.remove(key, version), executor); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 133 | return Tools.asyncFuture(delegateMap.replace(key, value), executor); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 138 | return Tools.asyncFuture(delegateMap.replace(key, oldValue, newValue), executor); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
| 143 | return Tools.asyncFuture(delegateMap.replace(key, oldVersion, newValue), executor); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 148 | return Tools.asyncFuture(delegateMap.begin(transactionId), executor); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public CompletableFuture<Boolean> prepare(TransactionLog<MapUpdate<K, V>> transactionLog) { |
| 153 | return Tools.asyncFuture(delegateMap.prepare(transactionLog), executor); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 158 | return Tools.asyncFuture(delegateMap.commit(transactionId), executor); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 163 | return Tools.asyncFuture(delegateMap.rollback(transactionId), executor); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<MapUpdate<K, V>> transactionLog) { |
| 168 | return Tools.asyncFuture(delegateMap.prepareAndCommit(transactionLog), executor); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CompletableFuture<Void> addListener(MapEventListener<K, V> listener) { |
| 173 | return addListener(listener, executor); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor) { |
| 178 | return Tools.asyncFuture(delegateMap.addListener(listener, executor), this.executor); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) { |
| 183 | return Tools.asyncFuture(delegateMap.removeListener(listener), executor); |
| 184 | } |
| 185 | } |