Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/386#discussion_r85703704
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSensorChange.java 
---
    @@ -40,28 +44,56 @@
      * * support conditions
      * * allow to be triggered by sensors on members
      */
    -public class InvokeEffectorOnSensorChange extends AbstractPolicy 
implements SensorEventListener<Object> {
    +public class InvokeEffectorOnSensorChange extends 
AbstractInvokeEffectorPolicy implements SensorEventListener<Object> {
         
         private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSensorChange.class);
     
    -    public static final ConfigKey<Object> SENSOR = 
ConfigKeys.newConfigKey(Object.class, 
    -            "sensor", "Sensor to be monitored, as string or sensor type");
    +    public static final ConfigKey<Object> SENSOR = 
ConfigKeys.builder(Object.class)
    +            .name("sensor")
    +            .description("Sensor to be monitored, as string or sensor 
type")
    +            .constraint(Predicates.notNull())
    +            .build();
     
    -    public static final ConfigKey<String> EFFECTOR = 
ConfigKeys.newStringConfigKey(
    -            "effector", "Name of effector to invoke");
    +    public static final ConfigKey<String> EFFECTOR = 
ConfigKeys.builder(String.class)
    +            .name("effector")
    +            .description("Name of effector to invoke")
    +            .constraint(StringPredicates.isNonBlank())
    +            .build();
    +
    +    private AttributeSensor<Object> sensor;
     
         @Override
         public void setEntity(EntityLocal entity) {
             super.setEntity(entity);
             Preconditions.checkNotNull(getConfig(EFFECTOR), EFFECTOR);
    -        Object sensor = Preconditions.checkNotNull(getConfig(SENSOR), 
SENSOR);
    -        if (sensor instanceof String) sensor = 
Sensors.newSensor(Object.class, (String)sensor);
    -        subscriptions().subscribe(entity, (Sensor<?>)sensor, this);
    +        sensor = getSensor();
    +        subscriptions().subscribe(entity, sensor, this);
    +        LOG.debug("{} subscribed to {} events on {}", new Object[]{this, 
sensor, entity});
         }
     
         @Override
         public void onEvent(SensorEvent<Object> event) {
    -        
entity.invoke(entity.getEntityType().getEffectorByName(getConfig(EFFECTOR)).get(),
 MutableMap.<String, Object>of());
    +        final Effector<?> eff = 
getEffectorNamed(getConfig(EFFECTOR)).get();
    +        if (isBusySensorEnabled()) {
    +            final Object currentSensorValue = entity.sensors().get(sensor);
    +            setMoreUpdatesComing(event.getTimestamp(), event.getValue(), 
currentSensorValue);
    +        }
    +        invoke(eff, MutableMap.<String, Object>of());
    +    }
    +
    +    private AttributeSensor<Object> getSensor() {
    +        final Object configVal = 
Preconditions.checkNotNull(getConfig(SENSOR), SENSOR);
    +        final AttributeSensor<Object> sensor;
    +        if (configVal == null) {
    +            throw new NullPointerException("Value for " + SENSOR.getName() 
+ " is null");
    +        } else if (configVal instanceof String) {
    +            sensor = Sensors.newSensor(Object.class, (String) configVal);
    +        } else if (configVal instanceof AttributeSensor) {
    +            sensor = (AttributeSensor<Object>) configVal;
    +        } else {
    +            sensor = TypeCoercions.tryCoerce(configVal, new 
TypeToken<AttributeSensor<Object>>() {}).get();
    +        }
    --- End diff --
    
    No need for `if`s, could replace it all with `coerce`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to