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

Method addAndGet

output/java_guava/1.4.17/AtomicLongMap.java:114–144  ·  view source on GitHub ↗

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

(K key, long delta)

Source from the content-addressed store, hash-verified

112 */
113
114 @CanIgnoreReturnValue
115 public long addAndGet(K key, long delta) {
116 outer:
117 while (true) {
118 AtomicLong atomic = map.get(key);
119 if (atomic == null) {
120 atomic = map.putIfAbsent(key, new AtomicLong(delta));
121 if (atomic == null) {
122 return delta;
123 }
124 // atomic is now non-null; fall through
125 }
126
127 while (true) {
128 long oldValue = atomic.get();
129 if (oldValue == 0L) {
130 // don't compareAndSet a zero
131 if (map.replace(key, atomic, new AtomicLong(delta))) {
132 return delta;
133 }
134 // atomic replaced
135 continue outer;
136 }
137 long newValue = oldValue + delta;
138 if (atomic.compareAndSet(oldValue, newValue)) {
139 return newValue;
140 }
141 // value changed
142 }
143 }
144 }
145
146 /**
147 * Increments by one the value currently associated with {@code key}, and returns the old value.

Callers 2

incrementAndGetMethod · 0.95
decrementAndGetMethod · 0.95

Calls 4

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

Tested by

no test coverage detected