blob: 40eacc6f30d35fb3124e4d1e82d4ae6fb933c88d [file] [log] [blame]
Jordan Halterman544d1d52017-04-19 23:45:12 -07001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.Executor;
21
22import com.google.common.collect.Maps;
23import org.onlab.util.Tools;
24import org.onosproject.store.service.AsyncAtomicValue;
25import org.onosproject.store.service.AtomicValueEventListener;
26
27/**
28 * {@link AsyncAtomicValue} that executes asynchronous callbacks on a user provided
29 * {@link Executor}.
30 */
31public class ExecutingAsyncAtomicValue<V> extends ExecutingDistributedPrimitive implements AsyncAtomicValue<V> {
32 private final AsyncAtomicValue<V> delegateValue;
33 private final Executor executor;
34 private final Map<AtomicValueEventListener<V>, AtomicValueEventListener<V>> listenerMap = Maps.newConcurrentMap();
35
36 public ExecutingAsyncAtomicValue(AsyncAtomicValue<V> delegateValue, Executor executor) {
37 super(delegateValue, executor);
38 this.delegateValue = delegateValue;
39 this.executor = executor;
40 }
41
42 @Override
43 public CompletableFuture<Boolean> compareAndSet(V expect, V update) {
44 return Tools.asyncFuture(delegateValue.compareAndSet(expect, update), executor);
45 }
46
47 @Override
48 public CompletableFuture<V> get() {
49 return Tools.asyncFuture(delegateValue.get(), executor);
50 }
51
52 @Override
53 public CompletableFuture<V> getAndSet(V value) {
54 return Tools.asyncFuture(delegateValue.getAndSet(value), executor);
55 }
56
57 @Override
58 public CompletableFuture<Void> set(V value) {
59 return Tools.asyncFuture(delegateValue.set(value), executor);
60 }
61
62 @Override
63 public CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener) {
64 AtomicValueEventListener<V> wrappedListener = e -> executor.execute(() -> listener.event(e));
65 listenerMap.put(listener, wrappedListener);
66 return Tools.asyncFuture(delegateValue.addListener(wrappedListener), executor);
67 }
68
69 @Override
70 public CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener) {
71 AtomicValueEventListener<V> wrappedListener = listenerMap.remove(listener);
72 if (wrappedListener != null) {
73 return Tools.asyncFuture(delegateValue.removeListener(wrappedListener), executor);
74 }
75 return CompletableFuture.completedFuture(null);
76 }
77}