blob: 531721aa35bcd8ede582945128e26d06392c7ac2 [file] [log] [blame]
Madan Jampanidfde6ba2016-01-13 21:36:09 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.store.service;
17
18import java.util.concurrent.CompletableFuture;
19
20/**
21 * Distributed version of java.util.concurrent.atomic.AtomicReference.
22 * <p>
23 * All methods of this interface return a {@link CompletableFuture future} immediately
24 * after a successful invocation. The operation itself is executed asynchronous and
25 * the returned future will be {@link CompletableFuture#complete completed} when the
26 * operation finishes.
27 *
28 * @param <V> value type
29 */
30public interface AsyncAtomicValue<V> {
31
32 /**
33 * Atomically sets the value to the given updated value if the current value is equal to the expected value.
34 * <p>
35 * IMPORTANT: Equality is based on the equality of the serialized {code byte[]} representations.
36 * <p>
37 * @param expect the expected value
38 * @param update the new value
39 * @return CompletableFuture that will be completed with {@code true} if update was successful. Otherwise future
40 * will be completed with a value of {@code false}
41 */
42 CompletableFuture<Boolean> compareAndSet(V expect, V update);
43
44 /**
45 * Gets the current value.
46 * @return CompletableFuture that will be completed with the value
47 */
48 CompletableFuture<V> get();
49
50 /**
51 * Atomically sets to the given value and returns the old value.
52 * @param value the new value
53 * @return CompletableFuture that will be completed with the previous value
54 */
55 CompletableFuture<V> getAndSet(V value);
56
57 /**
58 * Sets to the given value.
59 * @param value value to set
60 * @return CompletableFuture that will be completed when the operation finishes
61 */
62 CompletableFuture<Void> set(V value);
63
64 /**
65 * Registers the specified listener to be notified whenever the atomic value is updated.
66 * @param listener listener to notify about events
67 * @return CompletableFuture that will be completed when the operation finishes
68 */
69 CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener);
70
71 /**
72 * Unregisters the specified listener such that it will no longer
73 * receive atomic value update notifications.
74 * @param listener listener to unregister
75 * @return CompletableFuture that will be completed when the operation finishes
76 */
77 CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener);
78}