blob: 60d8337fef563ccc89b9a2b42fd1e6662995e3eb [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 */
Madan Jampania090a112016-01-18 16:38:17 -080030public interface AsyncAtomicValue<V> extends DistributedPrimitive {
31
32 @Override
33 default DistributedPrimitive.Type type() {
34 return DistributedPrimitive.Type.VALUE;
35 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -080036
37 /**
38 * Atomically sets the value to the given updated value if the current value is equal to the expected value.
39 * <p>
40 * IMPORTANT: Equality is based on the equality of the serialized {code byte[]} representations.
41 * <p>
42 * @param expect the expected value
43 * @param update the new value
44 * @return CompletableFuture that will be completed with {@code true} if update was successful. Otherwise future
45 * will be completed with a value of {@code false}
46 */
47 CompletableFuture<Boolean> compareAndSet(V expect, V update);
48
49 /**
50 * Gets the current value.
51 * @return CompletableFuture that will be completed with the value
52 */
53 CompletableFuture<V> get();
54
55 /**
56 * Atomically sets to the given value and returns the old value.
57 * @param value the new value
58 * @return CompletableFuture that will be completed with the previous value
59 */
60 CompletableFuture<V> getAndSet(V value);
61
62 /**
63 * Sets to the given value.
64 * @param value value to set
65 * @return CompletableFuture that will be completed when the operation finishes
66 */
67 CompletableFuture<Void> set(V value);
68
69 /**
70 * Registers the specified listener to be notified whenever the atomic value is updated.
71 * @param listener listener to notify about events
72 * @return CompletableFuture that will be completed when the operation finishes
73 */
74 CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener);
75
76 /**
77 * Unregisters the specified listener such that it will no longer
78 * receive atomic value update notifications.
79 * @param listener listener to unregister
80 * @return CompletableFuture that will be completed when the operation finishes
81 */
82 CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener);
83}