-
Notifications
You must be signed in to change notification settings - Fork 5
/
IndividualTimeSeries.java
87 lines (73 loc) · 2.78 KB
/
IndividualTimeSeries.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
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.models.timeseries.individual;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
import edu.ie3.datamodel.models.value.Value;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/** Describes a TimeSeries with individual values per time step */
public class IndividualTimeSeries<V extends Value> extends TimeSeries<TimeBasedValue<V>, V> {
/** Maps a time to its respective value to retrieve faster */
private final Map<ZonedDateTime, TimeBasedValue<V>> timeToValue;
public IndividualTimeSeries(Set<TimeBasedValue<V>> values) {
super(values);
timeToValue =
values.stream().collect(Collectors.toMap(TimeBasedValue::getTime, Function.identity()));
}
public IndividualTimeSeries(UUID uuid, Set<TimeBasedValue<V>> values) {
super(uuid, values);
timeToValue =
values.stream().collect(Collectors.toMap(TimeBasedValue::getTime, Function.identity()));
}
/**
* Returns the sorted set of all entries known to this time series
*
* @return An unmodifiable sorted set of all known time based values of this time series
*/
@Override
public SortedSet<TimeBasedValue<V>> getEntries() {
TreeSet<TimeBasedValue<V>> sortedEntries = new TreeSet<>(timeToValue.values());
return Collections.unmodifiableSortedSet(sortedEntries);
}
@Override
public Optional<TimeBasedValue<V>> getTimeBasedValue(ZonedDateTime time) {
return Optional.ofNullable(timeToValue.get(time));
}
@Override
public Optional<V> getValue(ZonedDateTime time) {
return getTimeBasedValue(time).map(TimeBasedValue::getValue);
}
@Override
protected Optional<ZonedDateTime> getPreviousDateTime(ZonedDateTime time) {
return timeToValue.keySet().stream()
.filter(valueTime -> valueTime.compareTo(time) <= 0)
.max(Comparator.naturalOrder());
}
@Override
protected Optional<ZonedDateTime> getNextDateTime(ZonedDateTime time) {
return timeToValue.keySet().stream()
.filter(valueTime -> valueTime.compareTo(time) >= 0)
.min(Comparator.naturalOrder());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
IndividualTimeSeries<?> that = (IndividualTimeSeries<?>) o;
return timeToValue.equals(that.timeToValue);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), timeToValue);
}
@Override
public String toString() {
return "IndividualTimeSeries{" + ", #entries=" + timeToValue.size() + '}';
}
}