From db976101116804d65df232f283827f4b78ade096 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sun, 25 Jun 2023 17:24:46 +0200 Subject: [PATCH] Replace StringBuffer usages with StringBuilder (#3668) See: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StringBuilder.html > This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. > This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). > Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations. Signed-off-by: Wouter Born --- .../automation/internal/RuleEngineImpl.java | 23 ++++++++----------- .../automation/internal/RuleRegistryImpl.java | 6 ++--- .../core/io/console/ConsoleInterpreter.java | 2 +- .../binding/internal/MagicServiceConfig.java | 2 +- ...FirmwareUpdateConsoleCommandExtension.java | 2 +- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleEngineImpl.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleEngineImpl.java index 1585d0be8..9d3adc6e1 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleEngineImpl.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleEngineImpl.java @@ -266,7 +266,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener() { + listener = new RegistryChangeListener<>() { @Override public void added(Rule rule) { RuleEngineImpl.this.addRule(rule); @@ -337,8 +337,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener rulesPerModule = mapModuleTypeToRules.get(moduleTypeName); if (rulesPerModule != null) { - rules = new HashSet<>(); - rules.addAll(rulesPerModule); + rules = new HashSet<>(rulesPerModule); } } if (rules != null) { @@ -366,8 +365,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener rulesPerModule = mapModuleTypeToRules.get(moduleTypeName); if (rulesPerModule != null) { - rules = new HashSet<>(); - rules.addAll(rulesPerModule); + rules = new HashSet<>(rulesPerModule); } } if (rules != null) { @@ -402,8 +400,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener rulesPerModule = mapModuleTypeToRules.get(moduleTypeName); if (rulesPerModule != null) { - rules = new HashSet<>(); - rules.addAll(rulesPerModule); + rules = new HashSet<>(rulesPerModule); } } if (rules != null) { @@ -505,10 +502,8 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener f = scheduleTasks.remove(rUID); - if (f != null) { - if (!f.isDone()) { - f.cancel(true); - } + if ((f != null) && !f.isDone()) { + f.cancel(true); } } } @@ -942,7 +937,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener> e : mapMissingHandlers.entrySet()) { String rUID = e.getKey(); List missingTypes = e.getValue(); - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append("Missing handlers: "); for (String typeUID : missingTypes) { sb.append(typeUID).append(", "); @@ -1097,7 +1092,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener { ruleRegistry.getAll().stream() // - .filter(r -> mustTrigger(r)) // + .filter(this::mustTrigger) // .forEach(r -> runNow(r.getUID(), true, Map.of(SystemTriggerHandler.OUT_STARTLEVEL, StartLevelService.STARTLEVEL_RULES))); started = true; diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java index c7c597182..f50a9f5d0 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/RuleRegistryImpl.java @@ -500,7 +500,7 @@ public class RuleRegistryImpl extends AbstractRegistry modules, Map ruleConfiguration) { if (modules != null) { - StringBuffer statusDescription = new StringBuffer(); + StringBuilder statusDescription = new StringBuilder(); for (Module module : modules) { try { ReferenceResolver.updateConfiguration(module.getConfiguration(), ruleConfiguration, logger); diff --git a/bundles/org.openhab.core.io.console/src/main/java/org/openhab/core/io/console/ConsoleInterpreter.java b/bundles/org.openhab.core.io.console/src/main/java/org/openhab/core/io/console/ConsoleInterpreter.java index d4f94db62..01c8d076e 100644 --- a/bundles/org.openhab.core.io.console/src/main/java/org/openhab/core/io/console/ConsoleInterpreter.java +++ b/bundles/org.openhab.core.io.console/src/main/java/org/openhab/core/io/console/ConsoleInterpreter.java @@ -33,7 +33,7 @@ public class ConsoleInterpreter { public static String getHelp(final String base, final String separator, Collection extensions) { final List usages = ConsoleInterpreter.getUsages(extensions); - final StringBuffer buffer = new StringBuffer(); + final StringBuilder buffer = new StringBuilder(); buffer.append("---openHAB commands---\n\t"); for (int i = 0; i < usages.size(); i++) { diff --git a/bundles/org.openhab.core.test.magic/src/main/java/org/openhab/core/magic/binding/internal/MagicServiceConfig.java b/bundles/org.openhab.core.test.magic/src/main/java/org/openhab/core/magic/binding/internal/MagicServiceConfig.java index ce8895e07..1e46d4bf3 100644 --- a/bundles/org.openhab.core.test.magic/src/main/java/org/openhab/core/magic/binding/internal/MagicServiceConfig.java +++ b/bundles/org.openhab.core.test.magic/src/main/java/org/openhab/core/magic/binding/internal/MagicServiceConfig.java @@ -46,7 +46,7 @@ public class MagicServiceConfig { @Override public String toString() { - StringBuffer b = new StringBuffer(); + StringBuilder b = new StringBuilder(); for (Field field : this.getClass().getDeclaredFields()) { Object value; try { diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/console/FirmwareUpdateConsoleCommandExtension.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/console/FirmwareUpdateConsoleCommandExtension.java index 3e91ea4b9..b102afba5 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/console/FirmwareUpdateConsoleCommandExtension.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/console/FirmwareUpdateConsoleCommandExtension.java @@ -132,7 +132,7 @@ public final class FirmwareUpdateConsoleCommandExtension extends AbstractConsole FirmwareStatusInfo firmwareStatusInfo = firmwareUpdateService.getFirmwareStatusInfo(thingUID); if (firmwareStatusInfo != null) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append(String.format("Firmware status for thing with UID %s is %s.", thingUID, firmwareStatusInfo.getFirmwareStatus()));