Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -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 | |
| 18 | /** |
| 19 | * Distributed version of java.util.concurrent.atomic.AtomicReference. |
| 20 | * |
| 21 | * @param <V> value type |
| 22 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 23 | public interface AtomicValue<V> extends DistributedPrimitive { |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 24 | |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 25 | @Override |
| 26 | default Type primitiveType() { |
| 27 | return Type.VALUE; |
| 28 | } |
| 29 | |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Atomically sets the value to the given updated value if the current value is equal to the expected value. |
| 32 | * <p> |
| 33 | * IMPORTANT: Equality is based on the equality of the serialized byte[] representations. |
| 34 | * <p> |
| 35 | * @param expect the expected value |
| 36 | * @param update the new value |
| 37 | * @return true if successful. false return indicates that the actual value was not equal to the expected value. |
| 38 | */ |
| 39 | boolean compareAndSet(V expect, V update); |
| 40 | |
| 41 | /** |
| 42 | * Gets the current value. |
| 43 | * @return current value |
| 44 | */ |
| 45 | V get(); |
| 46 | |
| 47 | /** |
| 48 | * Atomically sets to the given value and returns the old value. |
| 49 | * @param value the new value |
| 50 | * @return previous value |
| 51 | */ |
| 52 | V getAndSet(V value); |
| 53 | |
| 54 | /** |
| 55 | * Sets to the given value. |
| 56 | * @param value new value |
| 57 | */ |
| 58 | void set(V value); |
| 59 | |
| 60 | /** |
| 61 | * Registers the specified listener to be notified whenever the atomic value is updated. |
| 62 | * |
| 63 | * @param listener listener to notify about events |
| 64 | */ |
| 65 | void addListener(AtomicValueEventListener<V> listener); |
| 66 | |
| 67 | /** |
| 68 | * Unregisters the specified listener such that it will no longer |
| 69 | * receive atomic value update notifications. |
| 70 | * |
| 71 | * @param listener listener to unregister |
| 72 | */ |
| 73 | void removeListener(AtomicValueEventListener<V> listener); |
| 74 | } |