brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [5/8] incubator-brooklyn git commit: uses sensor predicate for equality check
Date Fri, 09 Oct 2015 08:37:16 GMT
uses sensor predicate for equality check


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

Branch: refs/heads/master
Commit: 401ab13a77f3df2dc747eb553a0c0c6d818aa746
Parents: ae488e6
Author: Robert Moss <robertgmoss@gmail.com>
Authored: Fri Oct 2 09:46:22 2015 +0100
Committer: Robert Moss <robertgmoss@gmail.com>
Committed: Fri Oct 2 09:46:22 2015 +0100

----------------------------------------------------------------------
 .../brooklyn/enricher/stock/Enrichers.java      |  4 +-
 .../brooklyn/enricher/stock/Propagator.java     |  9 ++--
 .../brooklyn/enricher/stock/Transformer.java    |  2 -
 .../stock/reducer/GenericStringReducer.java     |  3 +-
 .../enricher/stock/reducer/Reducer.java         | 14 ++++--
 .../stock/reducer/StringStringReducer.java      |  3 +-
 .../util/core/sensor/SensorPredicates.java      | 49 ++++++++++++++++++++
 .../util/core/sensor/SensorPredicatesTest.java  | 38 +++++++++++++++
 8 files changed, 106 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/enricher/stock/Enrichers.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/Enrichers.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/Enrichers.java
index 8c73653..18bae9c 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/Enrichers.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/Enrichers.java
@@ -691,7 +691,7 @@ public class Enrichers {
         private Map<String, Object> parameters;
 
         public AbstractReducerBuilder(Class<? extends Reducer<S, T>> clazz, List<AttributeSensor<S>>
val) {
-            super(clazz);
+            super(checkNotNull(clazz));
             this.reducing = checkNotNull(val);
         }
         
@@ -726,7 +726,7 @@ public class Enrichers {
                     .put(Reducer.PRODUCER, fromEntity)
                     .put(Reducer.TARGET_SENSOR, publishing)
                     .putIfNotNull(Reducer.REDUCER_FUNCTION, computing)
-                    .putIfNotNull(Reducer.REDUCER_FUNCTION_UNTYPED, functionName)
+                    .putIfNotNull(Reducer.REDUCER_FUNCTION_TRANSFORMATION, functionName)
                     .putIfNotNull(Reducer.PARAMETERS, parameters)
                     .build()
             );

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/enricher/stock/Propagator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/Propagator.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/Propagator.java
index 0cf601c..711635e 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/Propagator.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/Propagator.java
@@ -34,6 +34,7 @@ import org.apache.brooklyn.core.enricher.AbstractEnricher;
 import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
+import org.apache.brooklyn.util.core.sensor.SensorPredicates;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.core.task.ValueResolver;
 import org.slf4j.Logger;
@@ -198,12 +199,8 @@ public class Propagator extends AbstractEnricher implements SensorEventListener<
 
     private Sensor<?> getDestinationSensor(final Sensor<?> sourceSensor) {
         // sensor equality includes the type; we want just name-equality so will use predicate.
-        Optional<? extends Sensor<?>> mappingSensor = Iterables.tryFind(sensorMapping.keySet(),
new Predicate<Sensor<?>>() {
-            @Override
-            public boolean apply(Sensor<?> sensor) {
-                return sourceSensor.getName().equals(sensor.getName());
-            }
-        });
+        Optional<? extends Sensor<?>> mappingSensor = Iterables.tryFind(sensorMapping.keySet(),

+                SensorPredicates.sensorNameEqualTo(sourceSensor.getName()));
 
         return mappingSensor.isPresent() ? sensorMapping.get(mappingSensor.get()) : sourceSensor;
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
index f15b2b2..ef23ab4 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
@@ -23,8 +23,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import org.apache.brooklyn.api.sensor.SensorEvent;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.entity.Attributes;
-import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.core.task.ValueResolver;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/GenericStringReducer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/GenericStringReducer.java
b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/GenericStringReducer.java
index 09cec13..3dacb60 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/GenericStringReducer.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/GenericStringReducer.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.enricher.stock.reducer;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
@@ -29,7 +30,7 @@ public abstract class GenericStringReducer<T> extends Reducer<T,
String>{
     @Override
     protected Function<List<T>, String> createReducerFunction(
             String reducerName, Map<String, ?> parameters) {
-        if (reducerName.equals("formatString")){
+        if (Objects.equals(reducerName, "formatString")){
             String format = Preconditions.checkNotNull((String)parameters.get("format"),
"format");
             return new FormatStringReducerFunction<T>(format);
         }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/Reducer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/Reducer.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/Reducer.java
index dfce94e..2c1c4c7 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/Reducer.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/Reducer.java
@@ -32,6 +32,7 @@ import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.enricher.AbstractEnricher;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
+import org.apache.brooklyn.util.core.sensor.SensorPredicates;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.core.task.ValueResolver;
 import org.slf4j.Logger;
@@ -39,8 +40,10 @@ import org.slf4j.LoggerFactory;
 
 import com.google.api.client.util.Lists;
 import com.google.common.base.Function;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import com.google.common.reflect.TypeToken;
 
 @SuppressWarnings("serial")
@@ -54,8 +57,8 @@ public abstract class Reducer<S, T> extends AbstractEnricher implements
SensorEv
     public static ConfigKey<List<? extends AttributeSensor<?>>> SOURCE_SENSORS
= ConfigKeys.newConfigKey(new TypeToken<List<? extends AttributeSensor<?>>>()
{}, "enricher.sourceSensors");
     public static ConfigKey<Function<List<?>,?>> REDUCER_FUNCTION = ConfigKeys.newConfigKey(new
TypeToken<Function<List<?>, ?>>() {}, "enricher.reducerFunction");
     @SetFromFlag("transformation")
-    public static final ConfigKey<String> REDUCER_FUNCTION_UNTYPED = ConfigKeys.newStringConfigKey("enricher.reducerFunction.untyped",
-        "A string matching a pre-defined named reducer function, such as join");
+    public static final ConfigKey<String> REDUCER_FUNCTION_TRANSFORMATION = ConfigKeys.newStringConfigKey("enricher.reducerFunction.transformation",
+        "A string matching a pre-defined named reducer function, such as joiner");
     public static final ConfigKey<Map<String, Object>> PARAMETERS = ConfigKeys.newConfigKey(new
TypeToken<Map<String, Object>>() {}, "enricher.reducerFunction.parameters", 
         "A map of parameters to pass into the reducer function");
    
@@ -75,12 +78,15 @@ public abstract class Reducer<S, T> extends AbstractEnricher implements
SensorEv
 
         for (Object sensorO : getConfig(SOURCE_SENSORS)) {
             AttributeSensor<S> sensor = Tasks.resolving(sensorO).as(AttributeSensor.class).timeout(ValueResolver.REAL_QUICK_WAIT).context(producer).get();
-            if(!sensorListTemp.contains(sensor)) {
+            Optional<? extends Sensor<?>> foundSensor = Iterables.tryFind(sensorListTemp,

+                    SensorPredicates.sensorNameEqualTo(sensor.getName()));
+            
+            if(!foundSensor.isPresent()) {
                 sensorListTemp.add(sensor);
             }
         }
         
-        String reducerName = config().get(REDUCER_FUNCTION_UNTYPED);
+        String reducerName = config().get(REDUCER_FUNCTION_TRANSFORMATION);
         Function<List<S>, T> reducerFunction = (Function) config().get(REDUCER_FUNCTION);
         if(reducerFunction == null){
             Map<String, ?> parameters = config().get(PARAMETERS);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/StringStringReducer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/StringStringReducer.java
b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/StringStringReducer.java
index 163f483..3180240 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/StringStringReducer.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/StringStringReducer.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.enricher.stock.reducer;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import com.google.common.base.Function;
 
@@ -33,7 +34,7 @@ public class StringStringReducer extends GenericStringReducer<String>
{
         Function<List<String>, String> function = super.createReducerFunction(reducerName,
parameters);
         if(function != null) return function;
         
-        if(reducerName.equals("joiner")){
+        if(Objects.equals(reducerName, "joiner")){
             return new JoinerFunction(parameters.get("separator"));
         }
         throw new IllegalStateException("unknown function: " + reducerName);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/main/java/org/apache/brooklyn/util/core/sensor/SensorPredicates.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/sensor/SensorPredicates.java
b/core/src/main/java/org/apache/brooklyn/util/core/sensor/SensorPredicates.java
new file mode 100644
index 0000000..5106557
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/util/core/sensor/SensorPredicates.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.core.sensor;
+
+import org.apache.brooklyn.api.sensor.Sensor;
+
+import com.google.common.base.Predicate;
+
+public class SensorPredicates {
+
+    private SensorPredicates() {
+        // not instantiable
+    }
+    
+    public static Predicate<Sensor<?>> sensorNameEqualTo(String sensorName) {
+        return new SensorNameEquals(sensorName);
+    }
+
+    private static class SensorNameEquals implements Predicate<Sensor<?>> {
+        
+        private String sensor;
+
+        public SensorNameEquals(String sensor) {
+            this.sensor = sensor;
+        }
+
+        @Override
+        public boolean apply(Sensor<?> other) {
+            return this.sensor.equals(other.getName());
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/401ab13a/core/src/test/java/org/apache/brooklyn/util/core/sensor/SensorPredicatesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/util/core/sensor/SensorPredicatesTest.java
b/core/src/test/java/org/apache/brooklyn/util/core/sensor/SensorPredicatesTest.java
new file mode 100644
index 0000000..d9cd186
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/util/core/sensor/SensorPredicatesTest.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.core.sensor;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.testng.annotations.Test;
+
+public class SensorPredicatesTest extends BrooklynAppUnitTestSupport {
+
+    @Test
+    public void testDisplayNameEqualTo() throws Exception {
+        Sensor<Object> task = Sensors.newSensor(Object.class, "myname");
+        assertTrue(SensorPredicates.sensorNameEqualTo("myname").apply(task));
+        assertFalse(SensorPredicates.sensorNameEqualTo("wrong").apply(task));
+    }
+    
+}


Mime
View raw message