Added nullness anotations to ChannelEventTriggerHandler (#2586)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/2589/head
Christoph Weitkamp 2021-12-03 14:32:05 +01:00 committed by GitHub
parent a9504bfced
commit 07ee8e6ffc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 44 deletions

View File

@ -15,6 +15,8 @@ package org.openhab.core.automation.handler;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.ModuleHandlerCallback;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.Trigger;
@ -30,6 +32,7 @@ import org.openhab.core.automation.type.Output;
* @author Kai Kreuzer - made it a sub-interface of ModuleHandlerCallback
* @author Fabian Wolter - Add method for retrieving the handler's scheduler
*/
@NonNullByDefault
public interface TriggerHandlerCallback extends ModuleHandlerCallback {
/**
@ -46,10 +49,10 @@ public interface TriggerHandlerCallback extends ModuleHandlerCallback {
* <li><code>value</code> - represents output value of the {@link Trigger}'s {@link Output}
* </ul>
*/
public void triggered(Trigger trigger, Map<String, ?> context);
public void triggered(Trigger trigger, @Nullable Map<String, ?> context);
/**
* @return the scheduler of this rule
*/
public ScheduledExecutorService getScheduler();
public @Nullable ScheduledExecutorService getScheduler();
}

View File

@ -143,7 +143,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
* {@link Map} holding all available {@link ModuleHandlerFactory}s linked with {@link ModuleType}s that they
* supporting. The relation is {@link ModuleType}'s UID to {@link ModuleHandlerFactory} instance.
*/
private final Map<String, ModuleHandlerFactory> moduleHandlerFactories;
private final Map<String, ModuleHandlerFactory> moduleHandlerFactories = new HashMap<>(20);
/**
* {@link Set} holding all available {@link ModuleHandlerFactory}s.
@ -176,7 +176,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
* The context map of a {@link Rule} is cleaned when the execution is completed. The relation is
* {@link Rule}'s UID to Rule context map.
*/
private final Map<String, Map<String, Object>> contextMap;
private final Map<String, Map<String, Object>> contextMap = new HashMap<>();
/**
* This field holds reference to {@link ModuleTypeRegistry}. The {@link RuleEngineImpl} needs it to auto-map
@ -254,9 +254,6 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
public RuleEngineImpl(final @Reference ModuleTypeRegistry moduleTypeRegistry,
final @Reference RuleRegistry ruleRegistry, final @Reference StorageService storageService,
final @Reference ReadyService readyService) {
this.contextMap = new HashMap<>();
this.moduleHandlerFactories = new HashMap<>(20);
this.disabledRulesStorage = storageService.<Boolean> getStorage(DISABLED_RULE_STORAGE,
this.getClass().getClassLoader());

View File

@ -19,6 +19,9 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.RuleStatus;
import org.openhab.core.automation.RuleStatusInfo;
import org.openhab.core.automation.Trigger;
@ -51,12 +54,12 @@ public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
}
@Override
public void triggered(Trigger trigger, Map<String, ?> outputs) {
public void triggered(Trigger trigger, @Nullable Map<@NonNull String, ?> context) {
synchronized (this) {
if (executor == null) {
return;
}
future = executor.submit(new TriggerData(trigger, outputs));
future = executor.submit(new TriggerData(trigger, context));
}
re.logger.debug("The trigger '{}' of rule '{}' is triggered.", trigger.getId(), ruleUID);
}
@ -66,21 +69,21 @@ public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
return future == null || !future.isDone();
}
@NonNullByDefault
class TriggerData implements Runnable {
private final Trigger trigger;
private @Nullable final Map<String, ?> outputs;
public Trigger getTrigger() {
return trigger;
}
public Map<String, ?> getOutputs() {
public @Nullable Map<String, ?> getOutputs() {
return outputs;
}
private final Map<String, ?> outputs;
public TriggerData(Trigger t, Map<String, ?> outputs) {
public TriggerData(Trigger t, @Nullable Map<String, ?> outputs) {
this.trigger = t;
this.outputs = outputs;
}

View File

@ -13,12 +13,13 @@
package org.openhab.core.automation.internal.module.handler;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.ModuleHandlerCallback;
import org.openhab.core.automation.Trigger;
import org.openhab.core.automation.handler.BaseTriggerModuleHandler;
import org.openhab.core.automation.handler.TriggerHandlerCallback;
@ -37,6 +38,7 @@ import org.slf4j.LoggerFactory;
*
* @author Stefan Triller - Initial contribution
*/
@NonNullByDefault
public class ChannelEventTriggerHandler extends BaseTriggerModuleHandler implements EventSubscriber, EventFilter {
public static final String MODULE_TYPE_ID = "core.ChannelEventTrigger";
@ -47,21 +49,19 @@ public class ChannelEventTriggerHandler extends BaseTriggerModuleHandler impleme
private final Logger logger = LoggerFactory.getLogger(ChannelEventTriggerHandler.class);
private final String eventOnChannel;
private @Nullable final String eventOnChannel;
private final ChannelUID channelUID;
private final Set<String> types = new HashSet<>();
private final Set<String> types;
private final BundleContext bundleContext;
@SuppressWarnings("rawtypes")
private ServiceRegistration eventSubscriberRegistration;
private final ServiceRegistration<?> eventSubscriberRegistration;
public ChannelEventTriggerHandler(Trigger module, BundleContext bundleContext) {
super(module);
this.eventOnChannel = (String) module.getConfiguration().get(CFG_CHANNEL_EVENT);
this.channelUID = new ChannelUID((String) module.getConfiguration().get(CFG_CHANNEL));
this.types = Set.of("ChannelTriggeredEvent");
this.bundleContext = bundleContext;
this.types.add("ChannelTriggeredEvent");
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("event.topics", TOPIC);
@ -71,37 +71,31 @@ public class ChannelEventTriggerHandler extends BaseTriggerModuleHandler impleme
@Override
public void receive(Event event) {
if (callback != null) {
ModuleHandlerCallback localCallback = callback;
if (localCallback != null) {
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(),
event.getTopic(), event.getType(), event.getPayload());
Map<String, Object> values = new HashMap<>();
values.put("event", event);
((TriggerHandlerCallback) callback).triggered(this.module, values);
((TriggerHandlerCallback) localCallback).triggered(module, Map.of("event", event));
}
}
@Override
public boolean apply(Event event) {
logger.trace("->FILTER: {}:{}", event.getTopic(), TOPIC);
boolean eventMatches = false;
if (event instanceof ChannelTriggeredEvent) {
ChannelTriggeredEvent cte = (ChannelTriggeredEvent) event;
if (cte.getChannel().equals(channelUID)) {
if (channelUID.equals(cte.getChannel())) {
logger.trace("->FILTER: {}:{}", cte.getEvent(), eventOnChannel);
eventMatches = true;
if (eventOnChannel != null && !eventOnChannel.isEmpty() && !eventOnChannel.equals(cte.getEvent())) {
eventMatches = false;
}
eventMatches = eventOnChannel == null || eventOnChannel.isBlank()
|| eventOnChannel.equals(cte.getEvent());
}
}
return eventMatches;
}
@Override
public EventFilter getEventFilter() {
public @Nullable EventFilter getEventFilter() {
return this;
}
@ -110,15 +104,9 @@ public class ChannelEventTriggerHandler extends BaseTriggerModuleHandler impleme
return types;
}
/**
* do the cleanup: unregistering eventSubscriber...
*/
@Override
public void dispose() {
super.dispose();
if (eventSubscriberRegistration != null) {
eventSubscriberRegistration.unregister();
eventSubscriberRegistration = null;
}
eventSubscriberRegistration.unregister();
}
}

View File

@ -44,7 +44,7 @@ public class TimeOfDayTriggerHandler extends BaseTriggerModuleHandler
private final CronScheduler scheduler;
private final String expression;
private ScheduledCompletableFuture<Void> schedule;
private ScheduledCompletableFuture<?> schedule;
public TimeOfDayTriggerHandler(Trigger module, CronScheduler scheduler) {
super(module);
@ -79,7 +79,11 @@ public class TimeOfDayTriggerHandler extends BaseTriggerModuleHandler
@Override
public void run() {
((TriggerHandlerCallback) callback).triggered(module, null);
if (callback != null) {
((TriggerHandlerCallback) callback).triggered(module, null);
} else {
logger.debug("Tried to trigger, but callback isn't available!");
}
}
@Override

View File

@ -12,6 +12,8 @@
*/
package org.openhab.core.scheduler;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Runnable that can throw checked exceptions.
*
@ -19,11 +21,12 @@ package org.openhab.core.scheduler;
* @author Simon Kaufmann - adapted to CompletableFutures
* @author Hilbrand Bouwkamp - moved to it's own class and renamed
*/
@NonNullByDefault
public interface SchedulerRunnable {
/**
* Scheduled job to run.
*
*
* @throws Exception exception thrown
*/
void run() throws Exception;