use single Reducer class
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/0f59c13f Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/0f59c13f Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/0f59c13f Branch: refs/heads/master Commit: 0f59c13f697fe20953ef781117385baf3c453f45 Parents: 401ab13 Author: Robert Moss <[email protected]> Authored: Thu Oct 8 21:47:39 2015 +0100 Committer: Robert Moss <[email protected]> Committed: Thu Oct 8 21:47:39 2015 +0100 ---------------------------------------------------------------------- .../brooklyn/enricher/stock/Enrichers.java | 22 ++-- .../brooklyn/enricher/stock/Propagator.java | 2 +- .../stock/reducer/GenericStringReducer.java | 40 ------- .../stock/reducer/ObjectStringReducer.java | 37 ------- .../enricher/stock/reducer/Reducer.java | 109 +++++-------------- .../stock/reducer/StringStringReducer.java | 42 ------- .../util/core/sensor/SensorPredicates.java | 8 +- .../enricher/stock/reducer/ReducerTest.java | 57 ++++------ .../util/core/sensor/SensorPredicatesTest.java | 4 +- .../brooklyn/util/text/StringFunctions.java | 16 +++ 10 files changed, 86 insertions(+), 251 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 18bae9c..64da60b 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 @@ -173,8 +173,8 @@ public class Enrichers { public JoinerBuilder joining(AttributeSensor<?> source) { return new JoinerBuilder(source); } - public <S, T> ReducerBuilder<S, T> reducing(Class<? extends Reducer<S, T>> clazz, List<AttributeSensor<S>> sourceSensors) { - return new ReducerBuilder<S, T>(clazz, sourceSensors); + public ReducerBuilder reducing(Class<? extends Reducer> clazz, List<AttributeSensor<?>> sourceSensors) { + return new ReducerBuilder(clazz, sourceSensors); } } @@ -682,20 +682,20 @@ public class Enrichers { } } - protected abstract static class AbstractReducerBuilder<S, T, B extends AbstractReducerBuilder<S, T, B>> extends AbstractEnricherBuilder<B> { - protected AttributeSensor<T> publishing; + protected abstract static class AbstractReducerBuilder<B extends AbstractReducerBuilder<B>> extends AbstractEnricherBuilder<B> { + protected AttributeSensor<?> publishing; protected Entity fromEntity; - protected List<AttributeSensor<S>> reducing; - protected Function<List<S>, T> computing; + protected List<AttributeSensor<?>> reducing; + protected Function<? extends Iterable<?>, ?> computing; protected String functionName; private Map<String, Object> parameters; - public AbstractReducerBuilder(Class<? extends Reducer<S, T>> clazz, List<AttributeSensor<S>> val) { + public AbstractReducerBuilder(Class<? extends Reducer> clazz, List<AttributeSensor<?>> val) { super(checkNotNull(clazz)); this.reducing = checkNotNull(val); } - public B publishing(AttributeSensor<T> val) { + public B publishing(AttributeSensor<?> val) { this.publishing = checkNotNull(val); return self(); } @@ -705,7 +705,7 @@ public class Enrichers { return self(); } - public B computing(Function<List<S>, T> val) { + public B computing(Function<? extends Iterable<?>, ?> val) { this.computing = checkNotNull(val); return self(); } @@ -790,8 +790,8 @@ public class Enrichers { } } - public static class ReducerBuilder<S, T> extends AbstractReducerBuilder<S, T, ReducerBuilder<S, T>> { - public ReducerBuilder(Class<? extends Reducer<S, T>> clazz, List<AttributeSensor<S>> val) { + public static class ReducerBuilder extends AbstractReducerBuilder<ReducerBuilder> { + public ReducerBuilder(Class<? extends Reducer> clazz, List<AttributeSensor<?>> val) { super(clazz, val); } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 711635e..e13a835 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 @@ -200,7 +200,7 @@ 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(), - SensorPredicates.sensorNameEqualTo(sourceSensor.getName())); + SensorPredicates.nameEqualTo(sourceSensor.getName())); return mappingSensor.isPresent() ? sensorMapping.get(mappingSensor.get()) : sourceSensor; } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 deleted file mode 100644 index 3dacb60..0000000 --- a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/GenericStringReducer.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.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; - -public abstract class GenericStringReducer<T> extends Reducer<T, String>{ - - @Override - protected Function<List<T>, String> createReducerFunction( - String reducerName, Map<String, ?> parameters) { - if (Objects.equals(reducerName, "formatString")){ - String format = Preconditions.checkNotNull((String)parameters.get("format"), "format"); - return new FormatStringReducerFunction<T>(format); - } - return null; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/ObjectStringReducer.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/ObjectStringReducer.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/ObjectStringReducer.java deleted file mode 100644 index c09f282..0000000 --- a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/ObjectStringReducer.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.enricher.stock.reducer; - -import java.util.List; -import java.util.Map; - -import com.google.common.base.Function; - -public class ObjectStringReducer extends GenericStringReducer<Object> { - - @Override - protected Function<List<Object>, String> createReducerFunction( - String reducerName, Map<String, ?> parameters) { - - Function<List<Object>, String> function = super.createReducerFunction(reducerName, parameters); - if(function != null) return function; - - throw new IllegalStateException("unknown function: " + reducerName); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 2c1c4c7..db3a72d 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 @@ -18,9 +18,9 @@ */ package org.apache.brooklyn.enricher.stock.reducer; -import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntityLocal; @@ -35,6 +35,7 @@ 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.apache.brooklyn.util.text.StringFunctions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +48,7 @@ import com.google.common.collect.Iterables; import com.google.common.reflect.TypeToken; @SuppressWarnings("serial") -public abstract class Reducer<S, T> extends AbstractEnricher implements SensorEventListener<Object> { +public class Reducer extends AbstractEnricher implements SensorEventListener<Object> { private static final Logger LOG = LoggerFactory.getLogger(Reducer.class); @@ -63,9 +64,9 @@ public abstract class Reducer<S, T> extends AbstractEnricher implements SensorEv "A map of parameters to pass into the reducer function"); protected Entity producer; - protected List<AttributeSensor<S>> subscribedSensors; - protected Sensor<T> targetSensor; - protected Function<List<S>, T> reducerFunction; + protected List<AttributeSensor<?>> subscribedSensors; + protected Sensor<?> targetSensor; + protected Function<Iterable<?>, ?> reducerFunction; @SuppressWarnings({ "unchecked", "rawtypes" }) @Override @@ -74,12 +75,12 @@ public abstract class Reducer<S, T> extends AbstractEnricher implements SensorEv Preconditions.checkNotNull(getConfig(SOURCE_SENSORS), "source sensors"); this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER); - List<AttributeSensor<S>> sensorListTemp = Lists.newArrayList(); + List<AttributeSensor<?>> sensorListTemp = Lists.newArrayList(); for (Object sensorO : getConfig(SOURCE_SENSORS)) { - AttributeSensor<S> sensor = Tasks.resolving(sensorO).as(AttributeSensor.class).timeout(ValueResolver.REAL_QUICK_WAIT).context(producer).get(); + AttributeSensor<?> sensor = Tasks.resolving(sensorO).as(AttributeSensor.class).timeout(ValueResolver.REAL_QUICK_WAIT).context(producer).get(); Optional<? extends Sensor<?>> foundSensor = Iterables.tryFind(sensorListTemp, - SensorPredicates.sensorNameEqualTo(sensor.getName())); + SensorPredicates.nameEqualTo(sensor.getName())); if(!foundSensor.isPresent()) { sensorListTemp.add(sensor); @@ -87,7 +88,7 @@ public abstract class Reducer<S, T> extends AbstractEnricher implements SensorEv } String reducerName = config().get(REDUCER_FUNCTION_TRANSFORMATION); - Function<List<S>, T> reducerFunction = (Function) config().get(REDUCER_FUNCTION); + Function<Iterable<?>, ?> reducerFunction = (Function) config().get(REDUCER_FUNCTION); if(reducerFunction == null){ Map<String, ?> parameters = config().get(PARAMETERS); reducerFunction = createReducerFunction(reducerName, parameters); @@ -96,92 +97,42 @@ public abstract class Reducer<S, T> extends AbstractEnricher implements SensorEv this.reducerFunction = reducerFunction; Preconditions.checkState(sensorListTemp.size() > 0, "Nothing to reduce"); - for (Sensor<S> sensor : sensorListTemp) { + for (Sensor<?> sensor : sensorListTemp) { subscribe(producer, sensor, this); } subscribedSensors = ImmutableList.copyOf(sensorListTemp); } - - protected abstract Function<List<S>, T> createReducerFunction(String reducerName, Map<String, ?> parameters); - @SuppressWarnings("unchecked") + // Default implementation, subclasses should override + protected Function<Iterable<?>, ?> createReducerFunction(String reducerName, Map<String, ?> parameters){ + if(Objects.equals(reducerName, "joiner")){ + String separator = (String) parameters.get("separator"); + return StringFunctions.joiner(separator == null ? ", " : separator); + } + + if (Objects.equals(reducerName, "formatString")){ + String format = Preconditions.checkNotNull((String)parameters.get("format"), "format"); + return StringFunctions.formatterForIterable(format); + } + throw new IllegalStateException("unknown function: " + reducerName); + } + @Override public void onEvent(SensorEvent<Object> event) { - Sensor<T> destinationSensor = (Sensor<T>) getConfig(TARGET_SENSOR); - - List<S> values = Lists.newArrayList(); + Sensor<?> destinationSensor = getConfig(TARGET_SENSOR); - for (AttributeSensor<S> sourceSensor : subscribedSensors) { - S resolvedSensorValue = entity.sensors().get(sourceSensor); - if (resolvedSensorValue == null) { - // only apply function if all values are resolved - return; - } + List<Object> values = Lists.newArrayList(); + for (AttributeSensor<?> sourceSensor : subscribedSensors) { + Object resolvedSensorValue = entity.sensors().get(sourceSensor); values.add(resolvedSensorValue); } - Object result = reducerFunction.apply(values); if (LOG.isTraceEnabled()) LOG.trace("enricher {} got {}, propagating via {} as {}", new Object[] {this, event, entity, reducerFunction, destinationSensor}); - emit((Sensor<T>)destinationSensor, result); - } - - public static class JoinerReducerFunction<A> implements Function<List<A>, String> { - - private Object separator; - - public JoinerReducerFunction(Object separator) { - this.separator = (separator == null) ? ", " : separator; - } - - @Override - public String apply(List<A> input) { - - StringBuilder sb = new StringBuilder(); - Iterator<A> it = input.iterator(); - while(it.hasNext()) { - sb.append(it.next().toString()); - if(it.hasNext()){ - sb.append(separator); - } - } - return sb.toString(); - } - - } - - public static class JoinerFunction extends JoinerReducerFunction<String>{ - - public JoinerFunction(Object separator) { - super(separator); - } - } - - public static class ToStringReducerFunction<A> implements Function<List<A>, String> { - - @Override - public String apply(List<A> input) { - return input.toString(); - } - - } - - public static class FormatStringReducerFunction<T> implements Function<List<T>, String> { - - private String format; - - public FormatStringReducerFunction(String format) { - this.format = Preconditions.checkNotNull(format, "format"); - } - - @Override - public String apply(List<T> input) { - return String.format(format, input.toArray()); - } - + emit(destinationSensor, result); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 deleted file mode 100644 index 3180240..0000000 --- a/core/src/main/java/org/apache/brooklyn/enricher/stock/reducer/StringStringReducer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.enricher.stock.reducer; - -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import com.google.common.base.Function; - -public class StringStringReducer extends GenericStringReducer<String> { - - public StringStringReducer() {} - - @Override - protected Function<List<String>, String> createReducerFunction( - String reducerName, Map<String, ?> parameters) { - Function<List<String>, String> function = super.createReducerFunction(reducerName, parameters); - if(function != null) return function; - - if(Objects.equals(reducerName, "joiner")){ - return new JoinerFunction(parameters.get("separator")); - } - throw new IllegalStateException("unknown function: " + reducerName); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 index 5106557..f96eca9 100644 --- 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 @@ -18,6 +18,8 @@ */ package org.apache.brooklyn.util.core.sensor; +import static com.google.common.base.Preconditions.checkNotNull; + import org.apache.brooklyn.api.sensor.Sensor; import com.google.common.base.Predicate; @@ -28,13 +30,13 @@ public class SensorPredicates { // not instantiable } - public static Predicate<Sensor<?>> sensorNameEqualTo(String sensorName) { - return new SensorNameEquals(sensorName); + public static Predicate<Sensor<?>> nameEqualTo(String sensorName) { + return new SensorNameEquals(checkNotNull(sensorName, "sensorName")); } private static class SensorNameEquals implements Predicate<Sensor<?>> { - private String sensor; + private final String sensor; public SensorNameEquals(String sensor) { this.sensor = sensor; http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/core/src/test/java/org/apache/brooklyn/enricher/stock/reducer/ReducerTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/enricher/stock/reducer/ReducerTest.java b/core/src/test/java/org/apache/brooklyn/enricher/stock/reducer/ReducerTest.java index a951d40..d14f78c 100644 --- a/core/src/test/java/org/apache/brooklyn/enricher/stock/reducer/ReducerTest.java +++ b/core/src/test/java/org/apache/brooklyn/enricher/stock/reducer/ReducerTest.java @@ -19,7 +19,6 @@ package org.apache.brooklyn.enricher.stock.reducer; import java.util.List; -import java.util.Map; import javax.annotation.Nullable; @@ -30,8 +29,6 @@ import org.apache.brooklyn.core.sensor.Sensors; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.core.test.entity.TestEntity; import org.apache.brooklyn.enricher.stock.Enrichers; -import org.apache.brooklyn.enricher.stock.reducer.Reducer; -import org.apache.brooklyn.enricher.stock.reducer.StringStringReducer; import org.apache.brooklyn.test.Asserts; import org.apache.brooklyn.test.EntityTestUtils; import org.apache.brooklyn.util.collections.MutableMap; @@ -62,7 +59,7 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testBasicReducer(){ - entity.addEnricher(EnricherSpec.create(StringStringReducer.class).configure( + entity.enrichers().add(EnricherSpec.create(Reducer.class).configure( MutableMap.of( Reducer.SOURCE_SENSORS, ImmutableList.of(STR1, STR2), Reducer.PRODUCER, entity, @@ -82,8 +79,8 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithConcatenator() { - entity.addEnricher(Enrichers.builder() - .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing(new Concatenator()) .publishing(STR3) @@ -101,8 +98,8 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithLengthCalculator() { - entity.addEnricher(Enrichers.builder() - .reducing(StringIntegerReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing(new LengthCalculator()) .publishing(INT1) @@ -112,7 +109,7 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { EntityTestUtils.assertAttributeEquals(entity, INT1, null); entity.sensors().set(STR1, "foo"); - EntityTestUtils.assertAttributeEqualsContinually(entity, INT1, null); + EntityTestUtils.assertAttributeEqualsEventually(entity, INT1, 3); entity.sensors().set(STR2, "bar"); EntityTestUtils.assertAttributeEqualsEventually(entity, INT1, 6); @@ -120,8 +117,8 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithJoinerFunction() { - entity.addEnricher(Enrichers.builder() - .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("joiner", ImmutableMap.<String, Object>of("separator", "-")) .publishing(STR3) @@ -131,7 +128,7 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { EntityTestUtils.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); - EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo-null"); entity.sensors().set(STR2, "bar"); EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo-bar"); @@ -139,8 +136,8 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithJoinerFunctionWithDefaultParameter() { - entity.addEnricher(Enrichers.builder() - .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("joiner") .publishing(STR3) @@ -149,7 +146,7 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { EntityTestUtils.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); - EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo, null"); entity.sensors().set(STR2, "bar"); EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo, bar"); @@ -158,8 +155,8 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithJoinerFunctionAndUnusedParameter() { - entity.addEnricher(Enrichers.builder() - .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("joiner", ImmutableMap.<String, Object>of("non.existent.parameter", "-")) .publishing(STR3) @@ -168,7 +165,7 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { EntityTestUtils.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); - EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo, null"); entity.sensors().set(STR2, "bar"); EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo, bar"); @@ -176,18 +173,18 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithFormatStringFunction() { - - entity.addEnricher(Enrichers.builder() - .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("formatString", ImmutableMap.<String, Object>of("format", "hello, %s and %s")) .publishing(STR3) .build() ); + EntityTestUtils.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); - EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "hello, foo and null"); entity.sensors().set(STR2, "bar"); EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "hello, foo and bar"); @@ -196,8 +193,8 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { @Test public void testReducingBuilderWithNamedNonExistentFunction() { try { - entity.addEnricher(Enrichers.builder() - .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + entity.enrichers().add(Enrichers.builder() + .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing("unknown function name", ImmutableMap.<String, Object>of("separator", "-")) .publishing(STR3) @@ -226,18 +223,6 @@ public class ReducerTest extends BrooklynAppUnitTestSupport { } } - public static class StringIntegerReducer extends Reducer<String, Integer> { - - public StringIntegerReducer() {} - - @Override - protected Function<List<String>, Integer> createReducerFunction( - String reducerName, Map<String, ?> parameters) { - throw new IllegalStateException("unknown function: " + reducerName); - } - - } - private static class LengthCalculator implements Function<List<String>, Integer>{ @Override http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/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 index d9cd186..4e3c0f2 100644 --- 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 @@ -31,8 +31,8 @@ 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)); + assertTrue(SensorPredicates.nameEqualTo("myname").apply(task)); + assertFalse(SensorPredicates.nameEqualTo("wrong").apply(task)); } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0f59c13f/utils/common/src/main/java/org/apache/brooklyn/util/text/StringFunctions.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/text/StringFunctions.java b/utils/common/src/main/java/org/apache/brooklyn/util/text/StringFunctions.java index 3eec640..d9f8fce 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/text/StringFunctions.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/text/StringFunctions.java @@ -18,6 +18,8 @@ */ package org.apache.brooklyn.util.text; +import java.util.List; + import javax.annotation.Nullable; import com.google.common.base.CaseFormat; @@ -25,6 +27,7 @@ import com.google.common.base.CharMatcher; import com.google.common.base.Function; import com.google.common.base.Functions; import com.google.common.base.Preconditions; +import com.google.common.collect.Iterables; public class StringFunctions { @@ -67,6 +70,15 @@ public class StringFunctions { } }; } + + /** given e.g. "hello %s %s" returns a function which will insert an Iterable of two strings into that pattern */ + public static Function<Iterable<?>, String> formatterForIterable(final String pattern) { + return new Function<Iterable<?>, String>() { + public String apply(@Nullable Iterable<?> input) { + return String.format(pattern, Iterables.toArray(input, Object.class)); + } + }; + } /** joins the given objects in a collection as a toString with the given separator */ public static Function<Iterable<?>, String> joiner(final String separator) { @@ -154,4 +166,8 @@ public class StringFunctions { }; } + + + + }
