5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166e; |
8 |
+ |
|
9 |
|
import jsr166e.LongAdder; |
10 |
|
import java.util.Map; |
11 |
|
import java.util.Set; |
13 |
|
|
14 |
|
/** |
15 |
|
* A keyed table of adders, that may be useful in computing frequency |
16 |
< |
* counts and histograms, or may be used a form of multiset. A {@link |
17 |
< |
* LongAdder} is associated with each key. Keys are added to the table |
18 |
< |
* implicitly upon any attempt to update, or may be added explicitly |
19 |
< |
* using method {@link #install}. |
16 |
> |
* counts and histograms, or may be used as a form of multiset. A |
17 |
> |
* {@link LongAdder} is associated with each key. Keys are added to |
18 |
> |
* the table implicitly upon any attempt to update, or may be added |
19 |
> |
* explicitly using method {@link #install}. |
20 |
|
* |
21 |
|
* <p><em>jsr166e note: This class is targeted to be placed in |
22 |
< |
* java.util.concurrent.atomic<em> |
22 |
> |
* java.util.concurrent.atomic.</em> |
23 |
|
* |
24 |
+ |
* @since 1.8 |
25 |
|
* @author Doug Lea |
26 |
|
*/ |
27 |
|
public class LongAdderTable<K> implements Serializable { |
32 |
|
private final ConcurrentHashMapV8<K, LongAdder> map; |
33 |
|
|
34 |
|
static final class CreateAdder |
35 |
< |
implements ConcurrentHashMapV8.MappingFunction<Object, LongAdder> { |
36 |
< |
public LongAdder map(Object unused) { return new LongAdder(); } |
35 |
> |
implements ConcurrentHashMapV8.Fun<Object, LongAdder> { |
36 |
> |
public LongAdder apply(Object unused) { return new LongAdder(); } |
37 |
|
} |
38 |
|
|
39 |
|
private static final CreateAdder createAdder = new CreateAdder(); |
54 |
|
* @return the adder associated with the key |
55 |
|
*/ |
56 |
|
public LongAdder install(K key) { |
57 |
< |
LongAdder a = map.get(key); |
56 |
< |
if (a == null) { |
57 |
< |
LongAdder r = new LongAdder(); |
58 |
< |
if ((a = map.putIfAbsent(key, r)) == null) |
59 |
< |
a = r; |
60 |
< |
} |
61 |
< |
return a; |
57 |
> |
return map.computeIfAbsent(key, createAdder); |
58 |
|
} |
59 |
|
|
60 |
|
/** |
66 |
|
* @param x the value to add |
67 |
|
*/ |
68 |
|
public void add(K key, long x) { |
69 |
< |
LongAdder a = map.get(key); |
74 |
< |
if (a == null) |
75 |
< |
a = map.computeIfAbsent(key, createAdder); |
76 |
< |
a.add(x); |
69 |
> |
map.computeIfAbsent(key, createAdder).add(x); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
128 |
|
/** |
129 |
|
* Returns the sum totalled across all keys. |
130 |
|
* |
131 |
< |
* @return the sum totalled across all keys. |
131 |
> |
* @return the sum totalled across all keys |
132 |
|
*/ |
133 |
|
public long sumAll() { |
134 |
|
long sum = 0L; |
148 |
|
/** |
149 |
|
* Totals, then resets, the sums associated with all keys. |
150 |
|
* |
151 |
< |
* @return the sum totalled across all keys. |
151 |
> |
* @return the sum totalled across all keys |
152 |
|
*/ |
153 |
|
public long sumThenResetAll() { |
154 |
|
long sum = 0L; |