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);
Collection<String> moduleTypes = moduleHandlerFactory.getTypes();
Set<String> notInitializedRules = null;
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext();) {
String moduleTypeName = it.next();
for (String moduleTypeName : moduleTypes) {
Set<String> rules = null;
synchronized (this) {
moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory);
@ -430,8 +429,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
allModuleHandlerFactories.remove(moduleHandlerFactory);
Collection<String> moduleTypes = moduleHandlerFactory.getTypes();
removeMissingModuleTypes(moduleTypes);
for (Iterator<String> 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<Modul
private void removeMissingModuleTypes(Collection<String> moduleTypes) {
Map<String, @Nullable List<String>> mapMissingHandlers = null;
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext();) {
String moduleTypeName = it.next();
for (String moduleTypeName : moduleTypes) {
Set<String> rules = null;
synchronized (this) {
rules = mapModuleTypeToRules.get(moduleTypeName);
@ -1141,15 +1138,14 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
}
final String ruleUID = rule.getUID();
RuleStatus ruleStatus = null;
for (Iterator<WrappedCondition> 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<String, @Nullable Object> context = getContext(ruleUID, managedCondition.getConnections());
final Condition condition = wrappedCondition.unwrap();
ConditionHandler tHandler = wrappedCondition.getModuleHandler();
Map<String, @Nullable Object> 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<Modul
return;
}
RuleStatus ruleStatus = null;
for (Iterator<WrappedAction> 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<String, @Nullable Object> context = getContext(ruleUID, managedAction.getConnections());
Map<String, @Nullable Object> context = getContext(ruleUID, wrappedAction.getConnections());
try {
Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context));
if (outputs != null) {
@ -1403,8 +1398,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
*/
private Set<Connection> copyConnections(Set<Connection> connections) {
Set<Connection> result = new HashSet<>(connections.size());
for (Iterator<Connection> 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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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) {

View File

@ -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;
}
}
}

View File

@ -117,8 +117,8 @@ public abstract class AbstractFileProvider<@NonNull E> implements Provider<E> {
}
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);
}
}

View File

@ -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;
}

View File

@ -250,10 +250,9 @@ public class JsonStorage<T> implements Storage<T> {
List<Long> 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;
}

View File

@ -344,8 +344,8 @@ public abstract class AbstractRuleBasedInterpreter implements HumanLanguageInter
*/
protected Expression[] exps(Object... objects) {
List<Expression> 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);
}

View File

@ -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());

View File

@ -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));
}