brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbou...@apache.org
Subject [2/3] brooklyn-server git commit: BROOKLYN-569: aggregator casts vals to numbers
Date Thu, 21 Dec 2017 14:23:11 GMT
BROOKLYN-569: aggregator casts vals to numbers

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/f78cc06b
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/f78cc06b
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/f78cc06b

Branch: refs/heads/master
Commit: f78cc06b2a23850e751ad3a9d8878fd0b585b72e
Parents: cedbd92
Author: Aled Sage <aled.sage@gmail.com>
Authored: Mon Dec 11 17:15:50 2017 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Thu Dec 21 09:31:38 2017 +0000

----------------------------------------------------------------------
 .../enricher/stock/MathAggregatorFunctions.java     | 16 +++++++++++-----
 .../enricher/stock/MathAggregatorFunctionsTest.java | 10 ++++++++++
 .../util/javalang/coerce/TypeCoercionsTest.java     | 12 ++++++++++++
 3 files changed, 33 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f78cc06b/core/src/main/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctions.java
b/core/src/main/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctions.java
index a67befd..c8509d5 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctions.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctions.java
@@ -25,6 +25,9 @@ import java.util.List;
 import javax.annotation.Nullable;
 
 import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
@@ -33,6 +36,8 @@ import com.google.common.reflect.TypeToken;
 @Beta
 public class MathAggregatorFunctions {
 
+    private static final Logger LOG = LoggerFactory.getLogger(MathAggregatorFunctions.class);
+
     private MathAggregatorFunctions() {}
     
     @Beta
@@ -118,9 +123,10 @@ public class MathAggregatorFunctions {
             List<Number> postProcessedVals = new ArrayList<>();
             int count = 0;
             if (vals != null) {
-                for (Number val : vals) { 
-                    if (val != null) {
-                        postProcessedVals.add(val);
+                for (Object val : vals) {
+                    Maybe<Number> coercedVal = TypeCoercions.tryCoerce(val, Number.class);
+                    if (coercedVal.isPresentAndNonNull()) {
+                        postProcessedVals.add(coercedVal.get());
                         count++;
                     } else if (defaultValueForUnreportedSensors != null) {
                         postProcessedVals.add(defaultValueForUnreportedSensors);
@@ -145,7 +151,7 @@ public class MathAggregatorFunctions {
         @Override
         public Number applyImpl(Collection<Number> vals) {
             double result = 0d;
-            for (Number val : vals) { 
+            for (Number val : vals) {
                 result += val.doubleValue();
             }
             return result;
@@ -160,7 +166,7 @@ public class MathAggregatorFunctions {
         @Override
         public Number applyImpl(Collection<Number> vals) {
             double sum = 0d;
-            for (Number val : vals) { 
+            for (Number val : vals) {
                 sum += val.doubleValue();
             }
             return (sum / vals.size());

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f78cc06b/core/src/test/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctionsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctionsTest.java
b/core/src/test/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctionsTest.java
index d3f4308..d07b0db 100644
--- a/core/src/test/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctionsTest.java
+++ b/core/src/test/java/org/apache/brooklyn/enricher/stock/MathAggregatorFunctionsTest.java
@@ -88,6 +88,16 @@ public class MathAggregatorFunctionsTest {
         }
     }
     
+    // See https://issues.apache.org/jira/browse/BROOKLYN-569
+    // Casting like this can be required when used in aggregators - the input sensors may
not have been cast.
+    @Test
+    public void testCastInputValuesToNumbers() throws Exception {
+        Function<Collection<? extends Number>, Integer> func = MathAggregatorFunctions.computingSum(null,
null, Integer.class);
+        @SuppressWarnings({ "rawtypes", "unchecked" })
+        List<Number> input = (List<Number>) (List) MutableList.<Object>of("1",
null, "4");
+        assertEquals(func.apply(input), (Integer)5);
+    }
+    
     @Test
     public void testSum() throws Exception {
         Function<Collection<? extends Number>, Integer> func = MathAggregatorFunctions.computingSum(null,
null, Integer.class);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f78cc06b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
index c5e989b..c3aea92 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.util.javalang.coerce;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertSame;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -485,6 +486,13 @@ public class TypeCoercionsTest {
     }
 
     @Test
+    public void testCoerceToSameTypeIsNoop() {
+        assertCoerceToSame(Integer.valueOf(1), Integer.class);
+        assertCoerceToSame(Integer.valueOf(1), Number.class);
+        assertCoerceToSame("myval", String.class);
+    }
+
+    @Test
     public void testCoerceStringToPredicate() {
         assertEquals(coerce("alwaysFalse", Predicate.class), Predicates.alwaysFalse());
         assertEquals(coerce("alwaysTrue", Predicate.class), Predicates.alwaysTrue());
@@ -509,6 +517,10 @@ public class TypeCoercionsTest {
         assertEquals(coercer.function(Double.class).apply("1"), Double.valueOf(1));
     }
 
+    private <T> void assertCoerceToSame(T val, Class<? super T> type) {
+        assertSame(coerce(val, type), val);
+    }
+    
     public static class WithAs {
         String value;
         public WithAs(Object x) { value = ""+x; }


Mime
View raw message