+ public AltosTimeValue first() {
+ if (values.size() > 0)
+ return values.get(0);
+ return null;
+ }
+
+ public AltosTimeValue last() {
+ if (values.size() > 0)
+ return values.get(values.size() - 1);
+ return null;
+ }
+
+ public double average() {
+ double total_value = 0;
+ double total_time = 0;
+ AltosTimeValue prev = null;
+ for (AltosTimeValue tv : values) {
+ if (prev != null) {
+ total_value += (tv.value + prev.value) / 2 * (tv.time - prev.time);
+ total_time += (tv.time - prev.time);
+ }
+ prev = tv;
+ }
+ if (total_time == 0)
+ return AltosLib.MISSING;
+ return total_value / total_time;
+ }
+
+ public double average(double start_time, double end_time) {
+ double total_value = 0;
+ double total_time = 0;
+ AltosTimeValue prev = null;
+ for (AltosTimeValue tv : values) {
+ if (start_time <= tv.time && tv.time <= end_time) {
+ if (prev != null) {
+ total_value += (tv.value + prev.value) / 2 * (tv.time - start_time);
+ total_time += (tv.time - start_time);
+ }
+ start_time = tv.time;
+ }
+ prev = tv;
+ }
+ if (total_time == 0)
+ return AltosLib.MISSING;
+ return total_value / total_time;
+ }
+