--- jsr166/src/jsr166e/LongAdderTable.java 2011/08/30 07:16:56 1.4
+++ jsr166/src/jsr166e/LongAdderTable.java 2011/10/09 20:12:04 1.8
@@ -12,14 +12,15 @@ import java.io.Serializable;
/**
* A keyed table of adders, that may be useful in computing frequency
- * counts and histograms, or may be used a form of multiset. A {@link
- * LongAdder} is associated with each key. Keys are added to the table
- * implicitly upon any attempt to update, or may be added explicitly
- * using method {@link #install}.
+ * counts and histograms, or may be used as a form of multiset. A
+ * {@link LongAdder} is associated with each key. Keys are added to
+ * the table implicitly upon any attempt to update, or may be added
+ * explicitly using method {@link #install}.
*
*
jsr166e note: This class is targeted to be placed in
* java.util.concurrent.atomic
*
+ * @since 1.8
* @author Doug Lea
*/
public class LongAdderTable implements Serializable {
@@ -52,13 +53,7 @@ public class LongAdderTable implement
* @return the adder associated with the key
*/
public LongAdder install(K key) {
- LongAdder a = map.get(key);
- if (a == null) {
- LongAdder r = new LongAdder();
- if ((a = map.putIfAbsent(key, r)) == null)
- a = r;
- }
- return a;
+ return map.computeIfAbsent(key, createAdder);
}
/**
@@ -70,10 +65,7 @@ public class LongAdderTable implement
* @param x the value to add
*/
public void add(K key, long x) {
- LongAdder a = map.get(key);
- if (a == null)
- a = map.computeIfAbsent(key, createAdder);
- a.add(x);
+ map.computeIfAbsent(key, createAdder).add(x);
}
/**
@@ -135,7 +127,7 @@ public class LongAdderTable implement
/**
* Returns the sum totalled across all keys.
*
- * @return the sum totalled across all keys.
+ * @return the sum totalled across all keys
*/
public long sumAll() {
long sum = 0L;
@@ -155,7 +147,7 @@ public class LongAdderTable implement
/**
* Totals, then resets, the sums associated with all keys.
*
- * @return the sum totalled across all keys.
+ * @return the sum totalled across all keys
*/
public long sumThenResetAll() {
long sum = 0L;