Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.store.service.AsyncAtomicCounterMap; |
| 20 | |
| 21 | import java.util.concurrent.CompletableFuture; |
| 22 | import java.util.function.Function; |
| 23 | |
| 24 | /** |
| 25 | * An {@code AsyncAtomicCounterMap} that transcodes keys. |
| 26 | */ |
| 27 | public class TranscodingAsyncAtomicCounterMap<K1, K2> implements AsyncAtomicCounterMap<K1> { |
| 28 | private final AsyncAtomicCounterMap<K2> backingMap; |
| 29 | private final Function<K1, K2> keyEncoder; |
| 30 | private final Function<K2, K1> keyDecoder; |
| 31 | |
| 32 | public TranscodingAsyncAtomicCounterMap(AsyncAtomicCounterMap<K2> backingMap, |
| 33 | Function<K1, K2> keyEncoder, Function<K2, K1> keyDecoder) { |
| 34 | this.backingMap = backingMap; |
| 35 | this.keyEncoder = k -> k == null ? null : keyEncoder.apply(k); |
| 36 | this.keyDecoder = k -> k == null ? null : keyDecoder.apply(k); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public String name() { |
| 41 | return backingMap.name(); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public CompletableFuture<Long> incrementAndGet(K1 key) { |
| 46 | try { |
| 47 | return backingMap.incrementAndGet(keyEncoder.apply(key)); |
| 48 | } catch (Exception e) { |
| 49 | return Tools.exceptionalFuture(e); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public CompletableFuture<Long> decrementAndGet(K1 key) { |
| 55 | try { |
| 56 | return backingMap.decrementAndGet(keyEncoder.apply(key)); |
| 57 | } catch (Exception e) { |
| 58 | return Tools.exceptionalFuture(e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public CompletableFuture<Long> getAndIncrement(K1 key) { |
| 64 | try { |
| 65 | return backingMap.getAndIncrement(keyEncoder.apply(key)); |
| 66 | } catch (Exception e) { |
| 67 | return Tools.exceptionalFuture(e); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<Long> getAndDecrement(K1 key) { |
| 73 | try { |
| 74 | return backingMap.getAndDecrement(keyEncoder.apply(key)); |
| 75 | } catch (Exception e) { |
| 76 | return Tools.exceptionalFuture(e); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public CompletableFuture<Long> addAndGet(K1 key, long delta) { |
| 82 | try { |
| 83 | return backingMap.addAndGet(keyEncoder.apply(key), delta); |
| 84 | } catch (Exception e) { |
| 85 | return Tools.exceptionalFuture(e); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public CompletableFuture<Long> getAndAdd(K1 key, long delta) { |
| 91 | try { |
| 92 | return backingMap.getAndAdd(keyEncoder.apply(key), delta); |
| 93 | } catch (Exception e) { |
| 94 | return Tools.exceptionalFuture(e); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<Long> get(K1 key) { |
| 100 | try { |
| 101 | return backingMap.get(keyEncoder.apply(key)); |
| 102 | } catch (Exception e) { |
| 103 | return Tools.exceptionalFuture(e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public CompletableFuture<Long> put(K1 key, long newValue) { |
| 109 | try { |
| 110 | return backingMap.put(keyEncoder.apply(key), newValue); |
| 111 | } catch (Exception e) { |
| 112 | return Tools.exceptionalFuture(e); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public CompletableFuture<Long> putIfAbsent(K1 key, long newValue) { |
| 118 | try { |
| 119 | return backingMap.putIfAbsent(keyEncoder.apply(key), newValue); |
| 120 | } catch (Exception e) { |
| 121 | return Tools.exceptionalFuture(e); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public CompletableFuture<Boolean> replace(K1 key, long expectedOldValue, long newValue) { |
| 127 | try { |
| 128 | return backingMap.replace(keyEncoder.apply(key), expectedOldValue, newValue); |
| 129 | } catch (Exception e) { |
| 130 | return Tools.exceptionalFuture(e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public CompletableFuture<Long> remove(K1 key) { |
| 136 | try { |
| 137 | return backingMap.remove(keyEncoder.apply(key)); |
| 138 | } catch (Exception e) { |
| 139 | return Tools.exceptionalFuture(e); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<Boolean> remove(K1 key, long value) { |
| 145 | try { |
| 146 | return backingMap.remove(keyEncoder.apply(key), value); |
| 147 | } catch (Exception e) { |
| 148 | return Tools.exceptionalFuture(e); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public CompletableFuture<Integer> size() { |
| 154 | try { |
| 155 | return backingMap.size(); |
| 156 | } catch (Exception e) { |
| 157 | return Tools.exceptionalFuture(e); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public CompletableFuture<Boolean> isEmpty() { |
| 163 | try { |
| 164 | return backingMap.isEmpty(); |
| 165 | } catch (Exception e) { |
| 166 | return Tools.exceptionalFuture(e); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public CompletableFuture<Void> clear() { |
| 172 | try { |
| 173 | return backingMap.clear(); |
| 174 | } catch (Exception e) { |
| 175 | return Tools.exceptionalFuture(e); |
| 176 | } |
| 177 | } |
| 178 | } |