Anton Romanov 518e01ca93 refactor
2021-06-01 14:05:19 +04:00

106 lines
2.6 KiB
Java

/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*
*/
package ru.ulstu.datamodel.ts;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
public class TimeSeries {
private List<TimeSeriesValue> values = new ArrayList<>();
private String name;
public TimeSeries(String name) {
this.name = name;
}
public TimeSeries(String prefix, String suffix) {
this.name = String.format("%s %s", prefix, suffix);
}
@JsonCreator
public TimeSeries(@JsonProperty(value = "values") List<TimeSeriesValue> values, @JsonProperty(value = "name") String name) {
this.values = values;
this.name = name;
}
public TimeSeries() {
}
public TimeSeries(List<TimeSeriesValue> values) {
this.values = values;
}
public List<TimeSeriesValue> getValues() {
return values;
}
public void setValues(List<TimeSeriesValue> values) {
this.values = values;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEmpty() {
return values.isEmpty();
}
public void addValue(TimeSeriesValue timeSeriesValue) {
values.add(timeSeriesValue);
}
public void addValue(TimeSeriesValue basedOnValue, Double value) {
values.add(new TimeSeriesValue(basedOnValue.getDate(), value));
}
public TimeSeriesValue getLastValue() {
return values.get(values.size() - 1);
}
public int getLength() {
return values.size();
}
public TimeSeriesValue getFirstValue() {
if ((values.size() > 0)) {
return values.get(0);
}
throw new RuntimeException("Временной ряд пуст");
}
public Double getNumericValue(int t) {
if ((values.size() > t) && (t >= 0)) {
return values.get(t).getValue();
}
throw new RuntimeException("Индекс выходит за границы временного ряда");
}
public TimeSeriesValue getValue(int t) {
if ((values.size() > t) && (t >= 0)) {
return values.get(t);
}
throw new RuntimeException("Индекс выходит за границы временного ряда");
}
@Override
public String toString() {
return "TimeSeries{" +
"values=" + values +
", name='" + name + '\'' +
'}';
}
}