blob: 36cb9f1a6940088fa4398c4f3a3d36515ab00ca2 [file] [log] [blame]
Ray Milkey24e60b32015-08-12 11:39:54 -07001/*
Ray Milkey2eb91672018-04-24 10:07:52 -07002 * Copyright 2018-present Open Networking Foundation
Ray Milkey24e60b32015-08-12 11:39:54 -07003 *
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 */
Ray Milkey2eb91672018-04-24 10:07:52 -070016package org.onosproject.store.primitives;
17
18import org.onosproject.store.service.AsyncAtomicCounter;
19import org.onosproject.store.service.AtomicCounterBuilder;
Ray Milkey24e60b32015-08-12 11:39:54 -070020
Madan Jampanie17d3282016-02-03 15:30:57 -080021import java.util.concurrent.CompletableFuture;
Ray Milkey24e60b32015-08-12 11:39:54 -070022import java.util.concurrent.atomic.AtomicLong;
23
24/**
25 * Test implementation of atomic counter.
26 */
Ray Milkey2eb91672018-04-24 10:07:52 -070027public class AsyncAtomicCounterAdapter implements AsyncAtomicCounter {
28 private final AtomicLong value;
Ray Milkey24e60b32015-08-12 11:39:54 -070029
Madan Jampania090a112016-01-18 16:38:17 -080030 @Override
31 public String name() {
32 return null;
33 }
34
Ray Milkey2eb91672018-04-24 10:07:52 -070035 AsyncAtomicCounterAdapter() {
Ray Milkey24e60b32015-08-12 11:39:54 -070036 value = new AtomicLong();
37 }
38
39 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080040 public CompletableFuture<Long> incrementAndGet() {
41 return CompletableFuture.completedFuture(value.incrementAndGet());
Ray Milkey24e60b32015-08-12 11:39:54 -070042 }
43
44 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080045 public CompletableFuture<Long> getAndIncrement() {
46 return CompletableFuture.completedFuture(value.getAndIncrement());
Ray Milkey24e60b32015-08-12 11:39:54 -070047 }
48
49 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080050 public CompletableFuture<Long> getAndAdd(long delta) {
51 return CompletableFuture.completedFuture(value.getAndAdd(delta));
Ray Milkey24e60b32015-08-12 11:39:54 -070052 }
53
54 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080055 public CompletableFuture<Long> addAndGet(long delta) {
56 return CompletableFuture.completedFuture(value.addAndGet(delta));
Ray Milkey24e60b32015-08-12 11:39:54 -070057 }
58
59 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080060 public CompletableFuture<Void> set(long value) {
andreafd912ac2015-10-02 14:58:35 -070061 this.value.set(value);
Madan Jampanie17d3282016-02-03 15:30:57 -080062 return CompletableFuture.completedFuture(null);
andreafd912ac2015-10-02 14:58:35 -070063 }
64
65 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080066 public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) {
67 return CompletableFuture.completedFuture(value.compareAndSet(expectedValue, updateValue));
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070068 }
69
70 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080071 public CompletableFuture<Long> get() {
72 return CompletableFuture.completedFuture(value.get());
Ray Milkey24e60b32015-08-12 11:39:54 -070073 }
74
75 public static AtomicCounterBuilder builder() {
76 return new Builder();
77 }
78
Madan Jampanie17d3282016-02-03 15:30:57 -080079 public static class Builder extends AtomicCounterBuilder {
Ray Milkey24e60b32015-08-12 11:39:54 -070080 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080081 public AsyncAtomicCounter build() {
Ray Milkey2eb91672018-04-24 10:07:52 -070082 return new AsyncAtomicCounterAdapter();
Ray Milkey24e60b32015-08-12 11:39:54 -070083 }
84 }
85}