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); } MeanFunction(); FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) List list); FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) Number single); FEELFnResult<BigDecimal> invoke(@ParameterName( "n" ) Object[] list);  }
@Test public void invokeNumberNull() { FunctionTestUtil.assertResultError(meanFunction.invoke((Number) null), InvalidParametersEvent.class); }
@Test public void invokeNumberBigDecimal() { FunctionTestUtil.assertResult(meanFunction.invoke(BigDecimal.TEN), BigDecimal.TEN); }
@Test public void invokeNumberInteger() { FunctionTestUtil.assertResult(meanFunction.invoke(10), BigDecimal.TEN); }
@Test public void invokeNumberDoubleWithoutDecimalPart() { FunctionTestUtil.assertResult(meanFunction.invoke(10d), BigDecimal.valueOf(10)); }
@Test public void invokeNumberDoubleWithDecimalPart() { FunctionTestUtil.assertResult(meanFunction.invoke(10.1d), BigDecimal.valueOf(10.1)); }
@Test public void invokeNumberFloat() { FunctionTestUtil.assertResult(meanFunction.invoke(10.1f), BigDecimal.valueOf(10.1)); }
@Test public void invokeUnconvertableNumber() { FunctionTestUtil.assertResultError(meanFunction.invoke(Double.POSITIVE_INFINITY), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(meanFunction.invoke(Double.NEGATIVE_INFINITY), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(meanFunction.invoke(Double.NaN), InvalidParametersEvent.class); }
@Test public void invokeListNull() { FunctionTestUtil.assertResultError(meanFunction.invoke((List) null), InvalidParametersEvent.class); }
@Test public void invokeListEmpty() { FunctionTestUtil.assertResultError(meanFunction.invoke(Collections.emptyList()), InvalidParametersEvent.class); }
@Test public void invokeListTypeHeterogenous() { FunctionTestUtil.assertResultError(meanFunction.invoke(Arrays.asList(1, "test")), InvalidParametersEvent.class); }
@Test public void invokeListWithIntegers() { FunctionTestUtil.assertResult(meanFunction.invoke(Arrays.asList(10, 20, 30)), BigDecimal.valueOf(20)); FunctionTestUtil.assertResult(meanFunction.invoke(Arrays.asList(10, 20, 30, -10, -20, -30)), BigDecimal.ZERO); FunctionTestUtil.assertResult(meanFunction.invoke(Arrays.asList(0, 0, 1)), new BigDecimal("0.3333333333333333333333333333333333")); }
@Test public void invokeListWithDoubles() { FunctionTestUtil.assertResult(meanFunction.invoke(Arrays.asList(10.0d, 20.0d, 30.0d)), BigDecimal.valueOf(20)); FunctionTestUtil.assertResult(meanFunction.invoke(Arrays.asList(10.2d, 20.2d, 30.2d)), BigDecimal.valueOf(20.2)); }
@Test public void invokeArrayNull() { FunctionTestUtil.assertResultError(meanFunction.invoke((Object[]) null), InvalidParametersEvent.class); }
@Test public void invokeArrayEmpty() { FunctionTestUtil.assertResultError(meanFunction.invoke(new Object[]{}), InvalidParametersEvent.class); }
@Test public void invokeArrayTypeHeterogenous() { FunctionTestUtil.assertResultError(meanFunction.invoke(new Object[]{1, "test"}), InvalidParametersEvent.class); }
@Test public void invokeArrayWithIntegers() { FunctionTestUtil.assertResult(meanFunction.invoke(new Object[]{10, 20, 30}), BigDecimal.valueOf(20)); FunctionTestUtil.assertResult(meanFunction.invoke(new Object[]{10, 20, 30, -10, -20, -30}), BigDecimal.ZERO); FunctionTestUtil.assertResult(meanFunction.invoke(new Object[]{0, 0, 1}), new BigDecimal("0.3333333333333333333333333333333333")); }
@Test public void invokeArrayWithDoubles() { FunctionTestUtil.assertResult(meanFunction.invoke(new Object[]{10.0d, 20.0d, 30.0d}), BigDecimal.valueOf(20)); FunctionTestUtil.assertResult(meanFunction.invoke(new Object[]{10.2d, 20.2d, 30.2d}), BigDecimal.valueOf(20.2)); }