Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.resources.impl; |
| 17 | |
| 18 | import io.atomix.variables.DistributedLong; |
| 19 | |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | |
| 22 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 23 | |
| 24 | /** |
| 25 | * {@code AsyncAtomicCounter} implementation backed by Atomix |
| 26 | * {@link DistributedLong}. |
| 27 | */ |
| 28 | public class AtomixCounter implements AsyncAtomicCounter { |
| 29 | |
| 30 | private final String name; |
| 31 | private final DistributedLong distLong; |
| 32 | |
| 33 | public AtomixCounter(String name, DistributedLong distLong) { |
| 34 | this.name = name; |
| 35 | this.distLong = distLong; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public String name() { |
| 40 | return name; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public CompletableFuture<Long> incrementAndGet() { |
| 45 | return distLong.incrementAndGet(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public CompletableFuture<Long> getAndIncrement() { |
| 50 | return distLong.getAndIncrement(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public CompletableFuture<Long> getAndAdd(long delta) { |
| 55 | return distLong.getAndAdd(delta); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public CompletableFuture<Long> addAndGet(long delta) { |
| 60 | return distLong.addAndGet(delta); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public CompletableFuture<Long> get() { |
| 65 | return distLong.get(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public CompletableFuture<Void> set(long value) { |
| 70 | return distLong.set(value); |
| 71 | } |
| 72 | |
| 73 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 74 | public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 75 | return distLong.compareAndSet(expectedValue, updateValue); |
| 76 | } |
| 77 | } |