Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [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 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 20 | import org.onosproject.store.primitives.DefaultAtomicValue; |
| 21 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Distributed version of java.util.concurrent.atomic.AtomicReference. |
| 24 | * <p> |
| 25 | * All methods of this interface return a {@link CompletableFuture future} immediately |
| 26 | * after a successful invocation. The operation itself is executed asynchronous and |
| 27 | * the returned future will be {@link CompletableFuture#complete completed} when the |
| 28 | * operation finishes. |
| 29 | * |
| 30 | * @param <V> value type |
| 31 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 32 | public interface AsyncAtomicValue<V> extends DistributedPrimitive { |
| 33 | |
| 34 | @Override |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 35 | default DistributedPrimitive.Type primitiveType() { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 36 | return DistributedPrimitive.Type.VALUE; |
| 37 | } |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Atomically sets the value to the given updated value if the current value is equal to the expected value. |
| 41 | * <p> |
| 42 | * IMPORTANT: Equality is based on the equality of the serialized {code byte[]} representations. |
| 43 | * <p> |
| 44 | * @param expect the expected value |
| 45 | * @param update the new value |
| 46 | * @return CompletableFuture that will be completed with {@code true} if update was successful. Otherwise future |
| 47 | * will be completed with a value of {@code false} |
| 48 | */ |
| 49 | CompletableFuture<Boolean> compareAndSet(V expect, V update); |
| 50 | |
| 51 | /** |
| 52 | * Gets the current value. |
| 53 | * @return CompletableFuture that will be completed with the value |
| 54 | */ |
| 55 | CompletableFuture<V> get(); |
| 56 | |
| 57 | /** |
| 58 | * Atomically sets to the given value and returns the old value. |
| 59 | * @param value the new value |
| 60 | * @return CompletableFuture that will be completed with the previous value |
| 61 | */ |
| 62 | CompletableFuture<V> getAndSet(V value); |
| 63 | |
| 64 | /** |
| 65 | * Sets to the given value. |
| 66 | * @param value value to set |
| 67 | * @return CompletableFuture that will be completed when the operation finishes |
| 68 | */ |
| 69 | CompletableFuture<Void> set(V value); |
| 70 | |
| 71 | /** |
| 72 | * Registers the specified listener to be notified whenever the atomic value is updated. |
| 73 | * @param listener listener to notify about events |
| 74 | * @return CompletableFuture that will be completed when the operation finishes |
| 75 | */ |
| 76 | CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener); |
| 77 | |
| 78 | /** |
| 79 | * Unregisters the specified listener such that it will no longer |
| 80 | * receive atomic value update notifications. |
| 81 | * @param listener listener to unregister |
| 82 | * @return CompletableFuture that will be completed when the operation finishes |
| 83 | */ |
| 84 | CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener); |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Returns a new {@link AtomicValue} that is backed by this instance. |
| 88 | * |
| 89 | * @param timeoutMillis timeout duration for the returned ConsistentMap operations |
| 90 | * @return new {@code AtomicValue} instance |
| 91 | */ |
| 92 | default AtomicValue<V> asAtomicValue(long timeoutMillis) { |
| 93 | return new DefaultAtomicValue<>(this, timeoutMillis); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a new {@link AtomicValue} that is backed by this instance and with a default operation timeout. |
| 98 | * |
| 99 | * @return new {@code AtomicValue} instance |
| 100 | */ |
| 101 | default AtomicValue<V> asAtomicValue() { |
Jordan Halterman | 6440b09 | 2017-05-24 17:48:08 -0700 | [diff] [blame] | 102 | return new DefaultAtomicValue<>(this, DEFAULT_OPERATION_TIMEOUT_MILLIS); |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 103 | } |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 104 | } |