/
AsyncAtomicCounterMap.java
185 lines (163 loc) · 6.11 KB
/
AsyncAtomicCounterMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2015-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.primitives.map;
import io.atomix.primitives.DistributedPrimitive;
import io.atomix.primitives.map.impl.DefaultAtomicCounterMap;
import java.util.concurrent.CompletableFuture;
/**
* An async atomic counter map dispenses monotonically increasing values associated with key.
*/
public interface AsyncAtomicCounterMap<K> extends DistributedPrimitive {
@Override
default DistributedPrimitive.Type primitiveType() {
return DistributedPrimitive.Type.COUNTER_MAP;
}
/**
* Increments by one the value currently associated with key, and returns the new value.
*
* @param key key with which the specified value is to be associated
* @return incremented value
*/
CompletableFuture<Long> incrementAndGet(K key);
/**
* Decrements by one the value currently associated with key, and returns the new value.
*
* @param key key with which the specified value is to be associated
* @return updated value
*/
CompletableFuture<Long> decrementAndGet(K key);
/**
* Increments by one the value currently associated with key, and returns the old value.
*
* @param key key with which the specified value is to be associated
* @return previous value
*/
CompletableFuture<Long> getAndIncrement(K key);
/**
* Decrements by one the value currently associated with key, and returns the old value.
*
* @param key key with which the specified value is to be associated
* @return previous value
*/
CompletableFuture<Long> getAndDecrement(K key);
/**
* Adds delta to the value currently associated with key, and returns the new value.
*
* @param key key with which the specified value is to be associated
* @param delta the value to add
* @return updated value
*/
CompletableFuture<Long> addAndGet(K key, long delta);
/**
* Adds delta to the value currently associated with key, and returns the old value.
*
* @param key key with which the specified value is to be associated
* @param delta the value to add
* @return previous value
*/
CompletableFuture<Long> getAndAdd(K key, long delta);
/**
* Returns the value associated with key, or zero if there is no value associated with key.
*
* @param key key with which the specified value is to be associated
* @return current value
*/
CompletableFuture<Long> get(K key);
/**
* Associates ewValue with key in this map, and returns the value previously
* associated with key, or zero if there was no such value.
*
* @param key key with which the specified value is to be associated
* @param newValue the value to put
* @return previous value or zero
*/
CompletableFuture<Long> put(K key, long newValue);
/**
* If key is not already associated with a value or if key is associated with
* zero, associate it with newValue. Returns the previous value associated with
* key, or zero if there was no mapping for key.
*
* @param key key with which the specified value is to be associated
* @param newValue the value to put
* @return previous value or zero
*/
CompletableFuture<Long> putIfAbsent(K key, long newValue);
/**
* If (key, expectedOldValue) is currently in the map, this method replaces
* expectedOldValue with newValue and returns true; otherwise, this method return false.
* <p>
* If expectedOldValue is zero, this method will succeed if (key, zero)
* is currently in the map, or if key is not in the map at all.
*
* @param key key with which the specified value is to be associated
* @param expectedOldValue the expected value
* @param newValue the value to replace
* @return true if the value was replaced, false otherwise
*/
CompletableFuture<Boolean> replace(K key, long expectedOldValue, long newValue);
/**
* Removes and returns the value associated with key. If key is not
* in the map, this method has no effect and returns zero.
*
* @param key key with which the specified value is to be associated
* @return the previous value associated with the specified key or null
*/
CompletableFuture<Long> remove(K key);
/**
* If (key, value) is currently in the map, this method removes it and returns
* true; otherwise, this method returns false.
*
* @param key key with which the specified value is to be associated
* @param value the value to remove
* @return true if the value was removed, false otherwise
*/
CompletableFuture<Boolean> remove(K key, long value);
/**
* Returns the number of entries in the map.
*
* @return the number of entries in the map
*/
CompletableFuture<Integer> size();
/**
* Returns a boolean indicating whether the map is empty.
*
* @return true if the map is empty, false otherwise
*/
CompletableFuture<Boolean> isEmpty();
/**
* Removes all entries from the map.
*
* @return void
*/
CompletableFuture<Void> clear();
/**
* Returns a new {@link AtomicCounterMap} that is backed by this instance.
*
* @return new {@code AtomicCounterMap} instance
*/
default AtomicCounterMap<K> asAtomicCounterMap() {
return asAtomicCounterMap(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
}
/**
* Returns a new {@link AtomicCounterMap} that is backed by this instance.
*
* @param timeoutMillis timeout duration for the returned ConsistentMap operations
* @return new {@code AtomicCounterMap} instance
*/
default AtomicCounterMap<K> asAtomicCounterMap(long timeoutMillis) {
return new DefaultAtomicCounterMap<>(this, timeoutMillis);
}
}