blob: 36c571b9552772923c01ff663b130fd8a21a3e40 [file] [log] [blame]
Madan Jampanib5d72d52015-04-03 16:53:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampanib5d72d52015-04-03 16:53:50 -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
18import java.util.concurrent.CompletableFuture;
19
Madan Jampanie17d3282016-02-03 15:30:57 -080020import org.onosproject.store.primitives.DefaultAtomicCounter;
21
Madan Jampanib5d72d52015-04-03 16:53:50 -070022/**
23 * An async atomic counter dispenses monotonically increasing values.
24 */
Madan Jampania090a112016-01-18 16:38:17 -080025public interface AsyncAtomicCounter extends DistributedPrimitive {
26
27 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080028 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080029 return DistributedPrimitive.Type.COUNTER;
30 }
Madan Jampanib5d72d52015-04-03 16:53:50 -070031
32 /**
33 * Atomically increment by one the current value.
34 *
35 * @return updated value
36 */
37 CompletableFuture<Long> incrementAndGet();
38
39 /**
Madan Jampani04aeb452015-05-02 16:12:24 -070040 * Atomically increment by one the current value.
41 *
42 * @return previous value
43 */
44 CompletableFuture<Long> getAndIncrement();
45
46 /**
47 * Atomically adds the given value to the current value.
48 *
49 * @param delta the value to add
50 * @return previous value
51 */
52 CompletableFuture<Long> getAndAdd(long delta);
53
54 /**
55 * Atomically adds the given value to the current value.
56 *
57 * @param delta the value to add
58 * @return updated value
59 */
60 CompletableFuture<Long> addAndGet(long delta);
61
62 /**
Madan Jampanib5d72d52015-04-03 16:53:50 -070063 * Returns the current value of the counter without modifying it.
64 *
65 * @return current value
66 */
67 CompletableFuture<Long> get();
andreafd912ac2015-10-02 14:58:35 -070068
69
70 /**
71 * Atomically sets the given value to the current value.
72 *
Brian O'Connor52515622015-10-09 17:03:44 -070073 * @param value new value
andreafd912ac2015-10-02 14:58:35 -070074 * @return future void
75 */
76 CompletableFuture<Void> set(long value);
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070077
78 /**
79 * Atomically sets the given counter to the updated value if the current value is the expected value, otherwise
80 * no change occurs.
81 * @param expectedValue the expected current value of the counter
82 * @param updateValue the new value to be set
83 * @return true if the update occurred and the expected value was equal to the current value, false otherwise
84 */
85 CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue);
Madan Jampanie17d3282016-02-03 15:30:57 -080086
87 /**
88 * Returns a new {@link AtomicCounter} that is backed by this instance.
89 *
90 * @param timeoutMillis timeout duration for the returned ConsistentMap operations
91 * @return new {@code ConsistentMap} instance
92 */
93 default AtomicCounter asAtomicCounter(long timeoutMillis) {
94 return new DefaultAtomicCounter(this, timeoutMillis);
95 }
96
97 /**
98 * Returns a new {@link AtomicCounter} that is backed by this instance and with a default operation timeout.
99 *
100 * @return new {@code ConsistentMap} instance
101 */
102 default AtomicCounter asAtomicCounter() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700103 return new DefaultAtomicCounter(this, DEFAULT_OPERATION_TIMEOUT_MILLIS);
Madan Jampanie17d3282016-02-03 15:30:57 -0800104 }
Madan Jampanib5d72d52015-04-03 16:53:50 -0700105}