MCPcopy Create free account
hub / github.com/antlr/codebuff / getAndAdd

Method getAndAdd

output/java_guava/1.4.17/AtomicLongMap.java:169–199  ·  view source on GitHub ↗

Adds delta to the value currently associated with key, and returns the old value.

(K key, long delta)

Source from the content-addressed store, hash-verified

167 */
168
169 @CanIgnoreReturnValue
170 public long getAndAdd(K key, long delta) {
171 outer:
172 while (true) {
173 AtomicLong atomic = map.get(key);
174 if (atomic == null) {
175 atomic = map.putIfAbsent(key, new AtomicLong(delta));
176 if (atomic == null) {
177 return 0L;
178 }
179 // atomic is now non-null; fall through
180 }
181
182 while (true) {
183 long oldValue = atomic.get();
184 if (oldValue == 0L) {
185 // don't compareAndSet a zero
186 if (map.replace(key, atomic, new AtomicLong(delta))) {
187 return 0L;
188 }
189 // atomic replaced
190 continue outer;
191 }
192 long newValue = oldValue + delta;
193 if (atomic.compareAndSet(oldValue, newValue)) {
194 return oldValue;
195 }
196 // value changed
197 }
198 }
199 }
200
201 /**
202 * Associates {@code newValue} with {@code key} in this map, and returns the value previously

Callers 2

getAndIncrementMethod · 0.95
getAndDecrementMethod · 0.95

Calls 4

getMethod · 0.65
putIfAbsentMethod · 0.45
replaceMethod · 0.45
compareAndSetMethod · 0.45

Tested by

no test coverage detected