Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.primitives.impl; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | |
| 21 | import com.google.common.collect.Maps; |
| 22 | import org.onosproject.store.service.AsyncAtomicValue; |
| 23 | import org.onosproject.store.service.AtomicValueEvent; |
| 24 | import org.onosproject.store.service.AtomicValueEventListener; |
| 25 | |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 26 | import static org.onosproject.store.atomix.primitives.impl.AtomixFutures.adaptFuture; |
| 27 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Atomix atomic value. |
| 30 | */ |
| 31 | public class AtomixAtomicValue<V> implements AsyncAtomicValue<V> { |
| 32 | private final io.atomix.core.value.AsyncAtomicValue<V> atomixValue; |
| 33 | private final Map<AtomicValueEventListener<V>, io.atomix.core.value.AtomicValueEventListener<V>> listenerMap = |
| 34 | Maps.newIdentityHashMap(); |
| 35 | |
| 36 | public AtomixAtomicValue(io.atomix.core.value.AsyncAtomicValue<V> atomixValue) { |
| 37 | this.atomixValue = atomixValue; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String name() { |
| 42 | return atomixValue.name(); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public CompletableFuture<Boolean> compareAndSet(V expect, V update) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 47 | return adaptFuture(atomixValue.compareAndSet(expect, update)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public CompletableFuture<V> get() { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 52 | return adaptFuture(atomixValue.get()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public CompletableFuture<V> getAndSet(V value) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 57 | return adaptFuture(atomixValue.getAndSet(value)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Void> set(V value) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 62 | return adaptFuture(atomixValue.set(value)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public synchronized CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener) { |
| 67 | io.atomix.core.value.AtomicValueEventListener<V> atomixListener = event -> |
| 68 | listener.event(new AtomicValueEvent<V>( |
| 69 | name(), |
| 70 | event.newValue(), |
| 71 | event.oldValue())); |
| 72 | listenerMap.put(listener, atomixListener); |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 73 | return adaptFuture(atomixValue.addListener(atomixListener)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public synchronized CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener) { |
| 78 | io.atomix.core.value.AtomicValueEventListener<V> atomixListener = listenerMap.remove(listener); |
| 79 | if (atomixListener != null) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 80 | return adaptFuture(atomixValue.removeListener(atomixListener)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 81 | } |
| 82 | return CompletableFuture.completedFuture(null); |
| 83 | } |
Jordan Halterman | 35a02ea | 2018-08-22 11:57:32 -0700 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | public CompletableFuture<Void> destroy() { |
| 87 | return adaptFuture(atomixValue.delete()); |
| 88 | } |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 89 | } |