blob: e670d40d059bb8b2217ca2b98f45ff840dbd091b [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani5e5b3d62016-02-01 16:03:33 -08003 *
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.resources.impl;
17
18import io.atomix.variables.DistributedLong;
19
20import java.util.concurrent.CompletableFuture;
21
22import org.onosproject.store.service.AsyncAtomicCounter;
23
24/**
25 * {@code AsyncAtomicCounter} implementation backed by Atomix
26 * {@link DistributedLong}.
27 */
28public 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 Jampanie14a09c2016-02-11 10:43:21 -080074 public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) {
Madan Jampani5e5b3d62016-02-01 16:03:33 -080075 return distLong.compareAndSet(expectedValue, updateValue);
76 }
77}