ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/LongAdderTable.java
(Generate patch)

Comparing jsr166/src/jsr166e/LongAdderTable.java (file contents):
Revision 1.4 by jsr166, Tue Aug 30 07:16:56 2011 UTC vs.
Revision 1.11 by jsr166, Sun Jan 18 20:17:33 2015 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166e;
8 +
9   import jsr166e.LongAdder;
10   import java.util.Map;
11   import java.util.Set;
# Line 12 | Line 13 | import java.io.Serializable;
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 {
# Line 30 | Line 32 | public class LongAdderTable<K> implement
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();
# Line 52 | Line 54 | public class LongAdderTable<K> implement
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      /**
# Line 70 | Line 66 | public class LongAdderTable<K> implement
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      /**
# Line 135 | Line 128 | public class LongAdderTable<K> implement
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;
# Line 155 | Line 148 | public class LongAdderTable<K> implement
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines