blob: adca8438ebd8c8ec0bb5a61610749171d2a7fec6 [file] [log] [blame]
Madan Jampani762246d2015-07-21 15:40:59 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampani762246d2015-07-21 15:40:59 -07003 *
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 */
16package org.onosproject.store.service;
17
18/**
19 * Distributed version of java.util.concurrent.atomic.AtomicReference.
20 *
21 * @param <V> value type
22 */
Madan Jampania090a112016-01-18 16:38:17 -080023public interface AtomicValue<V> extends DistributedPrimitive {
Madan Jampani762246d2015-07-21 15:40:59 -070024
Jordan Halterman980a8c12017-09-22 18:01:19 -070025 @Override
26 default Type primitiveType() {
27 return Type.VALUE;
28 }
29
Madan Jampani762246d2015-07-21 15:40:59 -070030 /**
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}