class MeanFunction extends BaseFEELFunction {

    public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) List list) {
        if ( list == null ) {
            return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
        }

        FEELFnResult<BigDecimal> s = sum.invoke( list );
        
        Function<FEELEvent, FEELFnResult<BigDecimal>> ifLeft = (event) -> {
            return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "unable to sum the elements which is required to calculate the mean"));
        };
        
        Function<BigDecimal, FEELFnResult<BigDecimal>> ifRight = (sum) -> {
            try {
                return FEELFnResult.ofResult( sum.divide( BigDecimal.valueOf( list.size() ), MathContext.DECIMAL128 ) );
            } catch (Exception e) {
                return FEELFnResult.ofError( new InvalidParametersEvent(Severity.ERROR, "unable to perform division to calculate the mean", e) );
            }
        };
        
        return s.cata(ifLeft, ifRight);
    }

    public  MeanFunction();

}