diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java
index 115290631..25202c917 100644
--- a/src/main/org/apache/tools/ant/Main.java
+++ b/src/main/org/apache/tools/ant/Main.java
@@ -47,7 +47,7 @@ import org.apache.tools.ant.property.ResolvePropertyMap;
import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.ProxySetup;
-
+import org.apache.tools.ant.util.StreamUtils;
/**
* Command line entry point into Ant. This class is entered via the
@@ -1266,7 +1266,7 @@ public class Main implements AntMain {
}
msg.append(eol);
if (!dependencies.isEmpty() && dependencies.elementAt(i).hasMoreElements()) {
- msg.append(Collections.list(dependencies.elementAt(i)).stream()
+ msg.append(StreamUtils.enumerationAsStream(dependencies.elementAt(i))
.collect(Collectors.joining(", ", " depends on: ", eol)));
}
}
diff --git a/src/main/org/apache/tools/ant/ProjectHelperRepository.java b/src/main/org/apache/tools/ant/ProjectHelperRepository.java
index 8bb0cb2e3..b1c48beb5 100644
--- a/src/main/org/apache/tools/ant/ProjectHelperRepository.java
+++ b/src/main/org/apache/tools/ant/ProjectHelperRepository.java
@@ -32,6 +32,7 @@ import java.util.stream.Stream;
import org.apache.tools.ant.helper.ProjectHelper2;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.util.LoaderUtils;
+import org.apache.tools.ant.util.StreamUtils;
/**
* Repository of {@link ProjectHelper} found in the classpath or via
@@ -77,8 +78,7 @@ public class ProjectHelperRepository {
private void collectProjectHelpers() {
// First, try the system property
- Constructor extends ProjectHelper> projectHelper = getProjectHelperBySystemProperty();
- registerProjectHelper(projectHelper);
+ registerProjectHelper(getProjectHelperBySystemProperty());
// A JDK1.3 'service' (like in JAXP). That will plug a helper
// automatically if in CLASSPATH, with the right META-INF/services.
@@ -88,17 +88,14 @@ public class ProjectHelperRepository {
for (URL resource : Collections.list(classLoader.getResources(ProjectHelper.SERVICE_ID))) {
URLConnection conn = resource.openConnection();
conn.setUseCaches(false);
- projectHelper =
- getProjectHelperByService(conn.getInputStream());
- registerProjectHelper(projectHelper);
+ registerProjectHelper(getProjectHelperByService(conn.getInputStream()));
}
}
InputStream systemResource =
ClassLoader.getSystemResourceAsStream(ProjectHelper.SERVICE_ID);
if (systemResource != null) {
- projectHelper = getProjectHelperByService(systemResource);
- registerProjectHelper(projectHelper);
+ registerProjectHelper(getProjectHelperByService(systemResource));
}
} catch (Exception e) {
System.err.println("Unable to load ProjectHelper from service "
@@ -250,20 +247,19 @@ public class ProjectHelperRepository {
* @return the first ProjectHelper that fit the requirement (never null
).
*/
public ProjectHelper getProjectHelperForBuildFile(Resource buildFile) throws BuildException {
- for (Iterator it = getHelpers(); it.hasNext();) {
- ProjectHelper helper = it.next();
- if (helper.canParseBuildFile(buildFile)) {
- if (DEBUG) {
- System.out.println("ProjectHelper "
- + helper.getClass().getName()
- + " selected for the build file "
- + buildFile);
- }
- return helper;
- }
+ ProjectHelper ph = StreamUtils.iteratorAsStream(getHelpers())
+ .filter(helper -> helper.canParseBuildFile(buildFile))
+ .findFirst().orElse(null);
+
+ if (ph == null) {
+ throw new BuildException("BUG: at least the ProjectHelper2 should "
+ + "have supported the file " + buildFile);
+ }
+ if (DEBUG) {
+ System.out.println("ProjectHelper " + ph.getClass().getName()
+ + " selected for the build file " + buildFile);
}
- throw new BuildException("BUG: at least the ProjectHelper2 should "
- + "have supported the file " + buildFile);
+ return ph;
}
/**
@@ -274,20 +270,19 @@ public class ProjectHelperRepository {
* @return the first ProjectHelper that fit the requirement (never null
).
*/
public ProjectHelper getProjectHelperForAntlib(Resource antlib) throws BuildException {
- for (Iterator it = getHelpers(); it.hasNext();) {
- ProjectHelper helper = it.next();
- if (helper.canParseAntlibDescriptor(antlib)) {
- if (DEBUG) {
- System.out.println("ProjectHelper "
- + helper.getClass().getName()
- + " selected for the antlib "
- + antlib);
- }
- return helper;
- }
+ ProjectHelper ph = StreamUtils.iteratorAsStream(getHelpers())
+ .filter(helper -> helper.canParseAntlibDescriptor(antlib))
+ .findFirst().orElse(null);
+
+ if (ph == null) {
+ throw new BuildException("BUG: at least the ProjectHelper2 should "
+ + "have supported the file " + antlib);
+ }
+ if (DEBUG) {
+ System.out.println("ProjectHelper " + ph.getClass().getName()
+ + " selected for the antlib " + antlib);
}
- throw new BuildException("BUG: at least the ProjectHelper2 should "
- + "have supported the file " + antlib);
+ return ph;
}
/**
diff --git a/src/main/org/apache/tools/ant/XmlLogger.java b/src/main/org/apache/tools/ant/XmlLogger.java
index 789b9fd1a..96cfc68e2 100644
--- a/src/main/org/apache/tools/ant/XmlLogger.java
+++ b/src/main/org/apache/tools/ant/XmlLogger.java
@@ -24,7 +24,6 @@ import java.io.PrintStream;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
-import java.util.Collections;
import java.util.Hashtable;
import java.util.Stack;
@@ -32,6 +31,7 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.tools.ant.util.DOMElementWriter;
+import org.apache.tools.ant.util.StreamUtils;
import org.apache.tools.ant.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -345,7 +345,8 @@ public class XmlLogger implements BuildLogger {
if (element != null) {
return element;
}
- return Collections.list(tasks.keys()).stream().filter(UnknownElement.class::isInstance)
+ return StreamUtils.enumerationAsStream(tasks.keys())
+ .filter(UnknownElement.class::isInstance)
.filter(key -> ((UnknownElement) key).getTask() == task).findFirst()
.map(key -> tasks.get(key)).orElse(null);
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java
index b09448c76..f9079ab7e 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Ant.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java
@@ -25,7 +25,6 @@ import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -33,7 +32,6 @@ import java.util.Set;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Main;
@@ -197,10 +195,7 @@ public class Ant extends Task {
private void initializeProject() {
newProject.setInputHandler(getProject().getInputHandler());
- Iterator iter = getBuildListeners();
- while (iter.hasNext()) {
- newProject.addBuildListener(iter.next());
- }
+ getProject().getBuildListeners().forEach(bl -> newProject.addBuildListener(bl));
if (output != null) {
File outfile;
@@ -748,13 +743,6 @@ public class Ant extends Task {
return newProject;
}
- /**
- * @since Ant 1.6.2
- */
- private Iterator getBuildListeners() {
- return getProject().getBuildListeners().iterator();
- }
-
/**
* Helper class that implements the nested <reference>
* element of <ant> and <antcall>.
diff --git a/src/main/org/apache/tools/ant/taskdefs/DependSet.java b/src/main/org/apache/tools/ant/taskdefs/DependSet.java
index 6fb946337..45c82749d 100644
--- a/src/main/org/apache/tools/ant/taskdefs/DependSet.java
+++ b/src/main/org/apache/tools/ant/taskdefs/DependSet.java
@@ -38,6 +38,7 @@ import org.apache.tools.ant.types.resources.comparators.Reverse;
import org.apache.tools.ant.types.resources.selectors.Exists;
import org.apache.tools.ant.types.resources.selectors.Not;
import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
+import org.apache.tools.ant.util.StreamUtils;
/**
* Examines and removes out of date target files. If any of the target files
@@ -263,18 +264,8 @@ public class DependSet extends MatchingTask {
}
private Resource getXest(ResourceCollection rc, ResourceComparator c) {
- Iterator i = rc.iterator();
- if (!i.hasNext()) {
- return null;
- }
- Resource xest = i.next();
- while (i.hasNext()) {
- Resource next = i.next();
- if (c.compare(xest, next) < 0) {
- xest = next;
- }
- }
- return xest;
+ return StreamUtils.iteratorAsStream(rc.iterator())
+ .min(c::compare).orElse(null);
}
private Resource getOldest(ResourceCollection rc) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/Execute.java b/src/main/org/apache/tools/ant/taskdefs/Execute.java
index f9b2ebe27..913ba36a4 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Execute.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Execute.java
@@ -28,7 +28,6 @@ import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
@@ -177,9 +176,7 @@ public class Execute {
@Deprecated
public static synchronized Vector getProcEnvironment() {
Vector v = new Vector<>();
- for (Entry entry : getEnvironmentVariables().entrySet()) {
- v.add(entry.getKey() + "=" + entry.getValue());
- }
+ getEnvironmentVariables().forEach((key, value) -> v.add(key + "=" + value));
return v;
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/Expand.java b/src/main/org/apache/tools/ant/taskdefs/Expand.java
index 82c80f488..3b62ea820 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Expand.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Expand.java
@@ -24,8 +24,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
-import java.util.Collections;
import java.util.Date;
+import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -189,7 +189,9 @@ public class Expand extends Task {
}
try (ZipFile zf = new ZipFile(srcF, encoding, scanForUnicodeExtraFields)) {
boolean empty = true;
- for (ZipEntry ze : Collections.list(zf.getEntries())) {
+ Enumeration entries = zf.getEntries();
+ while (entries.hasMoreElements()) {
+ ZipEntry ze = entries.nextElement();
empty = false;
InputStream is = null;
log("extracting " + ze.getName(), Project.MSG_DEBUG);
diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java
index 64c658d3f..f83532df2 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Jar.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java
@@ -55,6 +55,7 @@ import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.ZipFileSet;
import org.apache.tools.ant.types.spi.Service;
import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.util.StreamUtils;
import org.apache.tools.zip.JarMarker;
import org.apache.tools.zip.ZipExtraField;
import org.apache.tools.zip.ZipOutputStream;
@@ -321,18 +322,17 @@ public class Jar extends Zip {
*/
private Manifest getManifestFromJar(File jarFile) throws IOException {
try (ZipFile zf = new ZipFile(jarFile)) {
-
// must not use getEntry as "well behaving" applications
// must accept the manifest in any capitalization
- for (ZipEntry ze : Collections.list(zf.entries())) {
- if (MANIFEST_NAME.equalsIgnoreCase(ze.getName())) {
- try (InputStreamReader isr =
- new InputStreamReader(zf.getInputStream(ze), "UTF-8")) {
- return getManifest(isr);
- }
- }
+ ZipEntry ze = StreamUtils.enumerationAsStream(zf.entries())
+ .filter(entry -> MANIFEST_NAME.equalsIgnoreCase(entry.getName()))
+ .findFirst().orElse(null);
+ if (ze == null) {
+ return null;
+ }
+ try (InputStreamReader isr = new InputStreamReader(zf.getInputStream(ze), "UTF-8")) {
+ return getManifest(isr);
}
- return null;
}
}
@@ -351,7 +351,7 @@ public class Jar extends Zip {
private boolean jarHasIndex(File jarFile) throws IOException {
try (ZipFile zf = new ZipFile(jarFile)) {
- return Collections.list(zf.entries()).stream()
+ return StreamUtils.enumerationAsStream(zf.entries())
.anyMatch(ze -> INDEX_NAME.equalsIgnoreCase(ze.getName()));
}
}
@@ -524,9 +524,8 @@ public class Jar extends Zip {
private void writeManifest(ZipOutputStream zOut, Manifest manifest)
throws IOException {
- for (String warning : Collections.list(manifest.getWarnings())) {
- log("Manifest warning: " + warning, Project.MSG_WARN);
- }
+ StreamUtils.enumerationAsStream(manifest.getWarnings())
+ .forEach(warning -> log("Manifest warning: " + warning, Project.MSG_WARN));
zipDir((Resource) null, zOut, "META-INF/", ZipFileSet.DEFAULT_DIR_MODE,
JAR_MARKER);
@@ -1064,7 +1063,7 @@ public class Jar extends Zip {
throws IOException {
try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(file, "utf-8")) {
Set dirSet = new HashSet<>();
- for (org.apache.tools.zip.ZipEntry ze : Collections.list(zf.getEntries())) {
+ StreamUtils.enumerationAsStream(zf.getEntries()).forEach(ze -> {
String name = ze.getName();
if (ze.isDirectory()) {
dirSet.add(name);
@@ -1077,7 +1076,7 @@ public class Jar extends Zip {
// well.
dirSet.add(name.substring(0, name.lastIndexOf('/') + 1));
}
- }
+ });
dirs.addAll(dirSet);
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/Manifest.java b/src/main/org/apache/tools/ant/taskdefs/Manifest.java
index 70eb4fcca..ef6dc882c 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Manifest.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Manifest.java
@@ -26,7 +26,6 @@ import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
@@ -39,6 +38,7 @@ import java.util.stream.Collectors;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.util.StreamUtils;
/**
* Holds the data of a jar manifest.
@@ -676,7 +676,7 @@ public class Manifest {
public Object clone() {
Section cloned = new Section();
cloned.setName(name);
- Collections.list(getAttributeKeys()).stream()
+ StreamUtils.enumerationAsStream(getAttributeKeys())
.map(key -> new Attribute(getAttribute(key).getName(),
getAttribute(key).getValue())).forEach(cloned::storeAttribute);
return cloned;
@@ -932,8 +932,7 @@ public class Manifest {
for (String sectionName : Collections.list(other.getSectionNames())) {
Section ourSection = sections.get(sectionName);
- Section otherSection
- = other.sections.get(sectionName);
+ Section otherSection = other.sections.get(sectionName);
if (ourSection == null) {
if (otherSection != null) {
addConfiguredSection((Section) otherSection.clone());
@@ -1020,7 +1019,7 @@ public class Manifest {
*/
public Enumeration getWarnings() {
// create a vector and add in the warnings for the main section
- List warnings = new ArrayList<>(Collections.list(mainSection.getWarnings()));
+ List warnings = Collections.list(mainSection.getWarnings());
// add in the warnings for all the sections
sections.values().stream().map(section -> Collections.list(section.getWarnings()))
diff --git a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java
index 69fa7870f..d90a04952 100644
--- a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java
@@ -25,12 +25,12 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
-import java.util.Collections;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.util.StreamUtils;
/**
* Creates a manifest file for inclusion in a JAR, Ant task wrapper
@@ -112,7 +112,7 @@ public class ManifestTask extends Task {
*/
public void addConfiguredSection(Manifest.Section section)
throws ManifestException {
- Collections.list(section.getAttributeKeys()).stream()
+ StreamUtils.enumerationAsStream(section.getAttributeKeys())
.map(section::getAttribute).forEach(this::checkAttribute);
nestedManifest.addConfiguredSection(section);
}
@@ -241,7 +241,7 @@ public class ManifestTask extends Task {
}
// look for and print warnings
- Collections.list(nestedManifest.getWarnings())
+ StreamUtils.enumerationAsStream(nestedManifest.getWarnings())
.forEach(e -> log("Manifest warning: " + e, Project.MSG_WARN));
try {
if ("update".equals(mode.getValue()) && manifestFile.exists()) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/Recorder.java b/src/main/org/apache/tools/ant/taskdefs/Recorder.java
index cb9b4d88a..8a7d8213a 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Recorder.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Recorder.java
@@ -307,13 +307,9 @@ public class Recorder extends Task implements SubBuildListener {
*/
@SuppressWarnings("unchecked")
private void cleanup() {
- Hashtable entries
- = (Hashtable) recorderEntries.clone();
- for (Map.Entry entry : entries.entrySet()) {
- if (entry.getValue().getProject() == getProject()) {
- recorderEntries.remove(entry.getKey());
- }
- }
+ ((Hashtable) recorderEntries.clone()).entrySet().stream()
+ .filter(entry -> entry.getValue().getProject() == getProject())
+ .forEach(entry -> recorderEntries.remove(entry.getKey()));
getProject().removeBuildListener(this);
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java
index 5adcd9969..2cfaa8bb1 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Replace.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java
@@ -45,6 +45,7 @@ import org.apache.tools.ant.types.resources.FileProvider;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.util.StreamUtils;
/**
* Replaces all occurrences of one or more string tokens with given
@@ -511,14 +512,12 @@ public class Replace extends MatchingTask {
try {
if (replaceFilterResource != null) {
- Properties props = getProperties(replaceFilterResource);
- Iterator