Replace for-loops and iterators with foreach-loops (#1561)

Signed-off-by: Paul Vogel <pavog@users.noreply.github.com>
pull/1564/head
Paul Vogel 2020-07-21 17:40:29 +02:00 committed by GitHub
parent 2ee2e2d2f5
commit 27dcce5207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 54 additions and 61 deletions

View File

@ -393,8 +393,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
allModuleHandlerFactories.add(moduleHandlerFactory); allModuleHandlerFactories.add(moduleHandlerFactory);
Collection<String> moduleTypes = moduleHandlerFactory.getTypes(); Collection<String> moduleTypes = moduleHandlerFactory.getTypes();
Set<String> notInitializedRules = null; Set<String> notInitializedRules = null;
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext();) { for (String moduleTypeName : moduleTypes) {
String moduleTypeName = it.next();
Set<String> rules = null; Set<String> rules = null;
synchronized (this) { synchronized (this) {
moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory); moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory);
@ -430,8 +429,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
allModuleHandlerFactories.remove(moduleHandlerFactory); allModuleHandlerFactories.remove(moduleHandlerFactory);
Collection<String> moduleTypes = moduleHandlerFactory.getTypes(); Collection<String> moduleTypes = moduleHandlerFactory.getTypes();
removeMissingModuleTypes(moduleTypes); removeMissingModuleTypes(moduleTypes);
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext();) { for (String moduleTypeName : moduleTypes) {
String moduleTypeName = it.next();
moduleHandlerFactories.remove(moduleTypeName); moduleHandlerFactories.remove(moduleTypeName);
} }
} }
@ -914,8 +912,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
private void removeMissingModuleTypes(Collection<String> moduleTypes) { private void removeMissingModuleTypes(Collection<String> moduleTypes) {
Map<String, @Nullable List<String>> mapMissingHandlers = null; Map<String, @Nullable List<String>> mapMissingHandlers = null;
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext();) { for (String moduleTypeName : moduleTypes) {
String moduleTypeName = it.next();
Set<String> rules = null; Set<String> rules = null;
synchronized (this) { synchronized (this) {
rules = mapModuleTypeToRules.get(moduleTypeName); rules = mapModuleTypeToRules.get(moduleTypeName);
@ -1141,15 +1138,14 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
} }
final String ruleUID = rule.getUID(); final String ruleUID = rule.getUID();
RuleStatus ruleStatus = null; RuleStatus ruleStatus = null;
for (Iterator<WrappedCondition> it = conditions.iterator(); it.hasNext();) { for (WrappedCondition wrappedCondition : conditions) {
ruleStatus = getRuleStatus(ruleUID); ruleStatus = getRuleStatus(ruleUID);
if (ruleStatus != RuleStatus.RUNNING) { if (ruleStatus != RuleStatus.RUNNING) {
return false; return false;
} }
final WrappedCondition managedCondition = it.next(); final Condition condition = wrappedCondition.unwrap();
final Condition condition = managedCondition.unwrap(); ConditionHandler tHandler = wrappedCondition.getModuleHandler();
ConditionHandler tHandler = managedCondition.getModuleHandler(); Map<String, @Nullable Object> context = getContext(ruleUID, wrappedCondition.getConnections());
Map<String, @Nullable Object> context = getContext(ruleUID, managedCondition.getConnections());
if (tHandler != null && !tHandler.isSatisfied(Collections.unmodifiableMap(context))) { if (tHandler != null && !tHandler.isSatisfied(Collections.unmodifiableMap(context))) {
logger.debug("The condition '{}' of rule '{}' is unsatisfied.", condition.getId(), ruleUID); logger.debug("The condition '{}' of rule '{}' is unsatisfied.", condition.getId(), ruleUID);
return false; return false;
@ -1170,16 +1166,15 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
return; return;
} }
RuleStatus ruleStatus = null; RuleStatus ruleStatus = null;
for (Iterator<WrappedAction> it = actions.iterator(); it.hasNext();) { for (WrappedAction wrappedAction : actions) {
ruleStatus = getRuleStatus(ruleUID); ruleStatus = getRuleStatus(ruleUID);
if (ruleStatus != RuleStatus.RUNNING) { if (ruleStatus != RuleStatus.RUNNING) {
return; return;
} }
final WrappedAction managedAction = it.next(); final Action action = wrappedAction.unwrap();
final Action action = managedAction.unwrap(); ActionHandler aHandler = wrappedAction.getModuleHandler();
ActionHandler aHandler = managedAction.getModuleHandler();
if (aHandler != null) { if (aHandler != null) {
Map<String, @Nullable Object> context = getContext(ruleUID, managedAction.getConnections()); Map<String, @Nullable Object> context = getContext(ruleUID, wrappedAction.getConnections());
try { try {
Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context)); Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context));
if (outputs != null) { if (outputs != null) {
@ -1403,8 +1398,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
*/ */
private Set<Connection> copyConnections(Set<Connection> connections) { private Set<Connection> copyConnections(Set<Connection> connections) {
Set<Connection> result = new HashSet<>(connections.size()); Set<Connection> result = new HashSet<>(connections.size());
for (Iterator<Connection> it = connections.iterator(); it.hasNext();) { for (Connection c : connections) {
Connection c = it.next();
result.add(new Connection(c.getInputName(), c.getOutputModuleId(), c.getOutputName(), c.getReference())); result.add(new Connection(c.getInputName(), c.getOutputModuleId(), c.getOutputName(), c.getReference()));
} }
return result; return result;

View File

@ -60,29 +60,29 @@ public class AutomationCommandEnableRule extends AutomationCommand {
@Override @Override
protected String parseOptionsAndParameters(String[] parameterValues) { protected String parseOptionsAndParameters(String[] parameterValues) {
for (int i = 0; i < parameterValues.length; i++) { for (String parameterValue : parameterValues) {
if (null == parameterValues[i]) { if (null == parameterValue) {
continue; continue;
} }
if (parameterValues[i].charAt(0) == '-') { if (parameterValue.charAt(0) == '-') {
if (OPTION_ST.equals(parameterValues[i])) { if (OPTION_ST.equals(parameterValue)) {
st = true; st = true;
continue; continue;
} }
return String.format("Unsupported option: %s", parameterValues[i]); return String.format("Unsupported option: %s", parameterValue);
} }
if (uid == null) { if (uid == null) {
uid = parameterValues[i]; uid = parameterValue;
continue; continue;
} }
getEnable(parameterValues[i]); getEnable(parameterValue);
if (hasEnable) { if (hasEnable) {
continue; continue;
} }
if (uid == null) { if (uid == null) {
return "Missing required parameter: Rule UID"; return "Missing required parameter: Rule UID";
} }
return String.format("Unsupported parameter: %s", parameterValues[i]); return String.format("Unsupported parameter: %s", parameterValue);
} }
return SUCCESS; return SUCCESS;
} }

View File

@ -109,29 +109,29 @@ public class AutomationCommandList extends AutomationCommand {
protected String parseOptionsAndParameters(String[] parameterValues) { protected String parseOptionsAndParameters(String[] parameterValues) {
boolean getId = true; boolean getId = true;
boolean getLocale = true; boolean getLocale = true;
for (int i = 0; i < parameterValues.length; i++) { for (String parameterValue : parameterValues) {
if (null == parameterValues[i]) { if (null == parameterValue) {
continue; continue;
} }
if (parameterValues[i].charAt(0) == '-') { if (parameterValue.charAt(0) == '-') {
if (OPTION_ST.equals(parameterValues[i])) { if (OPTION_ST.equals(parameterValue)) {
st = true; st = true;
continue; continue;
} }
return String.format("Unsupported option: %s", parameterValues[i]); return String.format("Unsupported option: %s", parameterValue);
} }
if (getId) { if (getId) {
id = parameterValues[i]; id = parameterValue;
getId = false; getId = false;
continue; continue;
} }
if (getLocale) { if (getLocale) {
String l = parameterValues[i]; String l = parameterValue;
locale = new Locale(l); locale = new Locale(l);
getLocale = false; getLocale = false;
} }
if (getId && getLocale) { if (getId && getLocale) {
return String.format("Unsupported parameter: %s", parameterValues[i]); return String.format("Unsupported parameter: %s", parameterValue);
} }
} }
return SUCCESS; return SUCCESS;

View File

@ -134,26 +134,26 @@ public class AutomationCommandRemove extends AutomationCommand {
} else { } else {
getId = false; getId = false;
} }
for (int i = 0; i < parameterValues.length; i++) { for (String parameterValue : parameterValues) {
if (null == parameterValues[i]) { if (null == parameterValue) {
continue; continue;
} }
if (OPTION_ST.equals(parameterValues[i])) { if (OPTION_ST.equals(parameterValue)) {
st = true; st = true;
} else if (parameterValues[i].charAt(0) == '-') { } else if (parameterValue.charAt(0) == '-') {
return String.format("Unsupported option: %s", parameterValues[i]); return String.format("Unsupported option: %s", parameterValue);
} else if (getUrl) { } else if (getUrl) {
url = initURL(parameterValues[i]); url = initURL(parameterValue);
if (url != null) { if (url != null) {
getUrl = false; getUrl = false;
} }
} else if (getId) { } else if (getId) {
id = parameterValues[i]; id = parameterValue;
if (id != null) { if (id != null) {
getId = false; getId = false;
} }
} else { } else {
return String.format("Unsupported parameter: %s", parameterValues[i]); return String.format("Unsupported parameter: %s", parameterValue);
} }
} }
if (getUrl) { if (getUrl) {

View File

@ -359,9 +359,9 @@ public abstract class AbstractResourceBundleProvider<@NonNull E> {
} }
if (symbolicName != null) { if (symbolicName != null) {
Bundle[] bundles = bundleContext.getBundles(); Bundle[] bundles = bundleContext.getBundles();
for (int i = 0; i < bundles.length; i++) { for (Bundle bundle : bundles) {
if (bundles[i].getSymbolicName().equals(symbolicName)) { if (bundle.getSymbolicName().equals(symbolicName)) {
return bundles[i]; return bundle;
} }
} }
} }

View File

@ -117,8 +117,8 @@ public abstract class AbstractFileProvider<@NonNull E> implements Provider<E> {
} }
this.configurationRoots = roots.split(","); this.configurationRoots = roots.split(",");
} }
for (int i = 0; i < this.configurationRoots.length; i++) { for (String configurationRoot : this.configurationRoots) {
initializeWatchService(this.configurationRoots[i] + File.separator + rootSubdirectory); initializeWatchService(configurationRoot + File.separator + rootSubdirectory);
} }
} }

View File

@ -65,8 +65,8 @@ public class ParsingException extends Exception {
} }
int index = 0; int index = 0;
StackTraceElement[] st = new StackTraceElement[size]; StackTraceElement[] st = new StackTraceElement[size];
for (int n = 0; n < exceptions.size(); n++) { for (ParsingNestedException exception : exceptions) {
StackTraceElement[] ste = exceptions.get(n).getStackTrace(); StackTraceElement[] ste = exception.getStackTrace();
System.arraycopy(ste, 0, st, index, ste.length); System.arraycopy(ste, 0, st, index, ste.length);
index += ste.length; index += ste.length;
} }

View File

@ -250,10 +250,9 @@ public class JsonStorage<T> implements Storage<T> {
List<Long> fileTimes = new ArrayList<>(); List<Long> fileTimes = new ArrayList<>();
File[] files = folder.listFiles(); File[] files = folder.listFiles();
if (files != null) { if (files != null) {
int count = files.length; for (File value : files) {
for (int i = 0; i < count; i++) { if (value.isFile()) {
if (files[i].isFile()) { String[] parts = value.getName().split(SEPARATOR);
String[] parts = files[i].getName().split(SEPARATOR);
if (parts.length != 2 || !parts[1].equals(file.getName())) { if (parts.length != 2 || !parts[1].equals(file.getName())) {
continue; continue;
} }

View File

@ -344,8 +344,8 @@ public abstract class AbstractRuleBasedInterpreter implements HumanLanguageInter
*/ */
protected Expression[] exps(Object... objects) { protected Expression[] exps(Object... objects) {
List<Expression> result = new ArrayList<>(); List<Expression> result = new ArrayList<>();
for (int i = 0; i < objects.length; i++) { for (Object object : objects) {
Expression e = exp(objects[i]); Expression e = exp(object);
if (e != null) { if (e != null) {
result.add(e); result.add(e);
} }
@ -594,8 +594,8 @@ public abstract class AbstractRuleBasedInterpreter implements HumanLanguageInter
} else { } else {
split = text.toLowerCase(localeSafe).replaceAll("[\\']", "").replaceAll("[^\\w\\s]", " ").split("\\s"); split = text.toLowerCase(localeSafe).replaceAll("[\\']", "").replaceAll("[^\\w\\s]", " ").split("\\s");
} }
for (int i = 0; i < split.length; i++) { for (String s : split) {
String part = split[i].trim(); String part = s.trim();
if (part.length() > 0) { if (part.length() > 0) {
parts.add(part); parts.add(part);
} }

View File

@ -41,8 +41,8 @@ final class ExpressionAlternatives extends Expression {
@Override @Override
ASTNode parse(ResourceBundle language, TokenList list) { ASTNode parse(ResourceBundle language, TokenList list) {
ASTNode node = new ASTNode(), cr; ASTNode node = new ASTNode(), cr;
for (int i = 0; i < subExpressions.size(); i++) { for (Expression subExpression : subExpressions) {
cr = subExpressions.get(i).parse(language, list); cr = subExpression.parse(language, list);
if (cr.isSuccess()) { if (cr.isSuccess()) {
node.setChildren(new ASTNode[] { cr }); node.setChildren(new ASTNode[] { cr });
node.setRemainingTokens(cr.getRemainingTokens()); node.setRemainingTokens(cr.getRemainingTokens());

View File

@ -52,8 +52,8 @@ public class PeriodicSchedulerImplTest {
// the first time set is the offset on which we check the next values. // the first time set is the offset on which we check the next values.
long offset = times.poll(); long offset = times.poll();
long[] expectedResults = { 2, 5, 8, 11, 14 }; long[] expectedResults = { 2, 5, 8, 11, 14 };
for (int i = 0; i < expectedResults.length; i++) { for (long expectedResult : expectedResults) {
assertEquals("Expected periodic time", offset + expectedResults[i], times.poll().longValue()); assertEquals("Expected periodic time", offset + expectedResult, times.poll().longValue());
} }
assertFalse("No more jobs should have been scheduled", semaphore.tryAcquire(1, TimeUnit.SECONDS)); assertFalse("No more jobs should have been scheduled", semaphore.tryAcquire(1, TimeUnit.SECONDS));
} }