blob: 64b227dfc88279813ef5cf8d25345954a035d570 [file] [log] [blame]
Ray Milkey24e60b32015-08-12 11:39:54 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 */
16package org.onosproject.store.service;
17
Madan Jampanie17d3282016-02-03 15:30:57 -080018import java.util.concurrent.CompletableFuture;
Ray Milkey24e60b32015-08-12 11:39:54 -070019import java.util.concurrent.atomic.AtomicLong;
20
21/**
22 * Test implementation of atomic counter.
23 */
Madan Jampanie17d3282016-02-03 15:30:57 -080024public final class TestAtomicCounter implements AsyncAtomicCounter {
Ray Milkey24e60b32015-08-12 11:39:54 -070025 final AtomicLong value;
26
Madan Jampania090a112016-01-18 16:38:17 -080027 @Override
28 public String name() {
29 return null;
30 }
31
Ray Milkey24e60b32015-08-12 11:39:54 -070032 private TestAtomicCounter() {
33 value = new AtomicLong();
34 }
35
36 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080037 public CompletableFuture<Long> incrementAndGet() {
38 return CompletableFuture.completedFuture(value.incrementAndGet());
Ray Milkey24e60b32015-08-12 11:39:54 -070039 }
40
41 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080042 public CompletableFuture<Long> getAndIncrement() {
43 return CompletableFuture.completedFuture(value.getAndIncrement());
Ray Milkey24e60b32015-08-12 11:39:54 -070044 }
45
46 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080047 public CompletableFuture<Long> getAndAdd(long delta) {
48 return CompletableFuture.completedFuture(value.getAndAdd(delta));
Ray Milkey24e60b32015-08-12 11:39:54 -070049 }
50
51 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080052 public CompletableFuture<Long> addAndGet(long delta) {
53 return CompletableFuture.completedFuture(value.addAndGet(delta));
Ray Milkey24e60b32015-08-12 11:39:54 -070054 }
55
56 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080057 public CompletableFuture<Void> set(long value) {
andreafd912ac2015-10-02 14:58:35 -070058 this.value.set(value);
Madan Jampanie17d3282016-02-03 15:30:57 -080059 return CompletableFuture.completedFuture(null);
andreafd912ac2015-10-02 14:58:35 -070060 }
61
62 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080063 public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) {
64 return CompletableFuture.completedFuture(value.compareAndSet(expectedValue, updateValue));
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070065 }
66
67 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080068 public CompletableFuture<Long> get() {
69 return CompletableFuture.completedFuture(value.get());
Ray Milkey24e60b32015-08-12 11:39:54 -070070 }
71
72 public static AtomicCounterBuilder builder() {
73 return new Builder();
74 }
75
Madan Jampanie17d3282016-02-03 15:30:57 -080076 public static class Builder extends AtomicCounterBuilder {
Ray Milkey24e60b32015-08-12 11:39:54 -070077 @Override
Madan Jampanie17d3282016-02-03 15:30:57 -080078 public AsyncAtomicCounter build() {
Ray Milkey24e60b32015-08-12 11:39:54 -070079 return new TestAtomicCounter();
80 }
81 }
82}