blob: ab0deea3382d1d32f698b798e0789a4b8ad39bf1 [file] [log] [blame]
Madan Jampanidfde6ba2016-01-13 21:36:09 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampanidfde6ba2016-01-13 21:36:09 -08003 *
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
18import java.util.concurrent.CompletableFuture;
19
Madan Jampanie17d3282016-02-03 15:30:57 -080020import org.onosproject.store.primitives.DefaultAtomicValue;
21
Madan Jampanidfde6ba2016-01-13 21:36:09 -080022/**
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 Jampania090a112016-01-18 16:38:17 -080032public interface AsyncAtomicValue<V> extends DistributedPrimitive {
33
34 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080035 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080036 return DistributedPrimitive.Type.VALUE;
37 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -080038
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 Jampanie17d3282016-02-03 15:30:57 -080085
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 Halterman6440b092017-05-24 17:48:08 -0700102 return new DefaultAtomicValue<>(this, DEFAULT_OPERATION_TIMEOUT_MILLIS);
Madan Jampanie17d3282016-02-03 15:30:57 -0800103 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800104}