Adds delta to the value currently associated with key, and returns the old value.
(K key, long delta)
| 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 |
no test coverage detected