-
Notifications
You must be signed in to change notification settings - Fork 663
/
Copy pathTimeSeriesData.java
executable file
·108 lines (90 loc) · 2.2 KB
/
TimeSeriesData.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
package org.dataalgorithms.chap06;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.hadoop.io.Writable;
import org.dataalgorithms.util.DateUtil;
/**
*
* TimeSeriesData represents a pair of
* (time-series-timestamp, time-series-value).
*
* @author Mahmoud Parsian
*
*/
public class TimeSeriesData
implements Writable, Comparable<TimeSeriesData> {
private long timestamp;
private double value;
public static TimeSeriesData copy(TimeSeriesData tsd) {
return new TimeSeriesData(tsd.timestamp, tsd.value);
}
public TimeSeriesData(long timestamp, double value) {
set(timestamp, value);
}
public TimeSeriesData() {
}
public void set(long timestamp, double value) {
this.timestamp = timestamp;
this.value = value;
}
public long getTimestamp() {
return this.timestamp;
}
public double getValue() {
return this.value;
}
/**
* Deserializes the point from the underlying data.
* @param in a DataInput object to read the point from.
*/
public void readFields(DataInput in) throws IOException {
this.timestamp = in.readLong();
this.value = in.readDouble();
}
/**
* Convert a binary data into TimeSeriesData
*
* @param in A DataInput object to read from.
* @return A TimeSeriesData object
* @throws IOException
*/
public static TimeSeriesData read(DataInput in) throws IOException {
TimeSeriesData tsData = new TimeSeriesData();
tsData.readFields(in);
return tsData;
}
public String getDate() {
return DateUtil.getDateAsString(this.timestamp);
}
/**
* Creates a clone of this object
*/
public TimeSeriesData clone() {
return new TimeSeriesData(timestamp, value);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(this.timestamp );
out.writeDouble(this.value );
}
/**
* Used in sorting the data in the reducer
*/
@Override
public int compareTo(TimeSeriesData data) {
if (this.timestamp < data.timestamp ) {
return -1;
}
else if (this.timestamp > data.timestamp ) {
return 1;
}
else {
return 0;
}
}
public String toString() {
return "("+timestamp+","+value+")";
}
}