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