1   package com.stateofflow.eclipse.metrics.util;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.Set;
6   
7   public class CounterMap<K> {
8       private final Map<K, Counter> map;
9   
10      public CounterMap() {
11          this(new HashMap<K, Counter>());
12      }
13  
14      public CounterMap(final Map<K, Counter> toWrap) {
15          this.map = toWrap;
16      }
17  
18      public int get(final K key) {
19          return getCounter(key).getValue();
20      }
21  
22      private Counter getCounter(final K key) {
23          return map.get(key);
24      }
25  
26      public void increment(final K key) {
27          put(key, 1);
28      }
29  
30      public Set<K> keySet() {
31          return map.keySet();
32      }
33  
34      public void put(final K key, final int dValue) {
35          if (!map.containsKey(key)) {
36              map.put(key, new Counter(dValue));
37          } else {
38              getCounter(key).add(dValue);
39          }
40      }
41  
42      public int size() {
43          return map.size();
44      }
45  
46      @Override
47      public String toString() {
48          return map.toString();
49      }
50  }
51