/*
 *    Copyright 2017-2019 Frederic Thevenet
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package eu.binjr.core.data.timeseries;

import eu.binjr.core.data.workspace.TimeSeriesInfo;
import javafx.scene.chart.XYChart;

import java.util.OptionalDouble;

/**
 * This class provides a full implementation of a {@link TimeSeriesProcessor} of {@link Double} values.
 *
 * @author Frederic Thevenet
 */
public class DoubleTimeSeriesProcessor extends TimeSeriesProcessor {

    /**
     * Initializes a new instance of the {@link DoubleTimeSeriesProcessor} class with the provided binding.
     */
    public DoubleTimeSeriesProcessor() {
        super();
    }

    @Override
    public Double computeMinValue() {
        OptionalDouble res = this.data.stream().mapToDouble(XYChart.Data::getYValue).min();
        return res.isPresent() ? res.getAsDouble() : Double.NaN;
    }

    @Override
    public Double computeAverageValue() {
        OptionalDouble res = this.data.stream().mapToDouble(XYChart.Data::getYValue).average();
        return res.isPresent() ? res.getAsDouble() : Double.NaN;
    }

    @Override
    public Double computeMaxValue() {
        OptionalDouble res = this.data.stream().mapToDouble(XYChart.Data::getYValue).max();
        return res.isPresent() ? res.getAsDouble() : Double.NaN;
    }
}

@@ -1256,36 +1256,22 @@
         
-OptionalDouble res =
+return
  this.da
@@ -1273,32 +1273,49 @@
 is.data.stream()
+%0A                
 .mapToDouble(XYC
@@ -1339,15 +1339,8 @@
 lue)
-.min();
 %0A   
@@ -1348,53 +1348,93 @@
     
-return res.isPresent() ? res.getAsDouble() : 
+        .filter(d -%3E!Double.isNaN(d))%0A                .min()%0A                .orElse(
 Doub
@@ -1431,32 +1431,33 @@
 rElse(Double.NaN
+)
 ;%0A    %7D%0A%0A    @Ov
@@ -1512,37 +1512,22 @@
 %0A       
- OptionalDouble res =
+return
  this.da
@@ -1529,32 +1529,48 @@
 is.data.stream()
+%0A               
 .mapToDouble(XYC
@@ -1594,73 +1594,103 @@
 lue)
-.average();%0A        return res.isPresent() ? res.getAsDouble() : 
+%0A               .filter(d -%3E!Double.isNaN(d))%0A               .average()%0A               .orElse(
 Doub
@@ -1687,32 +1687,33 @@
 rElse(Double.NaN
+)
 ;%0A    %7D%0A%0A    @Ov
@@ -1768,29 +1768,14 @@
     
- OptionalDouble res =
+return
  thi
@@ -1789,16 +1789,32 @@
 stream()
+%0A               
 .mapToDo
@@ -1846,69 +1846,99 @@
 lue)
-.max();%0A        return res.isPresent() ? res.getAsDouble() : 
+%0A               .filter(d -%3E!Double.isNaN(d))%0A               .max()%0A               .orElse(
 Doub
@@ -1943,16 +1943,17 @@
 uble.NaN
+)
 ;%0A    %7D%0A

Handle NaN values in aggregate methods in DoubleTimeSeriesProcessor.