Average.java 808 Bytes
package kernel.function;

import kernel.Cell;

import java.util.OptionalDouble;
import java.util.stream.Collectors;

public class Average extends Function {

    public String toString() {
        return "MOYENNE(" + this.listCells.stream()
                .map(Cell::getId)
                .collect(Collectors.joining(","))
                + ")";
    }

    public String getDevelopedFormula() {
        return "MOYENNE(" + this.listCells.stream()
                .map(Cell::getDevelopedFormula)
                .collect(Collectors.joining(","))
                + ")";
    }

    public Double eval() {
        OptionalDouble average = this.listCells.stream()
                .mapToDouble(Cell::getValue)
                .average();

        return average.isPresent() ? average.getAsDouble() : 0.;
    }
}