-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimeSeries.java
122 lines (107 loc) · 3.59 KB
/
TimeSeries.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* © 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;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
import edu.ie3.datamodel.models.value.Value;
import java.time.ZonedDateTime;
import java.util.*;
/**
* Describes a Series of {@link edu.ie3.datamodel.models.value.Value values}
*
* @param <E> Type of the entries, the time series is foreseen to contain
* @param <V> Type of the values, the entries will have
*/
public abstract class TimeSeries<E extends TimeSeriesEntry<V>, V extends Value>
extends UniqueEntity {
private final Set<E> entries;
protected TimeSeries(Set<E> entries) {
super();
this.entries = Collections.unmodifiableSet(entries);
}
protected TimeSeries(UUID uuid, Set<E> entries) {
super(uuid);
this.entries = Collections.unmodifiableSet(entries);
}
/**
* Get the time based value for the queried time
*
* @param time Reference in time
* @return the value at the given time step as a TimeBasedValue
*/
public Optional<TimeBasedValue<V>> getTimeBasedValue(ZonedDateTime time) {
V content = getValue(time).orElse(null);
if (content != null) {
return Optional.of(new TimeBasedValue<>(time, content));
} else {
return Optional.empty();
}
}
/**
* If you prefer to keep the time with the value, please use {@link TimeSeries#getTimeBasedValue}
* instead
*
* @param time Queried time
* @return An option on the raw value at the given time step
*/
public abstract Optional<V> getValue(ZonedDateTime time);
/**
* Get the next earlier known time instant
*
* @param time Reference in time
* @return The next earlier known time instant
*/
protected abstract Optional<ZonedDateTime> getPreviousDateTime(ZonedDateTime time);
/**
* Get the next later known time instant
*
* @param time Reference in time
* @return The next later known time instant
*/
protected abstract Optional<ZonedDateTime> getNextDateTime(ZonedDateTime time);
/**
* Get the most recent available value before or at the given time step as a TimeBasedValue
*
* @param time Reference in time
* @return the most recent available value before or at the given time step as a TimeBasedValue
*/
public Optional<TimeBasedValue<V>> getPreviousTimeBasedValue(ZonedDateTime time) {
return getPreviousDateTime(time).map(this::getTimeBasedValue).map(Optional::get);
}
/**
* Get the next available value after or at the given time step as a TimeBasedValue
*
* @param time Reference in time
* @return the next available value after or at the given time step as a TimeBasedValue
*/
public Optional<TimeBasedValue<V>> getNextTimeBasedValue(ZonedDateTime time) {
return getNextDateTime(time).map(this::getTimeBasedValue).map(Optional::get);
}
/**
* Returns all unique entries
*
* @return all unique entries
*/
public Set<E> getEntries() {
return entries;
}
@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;
TimeSeries<?, ?> that = (TimeSeries<?, ?>) o;
return entries.equals(that.entries);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), entries);
}
@Override
public String toString() {
return "TimeSeries{" + "uuid=" + getUuid() + ", #entries=" + entries.size() + '}';
}
}