Skip to content

Commit

Permalink
Replace StringBuffer usages with StringBuilder (#3668)
Browse files Browse the repository at this point in the history
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 <github@maindrain.net>
  • Loading branch information
wborn committed Jun 25, 2023
1 parent 78e6674 commit db97610
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
Expand Up @@ -266,7 +266,7 @@ public RuleEngineImpl(final @Reference ModuleTypeRegistry moduleTypeRegistry,
this.ruleRegistry = ruleRegistry;
this.readyService = readyService;

listener = new RegistryChangeListener<Rule>() {
listener = new RegistryChangeListener<>() {
@Override
public void added(Rule rule) {
RuleEngineImpl.this.addRule(rule);
Expand Down Expand Up @@ -337,8 +337,7 @@ public void added(ModuleType moduleType) {
synchronized (this) {
Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
if (rulesPerModule != null) {
rules = new HashSet<>();
rules.addAll(rulesPerModule);
rules = new HashSet<>(rulesPerModule);
}
}
if (rules != null) {
Expand Down Expand Up @@ -366,8 +365,7 @@ public void updated(ModuleType oldElement, ModuleType moduleType) {
synchronized (this) {
Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
if (rulesPerModule != null) {
rules = new HashSet<>();
rules.addAll(rulesPerModule);
rules = new HashSet<>(rulesPerModule);
}
}
if (rules != null) {
Expand Down Expand Up @@ -402,8 +400,7 @@ protected void addModuleHandlerFactory(ModuleHandlerFactory moduleHandlerFactory
moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory);
Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
if (rulesPerModule != null) {
rules = new HashSet<>();
rules.addAll(rulesPerModule);
rules = new HashSet<>(rulesPerModule);
}
}
if (rules != null) {
Expand Down Expand Up @@ -505,10 +502,8 @@ private void setRule(WrappedRule rule) {
final boolean activated = activateRule(rule);
if (activated) {
Future<?> f = scheduleTasks.remove(rUID);
if (f != null) {
if (!f.isDone()) {
f.cancel(true);
}
if ((f != null) && !f.isDone()) {
f.cancel(true);
}
}
}
Expand Down Expand Up @@ -942,7 +937,7 @@ private void removeMissingModuleTypes(Collection<String> moduleTypes) {
for (Entry<String, List<String>> e : mapMissingHandlers.entrySet()) {
String rUID = e.getKey();
List<String> missingTypes = e.getValue();
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("Missing handlers: ");
for (String typeUID : missingTypes) {
sb.append(typeUID).append(", ");
Expand Down Expand Up @@ -1097,7 +1092,7 @@ private Map<String, Object> getContext(String ruleUID, @Nullable Set<Connection>
throw new IllegalStateException("context cannot be null at that point - please report a bug.");
}
if (connections != null) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (Connection c : connections) {
String outputModuleId = c.getOutputModuleId();
if (outputModuleId != null) {
Expand Down Expand Up @@ -1438,7 +1433,7 @@ public void onReadyMarkerRemoved(ReadyMarker readyMarker) {
private void executeRulesWithStartLevel() {
getScheduledExecutor().submit(() -> {
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;
Expand Down
Expand Up @@ -500,7 +500,7 @@ private void validateConfiguration(List<ConfigDescriptionParameter> configDescri
if (isOptionalConfig(configDescriptions)) {
return;
} else {
StringBuffer statusDescription = new StringBuffer();
StringBuilder statusDescription = new StringBuilder();
String msg = " '%s';";
for (ConfigDescriptionParameter configParameter : configDescriptions) {
if (configParameter.isRequired()) {
Expand All @@ -517,7 +517,7 @@ private void validateConfiguration(List<ConfigDescriptionParameter> configDescri
processValue(configurations.remove(configParameterName), configParameter);
}
if (!configurations.isEmpty()) {
StringBuffer statusDescription = new StringBuffer();
StringBuilder statusDescription = new StringBuilder();
String msg = " '%s';";
for (String name : configurations.keySet()) {
statusDescription.append(String.format(msg, name));
Expand Down Expand Up @@ -610,7 +610,7 @@ private boolean checkType(Type type, Object configValue) {
*/
private void resolveModuleConfigReferences(List<? extends Module> modules, Map<String, ?> ruleConfiguration) {
if (modules != null) {
StringBuffer statusDescription = new StringBuffer();
StringBuilder statusDescription = new StringBuilder();
for (Module module : modules) {
try {
ReferenceResolver.updateConfiguration(module.getConfiguration(), ruleConfiguration, logger);
Expand Down
Expand Up @@ -33,7 +33,7 @@ public class ConsoleInterpreter {
public static String getHelp(final String base, final String separator,
Collection<ConsoleCommandExtension> extensions) {
final List<String> 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++) {
Expand Down
Expand Up @@ -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 {
Expand Down
Expand Up @@ -132,7 +132,7 @@ private void listFirmwareStatus(Console console, String[] args) {
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()));

Expand Down

0 comments on commit db97610

Please sign in to comment.