diff --git a/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java b/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java index 6c79729db..ad7b08497 100644 --- a/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java +++ b/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java @@ -123,8 +123,8 @@ public class JRubyScriptEngineFactory extends AbstractScriptEngineFactory { importClassesToRuby(scriptEngine, partitionedMap.getOrDefault(true, new HashMap<>())); Object scriptExtension = scopeValues.get("scriptExtension"); - if (scriptExtension instanceof ScriptExtensionManagerWrapper && configuration.enableDependencyTracking()) { - ScriptExtensionManagerWrapper wrapper = (ScriptExtensionManagerWrapper) scriptExtension; + if (scriptExtension instanceof ScriptExtensionManagerWrapper wrapper + && configuration.enableDependencyTracking()) { // we inject like this instead of using the script context, because // this is executed _before_ the dependency tracker is added to the script // context. diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java index 5efdc8199..432a3a2c2 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java @@ -30,7 +30,6 @@ import java.nio.file.attribute.FileAttribute; import java.time.Duration; import java.time.Instant; import java.time.ZonedDateTime; -import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.concurrent.locks.Lock; @@ -209,7 +208,7 @@ public class OpenhabGraalJSScriptEngine public Map readAttributes(Path path, String attributes, LinkOption... options) throws IOException { if (isRootNodePath(path)) { - return Collections.singletonMap("isRegularFile", true); + return Map.of("isRegularFile", true); } return super.readAttributes(path, attributes, options); } diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/ScriptExtensionModuleProvider.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/ScriptExtensionModuleProvider.java index 040bc98c4..e22552e44 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/ScriptExtensionModuleProvider.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/ScriptExtensionModuleProvider.java @@ -78,10 +78,15 @@ public class ScriptExtensionModuleProvider { private Value toValue(Context ctx, Map map) { try { return ctx.eval(Source.newBuilder( // convert to Map to JS Object - "js", - "(function (mapOfValues) {\n" + "let rv = {};\n" + "for (var key in mapOfValues) {\n" - + " rv[key] = mapOfValues.get(key);\n" + "}\n" + "return rv;\n" + "})", - "").build()).execute(map); + "js", """ + (function (mapOfValues) { + let rv = {}; + for (var key in mapOfValues) { + rv[key] = mapOfValues.get(key); + } + return rv; + })\ + """, "").build()).execute(map); } catch (IOException e) { throw new IllegalArgumentException("Failed to generate exports", e); } diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/AbstractScriptExtensionProvider.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/AbstractScriptExtensionProvider.java index 89d12fb8a..1494f10f2 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/AbstractScriptExtensionProvider.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/AbstractScriptExtensionProvider.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -56,7 +57,7 @@ public abstract class AbstractScriptExtensionProvider implements ScriptExtension @Override public Collection getPresets() { - return Collections.singleton(getPresetName()); + return Set.of(getPresetName()); } @Override diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/ScriptDisposalAwareScriptExtensionProvider.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/ScriptDisposalAwareScriptExtensionProvider.java index 1f3a02c27..9f73e3916 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/ScriptDisposalAwareScriptExtensionProvider.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/ScriptDisposalAwareScriptExtensionProvider.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -57,7 +58,7 @@ public abstract class ScriptDisposalAwareScriptExtensionProvider @Override public Collection getPresets() { - return Collections.singleton(getPresetName()); + return Set.of(getPresetName()); } @Override @@ -92,8 +93,8 @@ public abstract class ScriptDisposalAwareScriptExtensionProvider if (forScript != null) { for (Object o : forScript.values()) { - if (o instanceof ScriptDisposalAware) { - ((ScriptDisposalAware) o).unload(scriptIdentifier); + if (o instanceof ScriptDisposalAware script) { + script.unload(scriptIdentifier); } } } diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeWrappingScriptedAutomationManagerDelegate.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeWrappingScriptedAutomationManagerDelegate.java index 3d23ae082..e8be9de31 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeWrappingScriptedAutomationManagerDelegate.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeWrappingScriptedAutomationManagerDelegate.java @@ -65,8 +65,8 @@ public class ThreadsafeWrappingScriptedAutomationManagerDelegate { public Rule addRule(Rule element) { // wrap in a threadsafe version, safe per context - if (element instanceof SimpleRule) { - element = new ThreadsafeSimpleRuleDelegate(lock, (SimpleRule) element); + if (element instanceof SimpleRule rule) { + element = new ThreadsafeSimpleRuleDelegate(lock, rule); } return delegate.addRule(element); diff --git a/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java b/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java index 6c90ac261..d36fe8c13 100644 --- a/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java +++ b/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java @@ -206,8 +206,8 @@ public class PIDControllerTriggerHandler extends BaseTriggerModuleHandler implem private TriggerHandlerCallback getCallback() { ModuleHandlerCallback localCallback = callback; - if (localCallback != null && localCallback instanceof TriggerHandlerCallback) { - return (TriggerHandlerCallback) localCallback; + if (localCallback != null && localCallback instanceof TriggerHandlerCallback handlerCallback) { + return handlerCallback; } throw new IllegalStateException("The module callback is not set"); @@ -236,8 +236,8 @@ public class PIDControllerTriggerHandler extends BaseTriggerModuleHandler implem private double getItemValueAsNumber(Item item) throws PIDException { State setpointState = item.getState(); - if (setpointState instanceof Number) { - double doubleValue = ((Number) setpointState).doubleValue(); + if (setpointState instanceof Number number) { + double doubleValue = number.doubleValue(); if (Double.isFinite(doubleValue) && !Double.isNaN(doubleValue)) { return doubleValue; @@ -254,9 +254,8 @@ public class PIDControllerTriggerHandler extends BaseTriggerModuleHandler implem @Override public void receive(Event event) { - if (event instanceof ItemStateChangedEvent) { + if (event instanceof ItemStateChangedEvent changedEvent) { if (commandTopic.isPresent() && event.getTopic().equals(commandTopic.get())) { - ItemStateChangedEvent changedEvent = (ItemStateChangedEvent) event; if ("RESET".equals(changedEvent.getItemState().toString())) { controller.setIntegralResult(0); controller.setDerivativeResult(0); diff --git a/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/handler/PWMTriggerHandler.java b/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/handler/PWMTriggerHandler.java index 335d9641d..2e532910e 100644 --- a/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/handler/PWMTriggerHandler.java +++ b/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/handler/PWMTriggerHandler.java @@ -15,7 +15,7 @@ package org.openhab.automation.pwm.internal.handler; import static org.openhab.automation.pwm.internal.PWMConstants.*; import java.math.BigDecimal; -import java.util.Collections; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -115,8 +115,8 @@ public class PWMTriggerHandler extends BaseTriggerModuleHandler implements Event private Optional getOptionalDoubleFromConfig(Configuration config, String key) { Object o = config.get(key); - if (o instanceof BigDecimal) { - return Optional.of(((BigDecimal) o).doubleValue()); + if (o instanceof BigDecimal decimal) { + return Optional.of(decimal.doubleValue()); } return Optional.empty(); @@ -202,21 +202,21 @@ public class PWMTriggerHandler extends BaseTriggerModuleHandler implements Event } private void setOutput(boolean enable) { - getCallback().triggered(module, Collections.singletonMap(OUTPUT, OnOffType.from(enable))); + getCallback().triggered(module, Map.of(OUTPUT, OnOffType.from(enable))); } private TriggerHandlerCallback getCallback() { ModuleHandlerCallback localCallback = callback; - if (localCallback != null && localCallback instanceof TriggerHandlerCallback) { - return (TriggerHandlerCallback) localCallback; + if (localCallback != null && localCallback instanceof TriggerHandlerCallback handlerCallback) { + return handlerCallback; } throw new IllegalStateException(); } private double getDutyCycleValueInPercent(State state) throws PWMException { - if (state instanceof DecimalType) { - return ((DecimalType) state).doubleValue(); + if (state instanceof DecimalType decimal) { + return decimal.doubleValue(); } else if (state instanceof StringType) { try { return Integer.parseInt(state.toString()); diff --git a/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/template/PWMRuleTemplate.java b/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/template/PWMRuleTemplate.java index c68772c21..78ebbb472 100644 --- a/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/template/PWMRuleTemplate.java +++ b/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/template/PWMRuleTemplate.java @@ -43,7 +43,7 @@ public class PWMRuleTemplate extends RuleTemplate { public static PWMRuleTemplate initialize() { final String triggerId = UUID.randomUUID().toString(); - final List triggers = Collections.singletonList(ModuleBuilder.createTrigger().withId(triggerId) + final List triggers = List.of(ModuleBuilder.createTrigger().withId(triggerId) .withTypeUID(PWMTriggerType.UID).withLabel("PWM Trigger").build()); final Map actionInputs = new HashMap(); diff --git a/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/type/PWMTriggerType.java b/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/type/PWMTriggerType.java index 73df39b31..b02c229bb 100644 --- a/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/type/PWMTriggerType.java +++ b/bundles/org.openhab.automation.pwm/src/main/java/org/openhab/automation/pwm/internal/type/PWMTriggerType.java @@ -16,7 +16,6 @@ import static org.openhab.automation.pwm.internal.PWMConstants.*; import java.math.BigDecimal; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Set; @@ -97,7 +96,7 @@ public class PWMTriggerType extends TriggerType { "If the duty cycle Item is not updated within this time (in ms), the output is switched off") .build()); - List outputs = Collections.singletonList(new Output(OUTPUT, OnOffType.class.getName(), "Output", + List outputs = List.of(new Output(OUTPUT, OnOffType.class.getName(), "Output", "Output value of the PWM module", Set.of("command"), null, null)); return new PWMTriggerType(configDescriptions, outputs);