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