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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics;
import com.yahoo.metrics.util.ValueType;
import com.yahoo.metrics.util.HasCopy;
import java.util.Collection;
/**
* @author thomasg
*/
public class LongValue
implements ValueMetric.Value<Long>
{
private int count;
private long min, max, last;
private long total;
public LongValue() {
count = 0;
min = Long.MAX_VALUE;
max = Long.MIN_VALUE;
last = 0;
total = 0;
}
@Override
public void add(Long v) {
LongValue val = this;
val.count = count + 1;
val.total = total + v;
val.min = Math.min(min, v);
val.max = Math.max(max, v);
val.last = v;
}
@Override
public void join(ValueMetric.Value<Long> v2, boolean createAverageOnJoin) {
LongValue value = this;
if (createAverageOnJoin) {
value.count = count + v2.getCount();
value.total = total + v2.getTotal();
value.last = v2.getLast();
} else {
double totalAverage = getAverage() + v2.getAverage();
value.count = count + v2.getCount();
value.total = (long) (totalAverage * value.count); // Total is "wrong" I guess.
value.last = last + v2.getLast();
}
value.min = Math.min(min, v2.getMin());
value.max = Math.max(max, v2.getMax());
}
@Override
public void add(ValueType other) {
LongValue dv = (LongValue) other;
count = count + dv.count;
total = total + dv.total;
min = Math.min(min, dv.min);
max = Math.max(max, dv.max);
last = dv.last;
}
@Override
public ValueType join(Collection<ValueType> sources, JoinBehavior joinBehavior) {
LongValue result = new LongValue();
for (ValueType t : sources) {
LongValue dv = (LongValue) t;
result.count = result.count + dv.count;
result.total = result.total + dv.total;
result.min = Math.min(result.min, dv.min);
result.max = Math.max(result.max, dv.max);
result.last += dv.last;
}
if (joinBehavior == JoinBehavior.AVERAGE_ON_JOIN) {
result.last /= sources.size();
} else {
result.total *= sources.size();
}
return result;
}
@Override
public boolean overflow(ValueMetric.Value<Long> v2) {
if (count > (count + v2.getCount())) {
return true;
}
if (v2.getTotal() > 0 && getTotal() > getTotal() + v2.getTotal()) {
return true;
}
if (v2.getTotal() < 0 && getTotal() < getTotal() + v2.getTotal()) {
return true;
}
return false;
}
@Override
public int getCount() { return count; }
@Override
public Long getMin() { return (count > 0) ? min : 0; }
@Override
public Long getMax() { return (count > 0) ? max : 0; }
@Override
public Long getLast() { return last; }
@Override
public Long getTotal() { return total; }
@Override
public Double getAverage() {
if (count == 0) {
return 0.0;
}
return ((double) total) / count;
}
@Override
public String valueToString(Long val) {
if (val == Long.MIN_VALUE || val == Long.MAX_VALUE) {
return valueToString((long)0);
}
return val.toString();
}
@Override
public LongValue clone() {
try{
return (LongValue) super.clone();
} catch (CloneNotSupportedException e) { return null; }
}
@Override
public ValueMetric.Value<Long> copyObject() {
return clone();
}
}
|