@Beta public static <T extends Number> Function<Collection<? extends Number>, T> computingMax( Number defaultValueForUnreportedSensors, Number valueToReportIfNoSensors, Class<T> type) { return computingMax(defaultValueForUnreportedSensors, valueToReportIfNoSensors, TypeToken.of(type)); }
@Test public void testMax() throws Exception { Function<Collection<? extends Number>, Integer> func = MathAggregatorFunctions.computingMax(null, null, Integer.class); assertEquals(func.apply(MutableList.<Number>of(1, 3, 5)), (Integer)5); assertEquals(func.apply(MutableList.<Number>of(3, null, 1)), (Integer)3); } }
@Test public void testValueIfNone() throws Exception { List<Function<Collection<? extends Number>, Integer>> funcs = new ArrayList<>(); funcs.add(MathAggregatorFunctions.computingSum(null, 999, Integer.class)); funcs.add(MathAggregatorFunctions.computingAverage(null, 999, Integer.class)); funcs.add(MathAggregatorFunctions.computingMin(null, 999, Integer.class)); funcs.add(MathAggregatorFunctions.computingMax(null, 999, Integer.class)); for (Function<Collection<? extends Number>, Integer> func : funcs) { assertEquals(func.apply(ImmutableList.<Number>of()), (Integer)999); } }
@Test public void testCastValueWithTypeToken() throws Exception { List<Function<Collection<? extends Number>, Double>> funcs = new ArrayList<>(); funcs.add(MathAggregatorFunctions.computingSum(999, null, doubleTypeToken)); funcs.add(MathAggregatorFunctions.computingAverage(999, null, doubleTypeToken)); funcs.add(MathAggregatorFunctions.computingMin(999, null, doubleTypeToken)); funcs.add(MathAggregatorFunctions.computingMax(999, null, doubleTypeToken)); for (Function<Collection<? extends Number>, Double> func : funcs) { assertEquals(func.apply(MutableList.<Number>of(null)), (Double)999d); } }
@Test public void testCastValue() throws Exception { List<Function<Collection<? extends Number>, Double>> funcs = new ArrayList<>(); funcs.add(MathAggregatorFunctions.computingSum(999, null, Double.class)); funcs.add(MathAggregatorFunctions.computingAverage(999, null, Double.class)); funcs.add(MathAggregatorFunctions.computingMin(999, null, Double.class)); funcs.add(MathAggregatorFunctions.computingMax(999, null, Double.class)); for (Function<Collection<? extends Number>, Double> func : funcs) { assertEquals(func.apply(MutableList.<Number>of(null)), (Double)999d); } }
@Test public void testValueIfNull() throws Exception { List<Function<Collection<? extends Number>, Integer>> funcs = new ArrayList<>(); funcs.add(MathAggregatorFunctions.computingSum(999, null, Integer.class)); funcs.add(MathAggregatorFunctions.computingAverage(999, null, Integer.class)); funcs.add(MathAggregatorFunctions.computingMin(999, null, Integer.class)); funcs.add(MathAggregatorFunctions.computingMax(999, null, Integer.class)); for (Function<Collection<? extends Number>, Integer> func : funcs) { assertEquals(func.apply(MutableList.<Number>of(null)), (Integer)999); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Function<? super Collection<?>, ?> lookupTransformation(String t1) { //return MathAggregatorFunctions.computingAverage( TypeToken<U> targetType = targetSensor.getTypeToken(); Function<?, ?> result; if ("average".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingAverage(null, null, (TypeToken<? extends Number>) targetType); } else if ("sum".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingSum(null, null, (TypeToken<? extends Number>) targetType); } else if ("min".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingMin(null, null, (TypeToken<? extends Number>) targetType); } else if ("max".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingMax(null, null, (TypeToken<? extends Number>) targetType); } else if ("isQuorate".equalsIgnoreCase(t1)) { result = new Enrichers.ComputingIsQuorate(targetType, QuorumChecks.of(config().get(QUORUM_CHECK_TYPE)), config().get(QUORUM_TOTAL_SIZE)); } else if ("list".equalsIgnoreCase(t1)) { result = new ComputingList(); } else if ("first".equalsIgnoreCase(t1)) { result = new FirstOrNull(); } else { result = null; } return (Function<? super Collection<?>, ?>) result; }