blob: 48c524ab26307df02f61f293ad94c2fd0ad3a806 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
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 Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
20
21import com.google.common.collect.Maps;
22import org.onosproject.store.service.AsyncAtomicValue;
23import org.onosproject.store.service.AtomicValueEvent;
24import org.onosproject.store.service.AtomicValueEventListener;
25
Jordan Halterman6cf60c32018-08-15 01:22:51 -070026import static org.onosproject.store.atomix.primitives.impl.AtomixFutures.adaptFuture;
27
Jordan Halterman00e92da2018-05-22 23:05:52 -070028/**
29 * Atomix atomic value.
30 */
31public 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 Halterman6cf60c32018-08-15 01:22:51 -070047 return adaptFuture(atomixValue.compareAndSet(expect, update));
Jordan Halterman00e92da2018-05-22 23:05:52 -070048 }
49
50 @Override
51 public CompletableFuture<V> get() {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070052 return adaptFuture(atomixValue.get());
Jordan Halterman00e92da2018-05-22 23:05:52 -070053 }
54
55 @Override
56 public CompletableFuture<V> getAndSet(V value) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070057 return adaptFuture(atomixValue.getAndSet(value));
Jordan Halterman00e92da2018-05-22 23:05:52 -070058 }
59
60 @Override
61 public CompletableFuture<Void> set(V value) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070062 return adaptFuture(atomixValue.set(value));
Jordan Halterman00e92da2018-05-22 23:05:52 -070063 }
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 Halterman6cf60c32018-08-15 01:22:51 -070073 return adaptFuture(atomixValue.addListener(atomixListener));
Jordan Halterman00e92da2018-05-22 23:05:52 -070074 }
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 Halterman6cf60c32018-08-15 01:22:51 -070080 return adaptFuture(atomixValue.removeListener(atomixListener));
Jordan Halterman00e92da2018-05-22 23:05:52 -070081 }
82 return CompletableFuture.completedFuture(null);
83 }
Jordan Halterman35a02ea2018-08-22 11:57:32 -070084
85 @Override
86 public CompletableFuture<Void> destroy() {
87 return adaptFuture(atomixValue.delete());
88 }
Jordan Halterman00e92da2018-05-22 23:05:52 -070089}