/
Statistic.java
151 lines (121 loc) · 4.32 KB
/
Statistic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.statistic;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Statistic {
private StatisticName name;
private String nodeId;
private Date captureStartTimeMs;
private BigDecimal total;
private long count;
private static Map<StatisticName, BigDecimal> lifeTimeTotal = new HashMap<StatisticName, BigDecimal>(
StatisticName.values().length);
private static Map<StatisticName, Long> lifeTimeCount = new HashMap<StatisticName, Long>(StatisticName
.values().length);
static {
StatisticName[] allStatistics = StatisticName.values();
for (StatisticName statisticName : allStatistics) {
lifeTimeCount.put(statisticName, new Long(0));
lifeTimeTotal.put(statisticName, BigDecimal.ZERO);
}
}
public Statistic(StatisticName name, String nodeId) {
this(name, nodeId, new Date());
}
public Statistic(StatisticName name, String nodeId, Date startTime) {
this.name = name;
this.nodeId = nodeId;
this.captureStartTimeMs = startTime;
this.total = BigDecimal.ZERO;
}
public StatisticName getName() {
return name;
}
public String getNodeId() {
return nodeId;
}
public BigDecimal getTotal() {
return total;
}
public BigDecimal getLifetimeTotal() {
return lifeTimeTotal.get(name);
}
public long getLifetimeCount() {
return lifeTimeCount.get(name);
}
public BigDecimal getLifetimeAverageValue() {
return getAverageValue(getLifetimeTotal(), getLifetimeCount());
}
public BigDecimal getAverageValue() {
return getAverageValue(this.total, this.count);
}
private BigDecimal getAverageValue(BigDecimal total, long count) {
if (total != null && count > 0) {
return total.divide(new BigDecimal(count));
} else {
return BigDecimal.ZERO;
}
}
public void increment() {
add(1,1);
}
public void add(int v) {
add(v, 1);
}
public void add(BigDecimal v) {
add(v, 1);
}
public void add(long v) {
add(v, 1);
}
public void add(long v, long count) {
synchronized (name) {
this.total = this.total.add(new BigDecimal(v));
this.count += this.count;
lifeTimeCount.put(name, new Long(lifeTimeCount.get(name) + count));
lifeTimeTotal.put(name, lifeTimeTotal.get(name).add(new BigDecimal(v)));
}
}
public void add(int v, long count) {
synchronized (name) {
this.total = this.total.add(new BigDecimal(v));
this.count += this.count;
lifeTimeCount.put(name, new Long(lifeTimeCount.get(name) + count));
lifeTimeTotal.put(name, lifeTimeTotal.get(name).add(new BigDecimal(v)));
}
}
public void add(BigDecimal v, long count) {
synchronized (name) {
this.total = this.total.add(v);
this.count += this.count;
lifeTimeCount.put(name, new Long(lifeTimeCount.get(name) + count));
lifeTimeTotal.put(name, lifeTimeTotal.get(name).add(v));
}
}
public Date getCaptureStartTimeMs() {
return captureStartTimeMs;
}
public long getCount() {
return count;
}
}