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