From 27dcce5207ec9b4e51cb347047709568310c042f Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Tue, 21 Jul 2020 17:40:29 +0200 Subject: [PATCH] Replace for-loops and iterators with foreach-loops (#1561) Signed-off-by: Paul Vogel --- .../automation/internal/RuleEngineImpl.java | 30 ++++++++----------- .../commands/AutomationCommandEnableRule.java | 16 +++++----- .../commands/AutomationCommandList.java | 16 +++++----- .../commands/AutomationCommandRemove.java | 16 +++++----- .../AbstractResourceBundleProvider.java | 6 ++-- .../provider/file/AbstractFileProvider.java | 4 +-- .../automation/parser/ParsingException.java | 4 +-- .../storage/json/internal/JsonStorage.java | 7 ++--- .../text/AbstractRuleBasedInterpreter.java | 8 ++--- .../voice/text/ExpressionAlternatives.java | 4 +-- .../scheduler/PeriodicSchedulerImplTest.java | 4 +-- 11 files changed, 54 insertions(+), 61 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 e876d8cc5d..bd1d2c37bc 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 @@ -393,8 +393,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener moduleTypes = moduleHandlerFactory.getTypes(); Set notInitializedRules = null; - for (Iterator it = moduleTypes.iterator(); it.hasNext();) { - String moduleTypeName = it.next(); + for (String moduleTypeName : moduleTypes) { Set rules = null; synchronized (this) { moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory); @@ -430,8 +429,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener moduleTypes = moduleHandlerFactory.getTypes(); removeMissingModuleTypes(moduleTypes); - for (Iterator it = moduleTypes.iterator(); it.hasNext();) { - String moduleTypeName = it.next(); + for (String moduleTypeName : moduleTypes) { moduleHandlerFactories.remove(moduleTypeName); } } @@ -914,8 +912,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener moduleTypes) { Map> mapMissingHandlers = null; - for (Iterator it = moduleTypes.iterator(); it.hasNext();) { - String moduleTypeName = it.next(); + for (String moduleTypeName : moduleTypes) { Set rules = null; synchronized (this) { rules = mapModuleTypeToRules.get(moduleTypeName); @@ -1141,15 +1138,14 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener it = conditions.iterator(); it.hasNext();) { + for (WrappedCondition wrappedCondition : conditions) { ruleStatus = getRuleStatus(ruleUID); if (ruleStatus != RuleStatus.RUNNING) { return false; } - final WrappedCondition managedCondition = it.next(); - final Condition condition = managedCondition.unwrap(); - ConditionHandler tHandler = managedCondition.getModuleHandler(); - Map context = getContext(ruleUID, managedCondition.getConnections()); + final Condition condition = wrappedCondition.unwrap(); + ConditionHandler tHandler = wrappedCondition.getModuleHandler(); + Map context = getContext(ruleUID, wrappedCondition.getConnections()); if (tHandler != null && !tHandler.isSatisfied(Collections.unmodifiableMap(context))) { logger.debug("The condition '{}' of rule '{}' is unsatisfied.", condition.getId(), ruleUID); return false; @@ -1170,16 +1166,15 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener it = actions.iterator(); it.hasNext();) { + for (WrappedAction wrappedAction : actions) { ruleStatus = getRuleStatus(ruleUID); if (ruleStatus != RuleStatus.RUNNING) { return; } - final WrappedAction managedAction = it.next(); - final Action action = managedAction.unwrap(); - ActionHandler aHandler = managedAction.getModuleHandler(); + final Action action = wrappedAction.unwrap(); + ActionHandler aHandler = wrappedAction.getModuleHandler(); if (aHandler != null) { - Map context = getContext(ruleUID, managedAction.getConnections()); + Map context = getContext(ruleUID, wrappedAction.getConnections()); try { Map outputs = aHandler.execute(Collections.unmodifiableMap(context)); if (outputs != null) { @@ -1403,8 +1398,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener copyConnections(Set connections) { Set result = new HashSet<>(connections.size()); - for (Iterator it = connections.iterator(); it.hasNext();) { - Connection c = it.next(); + for (Connection c : connections) { result.add(new Connection(c.getInputName(), c.getOutputModuleId(), c.getOutputName(), c.getReference())); } return result; diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandEnableRule.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandEnableRule.java index e293288db3..fb3dbd8314 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandEnableRule.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandEnableRule.java @@ -60,29 +60,29 @@ public class AutomationCommandEnableRule extends AutomationCommand { @Override protected String parseOptionsAndParameters(String[] parameterValues) { - for (int i = 0; i < parameterValues.length; i++) { - if (null == parameterValues[i]) { + for (String parameterValue : parameterValues) { + if (null == parameterValue) { continue; } - if (parameterValues[i].charAt(0) == '-') { - if (OPTION_ST.equals(parameterValues[i])) { + if (parameterValue.charAt(0) == '-') { + if (OPTION_ST.equals(parameterValue)) { st = true; continue; } - return String.format("Unsupported option: %s", parameterValues[i]); + return String.format("Unsupported option: %s", parameterValue); } if (uid == null) { - uid = parameterValues[i]; + uid = parameterValue; continue; } - getEnable(parameterValues[i]); + getEnable(parameterValue); if (hasEnable) { continue; } if (uid == null) { return "Missing required parameter: Rule UID"; } - return String.format("Unsupported parameter: %s", parameterValues[i]); + return String.format("Unsupported parameter: %s", parameterValue); } return SUCCESS; } diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandList.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandList.java index c72bb3cca1..1ad13004d8 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandList.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandList.java @@ -109,29 +109,29 @@ public class AutomationCommandList extends AutomationCommand { protected String parseOptionsAndParameters(String[] parameterValues) { boolean getId = true; boolean getLocale = true; - for (int i = 0; i < parameterValues.length; i++) { - if (null == parameterValues[i]) { + for (String parameterValue : parameterValues) { + if (null == parameterValue) { continue; } - if (parameterValues[i].charAt(0) == '-') { - if (OPTION_ST.equals(parameterValues[i])) { + if (parameterValue.charAt(0) == '-') { + if (OPTION_ST.equals(parameterValue)) { st = true; continue; } - return String.format("Unsupported option: %s", parameterValues[i]); + return String.format("Unsupported option: %s", parameterValue); } if (getId) { - id = parameterValues[i]; + id = parameterValue; getId = false; continue; } if (getLocale) { - String l = parameterValues[i]; + String l = parameterValue; locale = new Locale(l); getLocale = false; } if (getId && getLocale) { - return String.format("Unsupported parameter: %s", parameterValues[i]); + return String.format("Unsupported parameter: %s", parameterValue); } } return SUCCESS; diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java index 2d363abfcb..06b0dba9a4 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java @@ -134,26 +134,26 @@ public class AutomationCommandRemove extends AutomationCommand { } else { getId = false; } - for (int i = 0; i < parameterValues.length; i++) { - if (null == parameterValues[i]) { + for (String parameterValue : parameterValues) { + if (null == parameterValue) { continue; } - if (OPTION_ST.equals(parameterValues[i])) { + if (OPTION_ST.equals(parameterValue)) { st = true; - } else if (parameterValues[i].charAt(0) == '-') { - return String.format("Unsupported option: %s", parameterValues[i]); + } else if (parameterValue.charAt(0) == '-') { + return String.format("Unsupported option: %s", parameterValue); } else if (getUrl) { - url = initURL(parameterValues[i]); + url = initURL(parameterValue); if (url != null) { getUrl = false; } } else if (getId) { - id = parameterValues[i]; + id = parameterValue; if (id != null) { getId = false; } } else { - return String.format("Unsupported parameter: %s", parameterValues[i]); + return String.format("Unsupported parameter: %s", parameterValue); } } if (getUrl) { diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AbstractResourceBundleProvider.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AbstractResourceBundleProvider.java index a7146bf56f..3931efd589 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AbstractResourceBundleProvider.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/AbstractResourceBundleProvider.java @@ -359,9 +359,9 @@ public abstract class AbstractResourceBundleProvider<@NonNull E> { } if (symbolicName != null) { Bundle[] bundles = bundleContext.getBundles(); - for (int i = 0; i < bundles.length; i++) { - if (bundles[i].getSymbolicName().equals(symbolicName)) { - return bundles[i]; + for (Bundle bundle : bundles) { + if (bundle.getSymbolicName().equals(symbolicName)) { + return bundle; } } } diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/AbstractFileProvider.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/AbstractFileProvider.java index dc4b0a82f0..01c45c9214 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/AbstractFileProvider.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/provider/file/AbstractFileProvider.java @@ -117,8 +117,8 @@ public abstract class AbstractFileProvider<@NonNull E> implements Provider { } this.configurationRoots = roots.split(","); } - for (int i = 0; i < this.configurationRoots.length; i++) { - initializeWatchService(this.configurationRoots[i] + File.separator + rootSubdirectory); + for (String configurationRoot : this.configurationRoots) { + initializeWatchService(configurationRoot + File.separator + rootSubdirectory); } } diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ParsingException.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ParsingException.java index 05c70fdbe6..e5dc7f5130 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ParsingException.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/parser/ParsingException.java @@ -65,8 +65,8 @@ public class ParsingException extends Exception { } int index = 0; StackTraceElement[] st = new StackTraceElement[size]; - for (int n = 0; n < exceptions.size(); n++) { - StackTraceElement[] ste = exceptions.get(n).getStackTrace(); + for (ParsingNestedException exception : exceptions) { + StackTraceElement[] ste = exception.getStackTrace(); System.arraycopy(ste, 0, st, index, ste.length); index += ste.length; } diff --git a/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java b/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java index 7d0c68f608..e80294fd1e 100644 --- a/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java +++ b/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java @@ -250,10 +250,9 @@ public class JsonStorage implements Storage { List fileTimes = new ArrayList<>(); File[] files = folder.listFiles(); if (files != null) { - int count = files.length; - for (int i = 0; i < count; i++) { - if (files[i].isFile()) { - String[] parts = files[i].getName().split(SEPARATOR); + for (File value : files) { + if (value.isFile()) { + String[] parts = value.getName().split(SEPARATOR); if (parts.length != 2 || !parts[1].equals(file.getName())) { continue; } diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/AbstractRuleBasedInterpreter.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/AbstractRuleBasedInterpreter.java index 1408ddcb4e..b75af96e76 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/AbstractRuleBasedInterpreter.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/AbstractRuleBasedInterpreter.java @@ -344,8 +344,8 @@ public abstract class AbstractRuleBasedInterpreter implements HumanLanguageInter */ protected Expression[] exps(Object... objects) { List result = new ArrayList<>(); - for (int i = 0; i < objects.length; i++) { - Expression e = exp(objects[i]); + for (Object object : objects) { + Expression e = exp(object); if (e != null) { result.add(e); } @@ -594,8 +594,8 @@ public abstract class AbstractRuleBasedInterpreter implements HumanLanguageInter } else { split = text.toLowerCase(localeSafe).replaceAll("[\\']", "").replaceAll("[^\\w\\s]", " ").split("\\s"); } - for (int i = 0; i < split.length; i++) { - String part = split[i].trim(); + for (String s : split) { + String part = s.trim(); if (part.length() > 0) { parts.add(part); } diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/ExpressionAlternatives.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/ExpressionAlternatives.java index ffca307b93..1e7ef23abc 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/ExpressionAlternatives.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/text/ExpressionAlternatives.java @@ -41,8 +41,8 @@ final class ExpressionAlternatives extends Expression { @Override ASTNode parse(ResourceBundle language, TokenList list) { ASTNode node = new ASTNode(), cr; - for (int i = 0; i < subExpressions.size(); i++) { - cr = subExpressions.get(i).parse(language, list); + for (Expression subExpression : subExpressions) { + cr = subExpression.parse(language, list); if (cr.isSuccess()) { node.setChildren(new ASTNode[] { cr }); node.setRemainingTokens(cr.getRemainingTokens()); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/scheduler/PeriodicSchedulerImplTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/scheduler/PeriodicSchedulerImplTest.java index cfe5247727..0e7f12c90b 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/scheduler/PeriodicSchedulerImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/scheduler/PeriodicSchedulerImplTest.java @@ -52,8 +52,8 @@ public class PeriodicSchedulerImplTest { // the first time set is the offset on which we check the next values. long offset = times.poll(); long[] expectedResults = { 2, 5, 8, 11, 14 }; - for (int i = 0; i < expectedResults.length; i++) { - assertEquals("Expected periodic time", offset + expectedResults[i], times.poll().longValue()); + for (long expectedResult : expectedResults) { + assertEquals("Expected periodic time", offset + expectedResult, times.poll().longValue()); } assertFalse("No more jobs should have been scheduled", semaphore.tryAcquire(1, TimeUnit.SECONDS)); }