From 4895977966886ce6bc508fdd929e117b3a6b703b Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sun, 20 Oct 2019 01:27:11 +0200 Subject: [PATCH] Use isEmpty() instead of "size() == 0" (#1151) isEmpty() expresses the intent more clearly and is therefore preferred. Counting the number of elements can also be an expensive operation e.g. when using linked lists. Signed-off-by: Wouter Born --- .../openhab/core/automation/internal/RuleEngineImpl.java | 4 ++-- .../openhab/core/automation/internal/commands/Printer.java | 2 +- .../automation/internal/provider/file/WatchServiceUtil.java | 2 +- .../org/openhab/core/binding/AbstractActiveBinding.java | 2 +- .../java/org/eclipse/smarthome/config/core/ConfigUtil.java | 2 +- .../io/rest/core/internal/profile/ProfileTypeResource.java | 6 +++--- .../chart/defaultchartprovider/DefaultChartProvider.java | 2 +- .../smarthome/ui/internal/items/ItemUIRegistryImpl.java | 6 +++--- .../smarthome/core/voice/text/ExpressionCardinality.java | 2 +- 9 files changed, 14 insertions(+), 14 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 95c833d78f..3e9268bea4 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 @@ -1140,7 +1140,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener conditions = rule.getConditions(); - if (conditions.size() == 0) { + if (conditions.isEmpty()) { return true; } final String ruleUID = rule.getUID(); @@ -1171,7 +1171,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener actions = rule.getActions(); - if (actions.size() == 0) { + if (actions.isEmpty()) { return; } RuleStatus ruleStatus = null; diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/Printer.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/Printer.java index 2dca4e7b2d..9cc6a4ed0e 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/Printer.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/Printer.java @@ -537,7 +537,7 @@ public class Printer { * @return a formated string, representing the set of {@link Input}s or {@link Output}s or {@link Input}s. */ private static String getTagsRecord(Set tags) { - if (tags == null || tags.size() == 0) { + if (tags == null || tags.isEmpty()) { return "[ ]"; } StringBuilder res = new StringBuilder().append("[ "); diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/WatchServiceUtil.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/WatchServiceUtil.java index 5b30818167..77e9b6a51c 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/WatchServiceUtil.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/WatchServiceUtil.java @@ -50,7 +50,7 @@ public class WatchServiceUtil { synchronized (WATCH_SERVICES) { Map watchers = WATCH_SERVICES.get(provider); aws = watchers.remove(watchingDir); - if (watchers.size() == 0) { + if (watchers.isEmpty()) { WATCH_SERVICES.remove(provider); } } diff --git a/bundles/org.openhab.core.compat1x/src/main/java/org/openhab/core/binding/AbstractActiveBinding.java b/bundles/org.openhab.core.compat1x/src/main/java/org/openhab/core/binding/AbstractActiveBinding.java index dffb76ed5f..379e8fdfcb 100644 --- a/bundles/org.openhab.core.compat1x/src/main/java/org/openhab/core/binding/AbstractActiveBinding.java +++ b/bundles/org.openhab.core.compat1x/src/main/java/org/openhab/core/binding/AbstractActiveBinding.java @@ -55,7 +55,7 @@ public abstract class AbstractActiveBinding

extends A // if there are no binding providers there is no need to run this // refresh thread any longer ... - if (this.providers.size() == 0) { + if (this.providers.isEmpty()) { activeService.deactivate(); } } diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java b/bundles/org.openhab.core.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java index 1d2d8bf1f0..f7e2a9166f 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java @@ -146,7 +146,7 @@ public class ConfigUtil { * @throws IllegalArgumentException if the type of the normalized values differ or an invalid type has been given */ private static Collection normalizeCollection(Collection collection) throws IllegalArgumentException { - if (collection.size() == 0) { + if (collection.isEmpty()) { return Collections.emptyList(); } else { final List lst = new ArrayList<>(collection.size()); diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/eclipse/smarthome/io/rest/core/internal/profile/ProfileTypeResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/eclipse/smarthome/io/rest/core/internal/profile/ProfileTypeResource.java index d0bfb90247..a077ffd39c 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/eclipse/smarthome/io/rest/core/internal/profile/ProfileTypeResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/eclipse/smarthome/io/rest/core/internal/profile/ProfileTypeResource.java @@ -120,7 +120,7 @@ public class ProfileTypeResource implements RESTResource { private boolean profileTypeMatchesItemType(ProfileType pt, String itemType) { Collection supportedItemTypesOnProfileType = pt.getSupportedItemTypes(); - if (supportedItemTypesOnProfileType.size() == 0 + if (supportedItemTypesOnProfileType.isEmpty() || supportedItemTypesOnProfileType.contains(ItemUtil.getMainItemType(itemType)) || supportedItemTypesOnProfileType.contains(itemType)) { return true; @@ -132,7 +132,7 @@ public class ProfileTypeResource implements RESTResource { if (profileType instanceof TriggerProfileType) { TriggerProfileType triggerProfileType = (TriggerProfileType) profileType; - if (triggerProfileType.getSupportedChannelTypeUIDs().size() == 0) { + if (triggerProfileType.getSupportedChannelTypeUIDs().isEmpty()) { return true; } @@ -147,7 +147,7 @@ public class ProfileTypeResource implements RESTResource { if (profileType instanceof StateProfileType) { StateProfileType stateProfileType = (StateProfileType) profileType; - if (stateProfileType.getSupportedItemTypesOfChannel().size() == 0) { + if (stateProfileType.getSupportedItemTypesOfChannel().isEmpty()) { return true; } diff --git a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java index a8a1e0c90c..4972ce4be7 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java @@ -372,7 +372,7 @@ public class DefaultChartProvider implements ChartProvider { // Add the new series to the chart - only if there's data elements to display // The chart engine will throw an exception if there's no data - if (xData.size() == 0) { + if (xData.isEmpty()) { return false; } diff --git a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java index f90dec4491..29c235d841 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java @@ -623,7 +623,7 @@ public class ItemUIRegistryImpl implements ItemUIRegistry { } } else if (w instanceof Switch) { Switch sw = (Switch) w; - if (sw.getMappings().size() == 0) { + if (sw.getMappings().isEmpty()) { returnState = itemState.as(OnOffType.class); } } @@ -1084,7 +1084,7 @@ public class ItemUIRegistryImpl implements ItemUIRegistry { if (colorList == null) { return null; } - if (colorList.size() == 0) { + if (colorList.isEmpty()) { return null; } @@ -1167,7 +1167,7 @@ public class ItemUIRegistryImpl implements ItemUIRegistry { if (ruleList == null) { return true; } - if (ruleList.size() == 0) { + if (ruleList.isEmpty()) { return true; } diff --git a/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/text/ExpressionCardinality.java b/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/text/ExpressionCardinality.java index c7e234ae13..b1ddc30f7a 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/text/ExpressionCardinality.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/text/ExpressionCardinality.java @@ -58,7 +58,7 @@ public final class ExpressionCardinality extends Expression { break; } } - if (!(atLeastOne && nodes.size() == 0)) { + if (!(atLeastOne && nodes.isEmpty())) { node.setChildren(nodes.toArray(new ASTNode[0])); node.setRemainingTokens(list); node.setSuccess(true);