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.3 by dl, Mon Aug 29 17:06:20 2011 UTC vs.
Revision 1.9 by dl, Mon Aug 13 15:52:33 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines