For-each conversion in director.

Change-Id: I952f3752adde2b6be0883393812d3add19d98cc4
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
diff --git a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Explanation.java b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Explanation.java
index 0d2c46a..11f0e80 100644
--- a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Explanation.java
+++ b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Explanation.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2017 Daniel Le Berre and others. All rights reserved. This
+ * Copyright (c) 2009, 2018 Daniel Le Berre and others. All rights reserved. This
  * program and the accompanying materials are made available under the terms of
  * the Eclipse Public License v1.0 which accompanies this distribution, and is
  * available at http://www.eclipse.org/legal/epl-v10.html
@@ -11,12 +11,8 @@
 package org.eclipse.equinox.internal.p2.director;
 
 import java.util.Arrays;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.MultiStatus;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.equinox.p2.metadata.IInstallableUnit;
-import org.eclipse.equinox.p2.metadata.IInstallableUnitPatch;
-import org.eclipse.equinox.p2.metadata.IRequirement;
+import org.eclipse.core.runtime.*;
+import org.eclipse.equinox.p2.metadata.*;
 import org.eclipse.osgi.util.NLS;
 
 public abstract class Explanation implements Comparable<Explanation> {
@@ -273,8 +269,8 @@
 		@Override
 		public IStatus toStatus() {
 			MultiStatus result = new MultiStatus(DirectorActivator.PI_DIRECTOR, 1, NLS.bind(Messages.Explanation_singleton, ""), null); //$NON-NLS-1$
-			for (int i = 0; i < ius.length; i++)
-				result.add(new Status(IStatus.ERROR, DirectorActivator.PI_DIRECTOR, getUserReadableName(ius[i])));
+			for (IInstallableUnit iu : ius)
+				result.add(new Status(IStatus.ERROR, DirectorActivator.PI_DIRECTOR, getUserReadableName(iu)));
 			return result;
 		}
 
diff --git a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/OptimizationFunction.java b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/OptimizationFunction.java
index a439c0d..3097fc1 100644
--- a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/OptimizationFunction.java
+++ b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/OptimizationFunction.java
@@ -111,8 +111,7 @@
 			if (req.getMin() > 0 || !req.isGreedy())
 				continue;
 			IQueryResult<IInstallableUnit> matches = picker.query(QueryUtil.createMatchQuery(req.getMatches()), null);
-			for (Iterator<IInstallableUnit> iterator = matches.iterator(); iterator.hasNext();) {
-				IInstallableUnit match = iterator.next();
+			for (IInstallableUnit match : matches) {
 				if (match instanceof IInstallableUnitPatch) {
 					requestedPatches.add(match);
 					countOptional = countOptional + 1;
diff --git a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java
index 937a1d6..e130576 100644
--- a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java
+++ b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java
@@ -411,9 +411,7 @@
 					}
 				} else {
 					List<Object> newConstraint = new ArrayList<>(matches.size());
-					IInstallableUnit current;
-					for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-						current = it.next();
+					for (IInstallableUnit current : matches) {
 						newConstraint.add(getNonGreedyVariable(current));
 					}
 					createImplication(new Object[] {iu}, newConstraint, new Explanation.HardRequirement(iu, req)); // FIXME
@@ -421,13 +419,11 @@
 			}
 		} else {
 			if (!matches.isEmpty()) {
-				IInstallableUnit current;
 				AbstractVariable abs;
 				if (req.isGreedy()) {
 					abs = getAbstractVariable(req);
 					createImplication(new Object[] {abs, iu}, matches, Explanation.OPTIONAL_REQUIREMENT);
-					for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-						current = it.next();
+					for (IInstallableUnit current : matches) {
 						if (nonGreedyIUs.contains(current)) {
 							addNonGreedyProvider(getNonGreedyVariable(current), abs);
 						}
@@ -436,8 +432,7 @@
 				} else {
 					abs = getAbstractVariable(req, false);
 					List<Object> newConstraint = new ArrayList<>();
-					for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-						current = it.next();
+					for (IInstallableUnit current : matches) {
 						newConstraint.add(getNonGreedyVariable(current));
 					}
 					createImplication(new Object[] {abs, iu}, newConstraint, Explanation.OPTIONAL_REQUIREMENT);
@@ -556,7 +551,6 @@
 						if (matches.isEmpty()) {
 							missingRequirement(patch, req);
 						} else {
-							IInstallableUnit current;
 							if (req.isGreedy()) {
 								IInstallableUnit reqIu = matches.get(0);
 								Explanation explanation;
@@ -570,16 +564,14 @@
 									explanation = new Explanation.PatchedHardRequirement(iu, req, patch);
 								}
 								createImplication(new Object[] {patch, iu}, matches, explanation);
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									if (nonGreedyIUs.contains(current)) {
 										addNonGreedyProvider(getNonGreedyVariable(current), iu);
 									}
 								}
 							} else {
 								List<Object> newConstraint = new ArrayList<>();
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									newConstraint.add(getNonGreedyVariable(current));
 								}
 								createImplication(new Object[] {iu}, newConstraint, new Explanation.HardRequirement(iu, req)); // FIXME
@@ -587,13 +579,11 @@
 						}
 					} else {
 						if (!matches.isEmpty()) {
-							IInstallableUnit current;
 							AbstractVariable abs;
 							if (req.isGreedy()) {
 								abs = getAbstractVariable(req);
 								createImplication(new Object[] {patch, abs, iu}, matches, Explanation.OPTIONAL_REQUIREMENT);
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									if (nonGreedyIUs.contains(current)) {
 										addNonGreedyProvider(getNonGreedyVariable(current), abs);
 									}
@@ -602,8 +592,7 @@
 							} else {
 								abs = getAbstractVariable(req, false);
 								List<Object> newConstraint = new ArrayList<>(matches.size());
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									newConstraint.add(getNonGreedyVariable(current));
 								}
 								createImplication(new Object[] {patch, abs, iu}, newConstraint, Explanation.OPTIONAL_REQUIREMENT);
@@ -634,10 +623,8 @@
 							pending.matches = matches;
 						} else {
 							// manage non greedy IUs
-							IInstallableUnit current;
 							List<Object> nonGreedys = new ArrayList<>();
-							for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-								current = it.next();
+							for (IInstallableUnit current : matches) {
 								if (nonGreedyIUs.contains(current)) {
 									nonGreedys.add(getNonGreedyVariable(current));
 								}
@@ -659,16 +646,14 @@
 								// Fix: make sure we collect all patches that will impact this IU-req, not just one
 								pending.explanation = explanation;
 								pending.matches = matches;
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									if (nonGreedyIUs.contains(current)) {
 										addNonGreedyProvider(getNonGreedyVariable(current), iu);
 									}
 								}
 							} else {
 								List<Object> newConstraint = new ArrayList<>(matches.size());
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									newConstraint.add(getNonGreedyVariable(current));
 								}
 								pending.explanation = new Explanation.HardRequirement(iu, req);
@@ -679,7 +664,6 @@
 						}
 					} else {
 						if (!matches.isEmpty()) {
-							IInstallableUnit current;
 							AbstractVariable abs;
 							matches.add(patch);
 							pending = new Pending();
@@ -690,8 +674,7 @@
 								// Fix: make sure we collect all patches that will impact this IU-req, not just one
 								pending.left = new Object[] {abs, iu};
 								pending.matches = matches;
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									if (nonGreedyIUs.contains(current)) {
 										addNonGreedyProvider(getNonGreedyVariable(current), abs);
 									}
@@ -699,8 +682,7 @@
 							} else {
 								abs = getAbstractVariable(req, false);
 								List<Object> newConstraint = new ArrayList<>(matches.size());
-								for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-									current = it.next();
+								for (IInstallableUnit current : matches) {
 									newConstraint.add(getNonGreedyVariable(current));
 								}
 								newConstraint.add(patch);
@@ -741,10 +723,8 @@
 					}
 				} else {
 					// manage non greedy IUs
-					IInstallableUnit current;
 					List<Object> nonGreedys = new ArrayList<>(matches.size());
-					for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-						current = it.next();
+					for (IInstallableUnit current : matches) {
 						if (nonGreedyIUs.contains(current)) {
 							nonGreedys.add(getNonGreedyVariable(current));
 						}
@@ -764,16 +744,14 @@
 							explanation = new Explanation.HardRequirement(iu, req);
 						}
 						createImplication(iu, matches, explanation);
-						for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-							current = it.next();
+						for (IInstallableUnit current : matches) {
 							if (nonGreedyIUs.contains(current)) {
 								addNonGreedyProvider(getNonGreedyVariable(current), iu);
 							}
 						}
 					} else {
 						List<Object> newConstraint = new ArrayList<>(matches.size());
-						for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-							current = it.next();
+						for (IInstallableUnit current : matches) {
 							newConstraint.add(getNonGreedyVariable(current));
 						}
 						createImplication(new Object[] {iu}, newConstraint, new Explanation.HardRequirement(iu, req)); // FIXME
@@ -781,15 +759,13 @@
 				}
 			} else {
 				if (!matches.isEmpty()) {
-					IInstallableUnit current;
 					if (!requiredPatches.isEmpty())
 						matches.addAll(requiredPatches);
 					AbstractVariable abs;
 					if (req.isGreedy()) {
 						abs = getAbstractVariable(req);
 						createImplication(new Object[] {abs, iu}, matches, Explanation.OPTIONAL_REQUIREMENT);
-						for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-							current = it.next();
+						for (IInstallableUnit current : matches) {
 							if (nonGreedyIUs.contains(current)) {
 								addNonGreedyProvider(getNonGreedyVariable(current), iu);
 							}
@@ -797,8 +773,7 @@
 					} else {
 						abs = getAbstractVariable(req, false);
 						List<Object> newConstraint = new ArrayList<>(matches.size());
-						for (Iterator<IInstallableUnit> it = matches.iterator(); it.hasNext();) {
-							current = it.next();
+						for (IInstallableUnit current : matches) {
 							newConstraint.add(getNonGreedyVariable(current));
 						}
 						createImplication(new Object[] {abs, iu}, newConstraint, new Explanation.HardRequirement(iu, req)); // FIXME
@@ -831,8 +806,7 @@
 	private List<IInstallableUnit> getApplicableMatches(IRequirement req) {
 		List<IInstallableUnit> target = new ArrayList<>();
 		IQueryResult<IInstallableUnit> matches = picker.query(QueryUtil.createMatchQuery(req.getMatches()), null);
-		for (Iterator<IInstallableUnit> iterator = matches.iterator(); iterator.hasNext();) {
-			IInstallableUnit match = iterator.next();
+		for (IInstallableUnit match : matches) {
 			if (isApplicable(match)) {
 				target.add(match);
 			}
@@ -868,9 +842,9 @@
 				rrr.add(new IRequirement[] {null, change.newValue()});
 		}
 		//Add all the unmodified requirements to the result
-		for (int i = 0; i < originalRequirements.length; i++) {
-			if (originalRequirements[i] != null)
-				rrr.add(new IRequirement[] {originalRequirements[i], originalRequirements[i]});
+		for (IRequirement originalRequirement : originalRequirements) {
+			if (originalRequirement != null)
+				rrr.add(new IRequirement[] {originalRequirement, originalRequirement});
 		}
 		return rrr.toArray(new IRequirement[rrr.size()][]);
 	}
@@ -953,8 +927,8 @@
 	private void createAtMostOne(IInstallableUnit[] ius) throws ContradictionException {
 		if (DEBUG) {
 			StringBuffer b = new StringBuffer();
-			for (int i = 0; i < ius.length; i++) {
-				b.append(ius[i].toString());
+			for (IInstallableUnit iu : ius) {
+				b.append(iu.toString());
 			}
 			Tracing.debug("At most 1 of " + b); //$NON-NLS-1$
 		}