-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeSeries.java
More file actions
140 lines (122 loc) · 4.82 KB
/
Copy pathTimeSeries.java
File metadata and controls
140 lines (122 loc) · 4.82 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
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
package ngordnet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeMap;
public class TimeSeries<T extends Number> extends TreeMap<Integer, T> {
/** Constructs a new empty TimeSeries. */
public TimeSeries() {
// Got idea from Jeffrey Zhang to use super(), since that calls the TreeMap constructor
super();
}
/**
* Returns the years in which this time series is valid. Doesn't really need
* to be a NavigableSet.
*/
private Set<Integer> validYears(int startYear, int endYear) {
// make a set of all the years
HashSet<Integer> validYears = new HashSet<Integer>();
// put each year into my time series if it is valid
for (Integer currentKey : this.keySet()) {
if (startYear <= currentKey && currentKey <= endYear) {
validYears.add(currentKey);
}
}
return validYears;
}
/** Creates a copy of TS, but only between STARTYEAR and ENDYEAR. */
public TimeSeries(TimeSeries<T> ts, int startYear, int endYear) {
this.clear();
int temp = startYear;
while (temp <= endYear) {
if (ts.containsKey(temp)) {
this.put(temp, ts.get(temp));
}
temp++;
}
}
/** Creates a copy of TS. */
public TimeSeries(TimeSeries<T> ts) {
this.clear();
// Enhanced for loop over all the keys and put in each key and value
for (Integer currentKey : ts.keySet()) {
this.put(currentKey, ts.get(currentKey));
}
}
/**
* Returns the quotient of this time series divided by the relevant value in
* ts. If ts is missing a key in this time series, return an
* IllegalArgumentException.
*/
public TimeSeries<Double> dividedBy(TimeSeries<? extends Number> ts) {
TimeSeries<Double> newSeries = new TimeSeries<Double>();
// Determines whether the sizes of the two time series are the same, if
// they are not I raise an exception
for (Integer keyToLoopOver : this.keySet()) {
if (ts.containsKey(keyToLoopOver)) {
double quotient = (double) this.get(keyToLoopOver)
.doubleValue()
/ (double) ts.get(keyToLoopOver).doubleValue();
newSeries.put(keyToLoopOver, quotient);
} else {
throw new IllegalArgumentException();
}
}
return newSeries;
}
/**
* Returns the sum of this time series with the given ts. The result is a a
* Double time series (for simplicity).
*/
public TimeSeries<Double> plus(TimeSeries<? extends Number> ts) {
TimeSeries<Double> newSeries = new TimeSeries<Double>();
// Loop over my local series and check to see if my given series has the
// same keys
// If so I add the results otherwise I just use the value of the given
// series
for (Integer keyToLoopOver : this.keySet()) {
if (ts.containsKey(keyToLoopOver)) {
// Got idea from StackOverFlow to do doubleValue(), applies for all useages of it
double sum = this.get(keyToLoopOver).doubleValue()
+ ts.get(keyToLoopOver).doubleValue();
newSeries.put(keyToLoopOver, sum);
} else {
newSeries.put(keyToLoopOver, this.get(keyToLoopOver)
.doubleValue());
}
}
for (Integer keyToLoopOver : ts.keySet()) {
// Do the same thing as above but with my passed in series, but
// first I check to see if
// My returned series already has the given key, in that case I
// ignore it
if (!newSeries.containsKey(keyToLoopOver)) {
newSeries.put(keyToLoopOver, ts.get(keyToLoopOver)
.doubleValue());
}
}
return newSeries;
}
/** Returns all years for this time series (in any order). */
public Collection<Number> years() {
// Make a hashset of the years
LinkedHashSet<Number> allYears = new LinkedHashSet<Number>();
for (Integer currentYear : this.keySet()) {
allYears.add(currentYear);
}
return allYears;
}
/** Returns all data for this time series (in any order). */
public Collection<Number> data() {
// Got idea from Riley Murray via Piazza to use an ArrayList
// Rather than a Set since the same data could appear
// Multiple times
ArrayList<Number> allData = new ArrayList<Number>();
for (Integer currentYear : this.keySet()) {
allData.add(this.get(currentYear));
}
return allData;
}
}