Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.concurrent.Executor; |
| 20 | |
| 21 | import org.onlab.util.Tools; |
| 22 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 23 | |
| 24 | /** |
| 25 | * {@link AsyncAtomicCounter} that executes asynchronous callbacks on a user provided |
| 26 | * {@link Executor}. |
| 27 | */ |
| 28 | public class ExecutingAsyncAtomicCounter extends ExecutingDistributedPrimitive implements AsyncAtomicCounter { |
| 29 | private final AsyncAtomicCounter delegateCounter; |
| 30 | private final Executor executor; |
| 31 | |
| 32 | public ExecutingAsyncAtomicCounter(AsyncAtomicCounter delegateCounter, Executor executor) { |
| 33 | super(delegateCounter, executor); |
| 34 | this.delegateCounter = delegateCounter; |
| 35 | this.executor = executor; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public CompletableFuture<Long> incrementAndGet() { |
| 40 | return Tools.asyncFuture(delegateCounter.incrementAndGet(), executor); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public CompletableFuture<Long> getAndIncrement() { |
| 45 | return Tools.asyncFuture(delegateCounter.getAndIncrement(), executor); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public CompletableFuture<Long> getAndAdd(long delta) { |
| 50 | return Tools.asyncFuture(delegateCounter.getAndAdd(delta), executor); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public CompletableFuture<Long> addAndGet(long delta) { |
| 55 | return Tools.asyncFuture(delegateCounter.addAndGet(delta), executor); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public CompletableFuture<Long> get() { |
| 60 | return Tools.asyncFuture(delegateCounter.get(), executor); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public CompletableFuture<Void> set(long value) { |
| 65 | return Tools.asyncFuture(delegateCounter.set(value), executor); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) { |
| 70 | return Tools.asyncFuture(delegateCounter.compareAndSet(expectedValue, updateValue), executor); |
| 71 | } |
| 72 | } |