DoubleDelta.java
1.4 KB
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
package se.lil.jm;
public class DoubleDelta {
public String name = null;
public Double curValue = null;
public Double lastValue = null;
public Long lastFetch = null;
public Long curFetch = null;
public DoubleDelta() {
}
public DoubleDelta(Long timestamp, Double value) {
this.curFetch = timestamp;
this.curValue = value;
}
public void update(Long timestamp, Double value) {
this.lastFetch = this.curFetch;
this.curFetch = timestamp;
this.lastValue = this.curValue;
this.curValue = value;
}
public double getPerSecondValue() {
if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
// We have values, calculate the number of gets per second
double numMilliSeconds = this.curFetch - this.lastFetch;
double delta = this.curValue - this.lastValue;
double perMilli = delta/numMilliSeconds;
return perMilli * 1000;
} else {
return 0;
}
}
public double getCurrentValue() {
if(this.curValue != null) return this.curValue;
else return 0;
}
public double getSeconds() {
if(this.lastFetch != null && this.curFetch != null) {
double numSeconds = this.curFetch - this.lastFetch;
return numSeconds/1000;
}
return 0;
}
public long getMilliSeconds() {
if(this.lastFetch != null && this.curFetch != null) {
return this.curFetch - this.lastFetch;
}
return 0;
}
}