From dd92288e9708c40d07b298fa32d23eb7ceefb00d Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sat, 24 Oct 2020 10:08:38 +0200 Subject: [PATCH] Added nullness annotations, ctor injection (#1747) Signed-off-by: Christoph Weitkamp --- .../internal/commands/AutomationCommand.java | 3 + .../commands/AutomationCommandsPluggable.java | 146 ++++-------------- 2 files changed, 32 insertions(+), 117 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommand.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommand.java index ed6e353055d..03bc857f9eb 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommand.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommand.java @@ -12,6 +12,8 @@ */ package org.openhab.core.automation.internal.commands; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * This class is base for all automation commands. It defines common functionality for an automation command. Each class * of commands is responsible for a group of commands, that are equivalent but each of them is related to a different @@ -19,6 +21,7 @@ * * @author Ana Dimova - Initial contribution */ +@NonNullByDefault public abstract class AutomationCommand { /** diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java index cd536bb1980..b112e27114a 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandsPluggable.java @@ -14,12 +14,10 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.automation.Rule; @@ -84,96 +82,41 @@ public class AutomationCommandsPluggable extends AutomationCommands implements C /** * This field holds the reference to the {@code RuleRegistry} providing the {@code Rule} automation objects. */ - protected @NonNullByDefault({}) RuleRegistry ruleRegistry; + protected final RuleRegistry ruleRegistry; /** * This field holds the reference to the {@code RuleManager}. */ - protected @NonNullByDefault({}) RuleManager ruleManager; + protected final RuleManager ruleManager; /** * This field holds the reference to the {@code TemplateRegistry} providing the {@code Template} automation objects. */ - protected @NonNullByDefault({}) TemplateRegistry<@NonNull RuleTemplate> templateRegistry; + protected final TemplateRegistry templateRegistry; /** * This field holds the reference to the {@code ModuleTypeRegistry} providing the {@code ModuleType} automation * objects. */ - protected @NonNullByDefault({}) ModuleTypeRegistry moduleTypeRegistry; + protected final ModuleTypeRegistry moduleTypeRegistry; - /** - * Activating this component - called from DS. - * - * @param componentContext - */ @Activate - protected void activate(ComponentContext componentContext) { - super.initialize(componentContext.getBundleContext(), moduleTypeRegistry, templateRegistry, ruleRegistry); - } - - /** - * Deactivating this component - called from DS. - */ - @Deactivate - protected void deactivate(ComponentContext componentContext) { - super.dispose(); - } - - /** - * Bind the {@link RuleRegistry} service - called from DS. - * - * @param ruleRegistry ruleRegistry service. - */ - @Reference - protected void setRuleRegistry(RuleRegistry ruleRegistry) { + public AutomationCommandsPluggable(ComponentContext componentContext, // + final @Reference RuleRegistry ruleRegistry, // + final @Reference ModuleTypeRegistry moduleTypeRegistry, // + final @Reference TemplateRegistry templateRegistry, // + final @Reference RuleManager ruleManager) { this.ruleRegistry = ruleRegistry; - } - - /** - * Bind the {@link ModuleTypeRegistry} service - called from DS. - * - * @param moduleTypeRegistry moduleTypeRegistry service. - */ - @Reference - protected void setModuleTypeRegistry(ModuleTypeRegistry moduleTypeRegistry) { this.moduleTypeRegistry = moduleTypeRegistry; - } - - /** - * Bind the {@link TemplateRegistry} service - called from DS. - * - * @param templateRegistry templateRegistry service. - */ - @Reference - protected void setTemplateRegistry(TemplateRegistry templateRegistry) { this.templateRegistry = templateRegistry; - } - - /** - * Bind the {@link RuleManager} service - called from DS. - * - * @param ruleManager RuleManager service. - */ - @Reference - protected void setRuleManager(RuleManager ruleManager) { this.ruleManager = ruleManager; - } - - protected void unsetRuleRegistry(RuleRegistry ruleRegistry) { - this.ruleRegistry = null; - } - - protected void unsetModuleTypeRegistry(ModuleTypeRegistry moduleTypeRegistry) { - this.moduleTypeRegistry = null; - } - protected void unsetTemplateRegistry(TemplateRegistry templateRegistry) { - this.templateRegistry = null; + initialize(componentContext.getBundleContext(), moduleTypeRegistry, templateRegistry, ruleRegistry); } - protected void unsetRuleManager(RuleManager ruleManager) { - this.ruleManager = null; + @Deactivate + protected void deactivate(ComponentContext componentContext) { + super.dispose(); } @Override @@ -247,86 +190,59 @@ public String getDescription() { @Override public @Nullable Rule getRule(String uid) { - if (ruleRegistry != null) { - return ruleRegistry.get(uid); - } - return null; + return ruleRegistry.get(uid); } @Override public @Nullable RuleTemplate getTemplate(String templateUID, @Nullable Locale locale) { - if (templateRegistry != null) { - return templateRegistry.get(templateUID, locale); - } - return null; + return templateRegistry.get(templateUID, locale); } @Override public Collection getTemplates(@Nullable Locale locale) { - if (templateRegistry != null) { - return templateRegistry.getAll(locale); - } - return Collections.emptyList(); + return templateRegistry.getAll(locale); } @Override public @Nullable ModuleType getModuleType(String typeUID, @Nullable Locale locale) { - if (moduleTypeRegistry != null) { - return moduleTypeRegistry.get(typeUID, locale); - } - return null; + return moduleTypeRegistry.get(typeUID, locale); } @SuppressWarnings("unchecked") @Override public Collection getTriggers(@Nullable Locale locale) { - if (moduleTypeRegistry != null) { - return moduleTypeRegistry.getTriggers(locale); - } - return Collections.emptyList(); + return moduleTypeRegistry.getTriggers(locale); } @SuppressWarnings("unchecked") @Override public Collection getConditions(@Nullable Locale locale) { - if (moduleTypeRegistry != null) { - return moduleTypeRegistry.getConditions(locale); - } - return Collections.emptyList(); + return moduleTypeRegistry.getConditions(locale); } @SuppressWarnings("unchecked") @Override public Collection getActions(@Nullable Locale locale) { - if (moduleTypeRegistry != null) { - return moduleTypeRegistry.getActions(locale); - } - return Collections.emptyList(); + return moduleTypeRegistry.getActions(locale); } @Override public String removeRule(String uid) { - if (ruleRegistry != null) { - if (ruleRegistry.remove(uid) != null) { - return AutomationCommand.SUCCESS; - } else { - return String.format("Rule with id '%s' does not exist.", uid); - } + if (ruleRegistry.remove(uid) != null) { + return AutomationCommand.SUCCESS; + } else { + return String.format("Rule with id '%s' does not exist.", uid); } - return String.format("%s! RuleRegistry not available!", AutomationCommand.FAIL); } @Override public String removeRules(String ruleFilter) { - if (ruleRegistry != null) { - for (Rule r : ruleRegistry.getAll()) { - if (r.getUID().contains(ruleFilter)) { - ruleRegistry.remove(r.getUID()); - } + for (Rule r : ruleRegistry.getAll()) { + if (r.getUID().contains(ruleFilter)) { + ruleRegistry.remove(r.getUID()); } - return AutomationCommand.SUCCESS; } - return String.format("%s! RuleRegistry not available!", AutomationCommand.FAIL); + return AutomationCommand.SUCCESS; } @Override @@ -408,11 +324,7 @@ protected String buildCommandUsage(final String syntax, final String description @Override public Collection getRules() { - if (ruleRegistry != null) { - return ruleRegistry.getAll(); - } else { - return Collections.emptyList(); - } + return ruleRegistry.getAll(); } @Override