Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 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 | */ |
Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 16 | package org.onosproject.store.primitives; |
| 17 | |
| 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 19 | import org.onosproject.store.service.AtomicCounterBuilder; |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 20 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 21 | import java.util.concurrent.CompletableFuture; |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 22 | import java.util.concurrent.atomic.AtomicLong; |
| 23 | |
| 24 | /** |
| 25 | * Test implementation of atomic counter. |
| 26 | */ |
Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 27 | public class AsyncAtomicCounterAdapter implements AsyncAtomicCounter { |
| 28 | private final AtomicLong value; |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 29 | |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 30 | @Override |
| 31 | public String name() { |
| 32 | return null; |
| 33 | } |
| 34 | |
Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 35 | AsyncAtomicCounterAdapter() { |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 36 | value = new AtomicLong(); |
| 37 | } |
| 38 | |
| 39 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 40 | public CompletableFuture<Long> incrementAndGet() { |
| 41 | return CompletableFuture.completedFuture(value.incrementAndGet()); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 45 | public CompletableFuture<Long> getAndIncrement() { |
| 46 | return CompletableFuture.completedFuture(value.getAndIncrement()); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 50 | public CompletableFuture<Long> getAndAdd(long delta) { |
| 51 | return CompletableFuture.completedFuture(value.getAndAdd(delta)); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 55 | public CompletableFuture<Long> addAndGet(long delta) { |
| 56 | return CompletableFuture.completedFuture(value.addAndGet(delta)); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 60 | public CompletableFuture<Void> set(long value) { |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 61 | this.value.set(value); |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 62 | return CompletableFuture.completedFuture(null); |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 66 | public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) { |
| 67 | return CompletableFuture.completedFuture(value.compareAndSet(expectedValue, updateValue)); |
Aaron Kruglikov | 82fd632 | 2015-10-06 12:02:46 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 71 | public CompletableFuture<Long> get() { |
| 72 | return CompletableFuture.completedFuture(value.get()); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public static AtomicCounterBuilder builder() { |
| 76 | return new Builder(); |
| 77 | } |
| 78 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 79 | public static class Builder extends AtomicCounterBuilder { |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 80 | @Override |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 81 | public AsyncAtomicCounter build() { |
Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 82 | return new AsyncAtomicCounterAdapter(); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |