@Beta public static <T extends Number> Function<Collection<? extends Number>, T> computingAverage( Number defaultValueForUnreportedSensors, Number valueToReportIfNoSensors, Class<T> type) { return computingAverage(defaultValueForUnreportedSensors, valueToReportIfNoSensors, TypeToken.of(type)); }
@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)); }
@Beta public static <T extends Number> Function<Collection<? extends Number>, T> computingMin( Number defaultValueForUnreportedSensors, Number valueToReportIfNoSensors, Class<T> type) { return computingMin(defaultValueForUnreportedSensors, valueToReportIfNoSensors, TypeToken.of(type)); }
@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); } }
@Beta public static <T extends Number> Function<Collection<? extends Number>, T> computingSum( Number defaultValueForUnreportedSensors, Number valueToReportIfNoSensors, Class<T> type) { return computingSum(defaultValueForUnreportedSensors, valueToReportIfNoSensors, TypeToken.of(type)); }
@Override public T apply(@Nullable Collection<? extends Number> vals) { List<Number> postProcessedVals = new ArrayList<>(); int count = 0; if (vals != null) { for (Number val : vals) { if (val != null) { postProcessedVals.add(val); count++; } else if (defaultValueForUnreportedSensors != null) { postProcessedVals.add(defaultValueForUnreportedSensors); count++; } } } if (count==0) return cast(valueToReportIfNoSensors, typeToken); Number result = applyImpl(postProcessedVals); return cast(result, typeToken); }
@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 testSum() throws Exception { Function<Collection<? extends Number>, Integer> func = MathAggregatorFunctions.computingSum(null, null, Integer.class); assertEquals(func.apply(MutableList.<Number>of(1, 2, 4)), (Integer)7); assertEquals(func.apply(MutableList.<Number>of(1, null, 4)), (Integer)5); }
@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 testMin() throws Exception { Function<Collection<? extends Number>, Integer> func = MathAggregatorFunctions.computingMin(null, null, Integer.class); assertEquals(func.apply(MutableList.<Number>of(1, 3, 5)), (Integer)1); assertEquals(func.apply(MutableList.<Number>of(3, null, 1)), (Integer)1); }
@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 testAverage() throws Exception { Function<Collection<? extends Number>, Integer> func = MathAggregatorFunctions.computingAverage(null, null, Integer.class); assertEquals(func.apply(MutableList.<Number>of(1, 3, 5)), (Integer)3); assertEquals(func.apply(MutableList.<Number>of(1, null, 3)), (Integer)2); }
@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; }