1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.jumpmind.symmetric.statistic;
21
22 import java.math.BigDecimal;
23 import java.math.RoundingMode;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 public class Statistic {
29
30 private StatisticName name;
31
32 private String nodeId;
33
34 private Date captureStartTimeMs;
35
36 private BigDecimal total;
37
38 private long count;
39
40 private static Map<StatisticName, BigDecimal> lifeTimeTotal = new HashMap<StatisticName, BigDecimal>(StatisticName
41 .values().length);
42
43 private static Map<StatisticName, Long> lifeTimeCount = new HashMap<StatisticName, Long>(
44 StatisticName.values().length);
45
46 static {
47 StatisticName[] allStatistics = StatisticName.values();
48 for (StatisticName statisticName : allStatistics) {
49 lifeTimeCount.put(statisticName, new Long(0));
50 lifeTimeTotal.put(statisticName, BigDecimal.ZERO);
51 }
52 }
53
54 public Statistic(StatisticName name, String nodeId) {
55 this(name, nodeId, new Date());
56 }
57
58 public Statistic(StatisticName name, String nodeId, Date startTime) {
59 this.name = name;
60 this.nodeId = nodeId;
61 this.captureStartTimeMs = startTime;
62 this.total = BigDecimal.ZERO;
63 }
64
65 public StatisticName getName() {
66 return name;
67 }
68
69 public String getNodeId() {
70 return nodeId;
71 }
72
73 public BigDecimal getTotal() {
74 return total;
75 }
76
77 public BigDecimal getLifetimeTotal() {
78 return lifeTimeTotal.get(name);
79 }
80
81 public long getLifetimeCount() {
82 return lifeTimeCount.get(name);
83 }
84
85 public BigDecimal getLifetimeAverageValue() {
86 return getAverageValue(getLifetimeTotal(), getLifetimeCount());
87 }
88
89 public BigDecimal getAverageValue() {
90 return getAverageValue(this.total, this.count);
91 }
92
93 private BigDecimal getAverageValue(BigDecimal total, long count) {
94 if (total != null && total.compareTo(BigDecimal.ZERO) > 0 && count > 0) {
95 return total.divide(new BigDecimal(count), RoundingMode.HALF_UP);
96 } else {
97 return BigDecimal.ZERO;
98 }
99 }
100
101 public void increment() {
102 add(1, 1);
103 }
104
105 public void add(int v) {
106 add(v, 1);
107 }
108
109 public void add(BigDecimal v) {
110 add(v, 1);
111 }
112
113 public void add(long v) {
114 add(v, 1);
115 }
116
117 public void add(long v, long count) {
118 synchronized (name) {
119 this.total = this.total.add(new BigDecimal(v));
120 this.count += count;
121 lifeTimeCount.put(name, new Long(lifeTimeCount.get(name) + count));
122 lifeTimeTotal.put(name, lifeTimeTotal.get(name).add(new BigDecimal(v)));
123 }
124 }
125
126 public void add(int v, long count) {
127 synchronized (name) {
128 this.total = this.total.add(new BigDecimal(v));
129 this.count += count;
130 lifeTimeCount.put(name, new Long(lifeTimeCount.get(name) + count));
131 lifeTimeTotal.put(name, lifeTimeTotal.get(name).add(new BigDecimal(v)));
132 }
133 }
134
135 public void add(BigDecimal v, long count) {
136 synchronized (name) {
137 this.total = this.total.add(v);
138 this.count += count;
139 lifeTimeCount.put(name, new Long(lifeTimeCount.get(name) + count));
140 lifeTimeTotal.put(name, lifeTimeTotal.get(name).add(v));
141 }
142 }
143
144 public Date getCaptureStartTimeMs() {
145 return captureStartTimeMs;
146 }
147
148 public long getCount() {
149 return count;
150 }
151
152 }