blob: 504fa75a7151fef4b166e0a5e7c724a35995b7d7 [file] [log] [blame]
Jordan Halterman9bdc24f2017-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.concurrent.CompletableFuture;
19import java.util.concurrent.Executor;
20
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070021import org.onosproject.store.service.AsyncAtomicCounter;
22
23/**
24 * {@link AsyncAtomicCounter} that executes asynchronous callbacks on a user provided
25 * {@link Executor}.
26 */
27public class ExecutingAsyncAtomicCounter extends ExecutingDistributedPrimitive implements AsyncAtomicCounter {
28 private final AsyncAtomicCounter delegateCounter;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070029
Jordan Halterman046faeb2017-05-01 15:10:13 -070030 public ExecutingAsyncAtomicCounter(
31 AsyncAtomicCounter delegateCounter, Executor orderedExecutor, Executor threadPoolExecutor) {
32 super(delegateCounter, orderedExecutor, threadPoolExecutor);
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070033 this.delegateCounter = delegateCounter;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070034 }
35
36 @Override
37 public CompletableFuture<Long> incrementAndGet() {
Jordan Halterman046faeb2017-05-01 15:10:13 -070038 return asyncFuture(delegateCounter.incrementAndGet());
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070039 }
40
41 @Override
42 public CompletableFuture<Long> getAndIncrement() {
Jordan Halterman046faeb2017-05-01 15:10:13 -070043 return asyncFuture(delegateCounter.getAndIncrement());
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070044 }
45
46 @Override
47 public CompletableFuture<Long> getAndAdd(long delta) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070048 return asyncFuture(delegateCounter.getAndAdd(delta));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070049 }
50
51 @Override
52 public CompletableFuture<Long> addAndGet(long delta) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070053 return asyncFuture(delegateCounter.addAndGet(delta));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070054 }
55
56 @Override
57 public CompletableFuture<Long> get() {
Jordan Halterman046faeb2017-05-01 15:10:13 -070058 return asyncFuture(delegateCounter.get());
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070059 }
60
61 @Override
62 public CompletableFuture<Void> set(long value) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070063 return asyncFuture(delegateCounter.set(value));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070064 }
65
66 @Override
67 public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070068 return asyncFuture(delegateCounter.compareAndSet(expectedValue, updateValue));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070069 }
70}