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 static <T> FEELFnResult<T> ofResult(T value) { return new FEELFnResult<>(Optional.empty(), Optional.ofNullable(value)); }
private FEELFnResult<List<Object>> invoke(final List list, final Comparator<? super Object> comparator) { if ( list == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null")); } final List<Object> newList = new ArrayList<Object>( list ); try { newList.sort(comparator); } catch (final Throwable ex) { return FEELFnResult.ofError( new InvalidParametersEvent(Severity.ERROR, "list", "raised an exception while sorting by natural order", ex ) ); } return FEELFnResult.ofResult( newList ); }
public static Number median(List<Number> list) { return medianFunction.invoke(list).cata(e -> null, Function.identity()); }
public FEELFnResult<TemporalAccessor> invoke() { return FEELFnResult.ofResult( ZonedDateTime.now() ); }
@Test public void invoke() { // The current time that we need to compare will almost never be the same as another one we get for comparison purposes, // because there is some execution between them, so the comparison assertion doesn't make sense. // Note: We cannot guarantee any part of the date to be the same. E.g. in case when the test is executed // at the exact moment when the year is flipped to the next one, we cannot guarantee the year will be the same. final FEELFnResult<TemporalAccessor> nowResult = nowFunction.invoke(); Assert.assertThat(nowResult.isRight(), Matchers.is(true)); final TemporalAccessor result = nowResult.cata(left -> null, right -> right); Assert.assertThat(result, Matchers.notNullValue()); Assert.assertThat(result.getClass(), Matchers.typeCompatibleWith(ZonedDateTime.class)); }
private static void assertNull(final FEELFnResult<?> result) { Assert.assertNull(result.getOrElse(null)); } }
public static <T> void assertResultError(final FEELFnResult<T> result, final Class expectedErrorEventClass) { Assert.assertThat(result, Matchers.notNullValue()); Assert.assertThat(result.isLeft(), Matchers.is(true)); final FEELEvent resultEvent = result.cata(left -> left, right -> null); checkErrorEvent(resultEvent, expectedErrorEventClass); }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Object sole) { if ( sole == null ) { // Arrays.asList does not accept null as parameter return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "the single value list cannot be null")); } else if (EvalHelper.getBigDecimalOrNull(sole) == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "the value can not be converted to a number")); } return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "sample standard deviation of a single sample is undefined")); }
public static <T> void assertResultNotError(final FEELFnResult<T> result) { Assert.assertThat(result, Matchers.notNullValue()); Assert.assertThat(result.isRight(), Matchers.is(true)); }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single) { if ( single == null ) { return FEELFnResult.ofResult( null ); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list not a number")); } }
public static Boolean all(List<Boolean> list) { return allFunction.invoke(list).cata(e -> Boolean.FALSE, Function.identity()); }
@Test public void testComposite1() { final FEELFnResult<TemporalAccessor> p1 = dateTimeFunction.invoke("2017-08-10T10:20:00+02:00"); final FEELFnResult<TemporalAccessor> p2 = timeFunction.invoke("23:59:01"); FunctionTestUtil.assertResult(p1, ZonedDateTime.of(2017, 8, 10, 10, 20, 0, 0, ZoneId.of("+02:00"))); FunctionTestUtil.assertResult(p2, LocalTime.of(23, 59, 1)); final FEELFnResult<TemporalAccessor> result = dateTimeFunction.invoke(p1.getOrElse(null), p2.getOrElse(null)); FunctionTestUtil.assertResult(result, LocalDateTime.of(2017, 8, 10, 23, 59, 1)); }
public FEELFnResult<Boolean> invoke(@ParameterName( "b" ) Object[] list) { if ( list == null ) { // Arrays.asList does not accept null as parameter return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "b", "cannot be null")); } return invoke( Arrays.asList( list ) ); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) Number single) { if ( single == null ) { return FEELFnResult.ofResult(null); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list is not a number")); } }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("from") String val) { if (val == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "cannot be null")); } if (!BEGIN_YEAR.matcher(val).find()) { // please notice the regex strictly requires the beginning, so we can use find. return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "year not compliant with XML Schema Part 2 Datatypes")); } try { return FEELFnResult.ofResult(LocalDate.from(FEEL_DATE.parse(val))); } catch (DateTimeException e) { // try to parse it as a date time and extract the date component // NOTE: this is an extension to the standard return BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(val) .cata(overrideLeft -> FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "date-parsing exception", e)), this::invoke ); } }
public static Boolean any(List<Boolean> list) { return anyFunction.invoke(list).cata(e -> Boolean.FALSE, Function.identity()); }