Collector.java
2.24 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
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
package se.lil.om;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
class Collector {
ArrayList<LongDelta> list = new ArrayList<LongDelta>();
public void update(ResultSet rset) throws Throwable {
while(rset.next()) {
// Add try catch for issue #5
Timestamp ts = rset.getTimestamp(1);
String name = rset.getString(2);
Long value;
try {
value = rset.getLong(3);
}
catch (java.sql.SQLException e) {
value = Long.MAX_VALUE;
}
updateValue(ts, name, value);
}
rset.close();
}
public void updateValue(Timestamp ts, String name, Long value) {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(name.equals(list.get(x).name)) {
myDelta = list.get(x);
}
}
if(myDelta != null) {
myDelta.lastFetch = myDelta.curFetch;
myDelta.curFetch = ts;
myDelta.lastValue = myDelta.curValue;
myDelta.curValue = value;
} else {
myDelta = new LongDelta();
myDelta.curFetch = ts;
myDelta.curValue = value;
myDelta.name = name;
list.add(myDelta);
}
}
public long getCurrentValue(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(name.equals(list.get(x).name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getCurrentValue();
else
return 0;
}
public long getDelta(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(name.equals(list.get(x).name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getDelta();
else
return 0;
}
public double getPerSecValue(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(name.equals(list.get(x).name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getPerSecondValue();
else
return 0;
}
public double getSeconds(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(name.equals(list.get(x).name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getSeconds();
else
return 0;
}
}