68 lines
1.4 KiB
Java
68 lines
1.4 KiB
Java
package de.ph87.data.series.varying;
|
|
|
|
import de.ph87.data.series.*;
|
|
import de.ph87.data.value.Value;
|
|
import jakarta.persistence.*;
|
|
import lombok.*;
|
|
|
|
import java.time.*;
|
|
|
|
@Getter
|
|
@ToString
|
|
@MappedSuperclass
|
|
@NoArgsConstructor
|
|
public abstract class Varying {
|
|
|
|
@EmbeddedId
|
|
private Id id;
|
|
|
|
@Column(nullable = false)
|
|
private double min;
|
|
|
|
@Column(nullable = false)
|
|
private double max;
|
|
|
|
@Column(nullable = false)
|
|
private double avg;
|
|
|
|
@Column(nullable = false)
|
|
private int count;
|
|
|
|
protected Varying(@NonNull final Id id, @NonNull final Value value) {
|
|
this.id = id;
|
|
final double converted = value.as(id.getSeries().getUnit()).value;
|
|
this.min = converted;
|
|
this.max = converted;
|
|
this.avg = converted;
|
|
this.count = 1;
|
|
}
|
|
|
|
public void update(@NonNull final Value value) {
|
|
final double converted = value.as(id.getSeries().getUnit()).value;
|
|
this.min = Math.min(this.min, converted);
|
|
this.max = Math.max(this.max, converted);
|
|
this.avg = (this.avg * this.count + converted) / ++this.count;
|
|
}
|
|
|
|
@Data
|
|
@Embeddable
|
|
@NoArgsConstructor
|
|
public static class Id {
|
|
|
|
@NonNull
|
|
@ManyToOne
|
|
private Series series;
|
|
|
|
@NonNull
|
|
@Column(nullable = false)
|
|
private ZonedDateTime date;
|
|
|
|
public Id(@NonNull final Series series, @NonNull final ZonedDateTime date, @NonNull final Alignment alignment) {
|
|
this.series = series;
|
|
this.date = alignment.align(date).date;
|
|
}
|
|
|
|
}
|
|
|
|
}
|