http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/FileResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/FileResource.java b/src/main/org/apache/tools/ant/types/resources/FileResource.java index d427888..a1c410a 100644 --- a/src/main/org/apache/tools/ant/types/resources/FileResource.java +++ b/src/main/org/apache/tools/ant/types/resources/FileResource.java @@ -105,9 +105,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Get the file represented by this FileResource. * @return the File. */ + @Override public File getFile() { if (isReference()) { - return ((FileResource) getCheckedRef()).getFile(); + return getCheckedRef().getFile(); } dieOnCircularReference(); synchronized (this) { @@ -138,7 +139,7 @@ public class FileResource extends Resource implements Touchable, FileProvider, */ public File getBaseDir() { if (isReference()) { - return ((FileResource) getCheckedRef()).getBaseDir(); + return getCheckedRef().getBaseDir(); } dieOnCircularReference(); return baseDir; @@ -148,6 +149,7 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Overrides the super version. * @param r the Reference to set. */ + @Override public void setRefid(Reference r) { if (file != null || baseDir != null) { throw tooManyAttributes(); @@ -161,9 +163,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * only will be returned. * @return the name of this resource. */ + @Override public String getName() { if (isReference()) { - return ((Resource) getCheckedRef()).getName(); + return getCheckedRef().getName(); } File b = getBaseDir(); return b == null ? getNotNullFile().getName() @@ -174,8 +177,9 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Learn whether this file exists. * @return true if this resource exists. */ + @Override public boolean isExists() { - return isReference() ? ((Resource) getCheckedRef()).isExists() + return isReference() ? getCheckedRef().isExists() : getNotNullFile().exists(); } @@ -183,9 +187,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Get the modification time in milliseconds since 01.01.1970 . * @return 0 if the resource does not exist. */ + @Override public long getLastModified() { return isReference() - ? ((Resource) getCheckedRef()).getLastModified() + ? getCheckedRef().getLastModified() : getNotNullFile().lastModified(); } @@ -193,8 +198,9 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Learn whether the resource is a directory. * @return boolean flag indicating if the resource is a directory. */ + @Override public boolean isDirectory() { - return isReference() ? ((Resource) getCheckedRef()).isDirectory() + return isReference() ? getCheckedRef().isDirectory() : getNotNullFile().isDirectory(); } @@ -202,8 +208,9 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Get the size of this Resource. * @return the size, as a long, 0 if the Resource does not exist. */ + @Override public long getSize() { - return isReference() ? ((Resource) getCheckedRef()).getSize() + return isReference() ? getCheckedRef().getSize() : getNotNullFile().length(); } @@ -212,9 +219,9 @@ public class FileResource extends Resource implements Touchable, FileProvider, * @return an InputStream object. * @throws IOException if an error occurs. */ + @Override public InputStream getInputStream() throws IOException { - return isReference() - ? ((Resource) getCheckedRef()).getInputStream() + return isReference() ? getCheckedRef().getInputStream() : Files.newInputStream(getNotNullFile().toPath()); } @@ -226,9 +233,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * @throws UnsupportedOperationException if OutputStreams are not * supported for this Resource type. */ + @Override public OutputStream getOutputStream() throws IOException { if (isReference()) { - return ((FileResource) getCheckedRef()).getOutputStream(); + return getCheckedRef().getOutputStream(); } return getOutputStream(false); } @@ -236,9 +244,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, /** * {@inheritDoc} */ + @Override public OutputStream getAppendOutputStream() throws IOException { if (isReference()) { - return ((FileResource) getCheckedRef()).getAppendOutputStream(); + return getCheckedRef().getAppendOutputStream(); } return getOutputStream(true); } @@ -264,9 +273,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * @return a negative integer, zero, or a positive integer as this FileResource * is less than, equal to, or greater than the specified Resource. */ + @Override public int compareTo(Resource another) { if (isReference()) { - return ((Resource) getCheckedRef()).compareTo(another); + return getCheckedRef().compareTo(another); } if (this.equals(another)) { return 0; @@ -293,6 +303,7 @@ public class FileResource extends Resource implements Touchable, FileProvider, * @param another the other Object to compare. * @return true if another is a FileResource representing the same file. */ + @Override public boolean equals(Object another) { if (this == another) { return true; @@ -313,6 +324,7 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Get the hash code for this Resource. * @return hash code as int. */ + @Override public int hashCode() { if (isReference()) { return getCheckedRef().hashCode(); @@ -324,6 +336,7 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Get the string representation of this Resource. * @return this FileResource formatted as a String. */ + @Override public String toString() { if (isReference()) { return getCheckedRef().toString(); @@ -339,9 +352,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Fulfill the ResourceCollection contract. * @return whether this Resource is a FileResource. */ + @Override public boolean isFilesystemOnly() { if (isReference()) { - return ((FileResource) getCheckedRef()).isFilesystemOnly(); + return getCheckedRef().isFilesystemOnly(); } dieOnCircularReference(); return true; @@ -351,9 +365,10 @@ public class FileResource extends Resource implements Touchable, FileProvider, * Implement the Touchable interface. * @param modTime new last modification time. */ + @Override public void touch(long modTime) { if (isReference()) { - ((FileResource) getCheckedRef()).touch(modTime); + getCheckedRef().touch(modTime); return; } if (!getNotNullFile().setLastModified(modTime)) { @@ -382,6 +397,7 @@ public class FileResource extends Resource implements Touchable, FileProvider, * @throws BuildException if desired * @since Ant1.8 */ + @Override public Resource getResource(String path) { File newfile = FILE_UTILS.resolveFile(getFile(), path); FileResource fileResource = new FileResource(newfile); @@ -390,4 +406,9 @@ public class FileResource extends Resource implements Touchable, FileProvider, } return fileResource; } + + @Override + protected FileResource getCheckedRef() { + return (FileResource) super.getCheckedRef(); + } }
http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/FileResourceIterator.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/FileResourceIterator.java b/src/main/org/apache/tools/ant/types/resources/FileResourceIterator.java index 6d8849c..2584117 100644 --- a/src/main/org/apache/tools/ant/types/resources/FileResourceIterator.java +++ b/src/main/org/apache/tools/ant/types/resources/FileResourceIterator.java @@ -114,6 +114,7 @@ public class FileResourceIterator implements Iterator<Resource> { * Find out whether this FileResourceIterator has more elements. * @return whether there are more Resources to iterate over. */ + @Override public boolean hasNext() { return pos < files.length; } @@ -122,6 +123,7 @@ public class FileResourceIterator implements Iterator<Resource> { * Get the next element from this FileResourceIterator. * @return the next Object. */ + @Override public Resource next() { return nextResource(); } @@ -129,6 +131,7 @@ public class FileResourceIterator implements Iterator<Resource> { /** * Not implemented. */ + @Override public void remove() { throw new UnsupportedOperationException(); } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Files.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Files.java b/src/main/org/apache/tools/ant/types/resources/Files.java index 521bcc8..00e5d4d 100644 --- a/src/main/org/apache/tools/ant/types/resources/Files.java +++ b/src/main/org/apache/tools/ant/types/resources/Files.java @@ -21,6 +21,7 @@ import java.io.File; import java.util.Collections; import java.util.Iterator; import java.util.Vector; +import java.util.stream.Collectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; @@ -39,11 +40,8 @@ import org.apache.tools.ant.types.selectors.FileSelector; public class Files extends AbstractSelectorContainer implements ResourceCollection { - private static final Iterator<Resource> EMPTY_ITERATOR - = Collections.<Resource>emptySet().iterator(); - private PatternSet defaultPatterns = new PatternSet(); - private Vector<PatternSet> additionalPatterns = new Vector<PatternSet>(); + private Vector<PatternSet> additionalPatterns = new Vector<>(); private boolean useDefaultExcludes = true; private boolean caseSensitive = true; @@ -82,6 +80,7 @@ public class Files extends AbstractSelectorContainer * @param r the <code>Reference</code> to use. * @throws BuildException if there is a problem. */ + @Override public void setRefid(Reference r) throws BuildException { if (hasPatterns(defaultPatterns)) { throw tooManyAttributes(); @@ -258,7 +257,7 @@ public class Files extends AbstractSelectorContainer * @return the defaultexclusions value. */ public synchronized boolean getDefaultexcludes() { - return (isReference()) + return isReference() ? getRef().getDefaultexcludes() : useDefaultExcludes; } @@ -280,7 +279,7 @@ public class Files extends AbstractSelectorContainer * collection is case-sensitive. */ public synchronized boolean isCaseSensitive() { - return (isReference()) + return isReference() ? getRef().isCaseSensitive() : caseSensitive; } @@ -302,7 +301,7 @@ public class Files extends AbstractSelectorContainer * should be followed. */ public synchronized boolean isFollowSymlinks() { - return (isReference()) + return isReference() ? getRef().isFollowSymlinks() : followSymlinks; } @@ -310,6 +309,7 @@ public class Files extends AbstractSelectorContainer * Fulfill the ResourceCollection contract. * @return an Iterator of Resources. */ + @Override public synchronized Iterator<Resource> iterator() { if (isReference()) { return getRef().iterator(); @@ -319,7 +319,7 @@ public class Files extends AbstractSelectorContainer int fct = ds.getIncludedFilesCount(); int dct = ds.getIncludedDirsCount(); if (fct + dct == 0) { - return EMPTY_ITERATOR; + return Collections.emptyIterator(); } FileResourceIterator result = new FileResourceIterator(getProject()); if (fct > 0) { @@ -335,6 +335,7 @@ public class Files extends AbstractSelectorContainer * Fulfill the ResourceCollection contract. * @return number of elements as int. */ + @Override public synchronized int size() { if (isReference()) { return getRef().size(); @@ -354,15 +355,8 @@ public class Files extends AbstractSelectorContainer return getRef().hasPatterns(); } dieOnCircularReference(); - if (hasPatterns(defaultPatterns)) { - return true; - } - for (PatternSet patternSet : additionalPatterns) { - if (hasPatterns(patternSet)) { - return true; - } - } - return false; + return hasPatterns(defaultPatterns) + || additionalPatterns.stream().anyMatch(this::hasPatterns); } /** @@ -370,6 +364,7 @@ public class Files extends AbstractSelectorContainer * * @param selector the new <code>FileSelector</code> to add. */ + @Override public synchronized void appendSelector(FileSelector selector) { if (isReference()) { throw noChildrenAllowed(); @@ -382,22 +377,13 @@ public class Files extends AbstractSelectorContainer * Format this Files collection as a String. * @return a descriptive <code>String</code>. */ + @Override public String toString() { if (isReference()) { return getRef().toString(); } - Iterator<Resource> i = iterator(); - if (!i.hasNext()) { - return ""; - } - StringBuffer sb = new StringBuffer(); - while (i.hasNext()) { - if (sb.length() > 0) { - sb.append(File.pathSeparatorChar); - } - sb.append(i.next()); - } - return sb.toString(); + return isEmpty() ? "" : stream().map(Object::toString) + .collect(Collectors.joining(File.pathSeparator)); } /** @@ -405,7 +391,8 @@ public class Files extends AbstractSelectorContainer * (the list of selectors is a shallow clone of this instance's list). * @return a cloned Object. */ - public synchronized Object clone() { + @Override + public synchronized Files clone() { if (isReference()) { return getRef().clone(); } @@ -451,11 +438,7 @@ public class Files extends AbstractSelectorContainer dieOnCircularReference(); PatternSet ps = new PatternSet(); ps.append(defaultPatterns, p); - final int count = additionalPatterns.size(); - for (int i = 0; i < count; i++) { - Object o = additionalPatterns.elementAt(i); - ps.append((PatternSet) o, p); - } + additionalPatterns.forEach(pat -> ps.append(pat, p)); return ps; } @@ -464,6 +447,7 @@ public class Files extends AbstractSelectorContainer * @return true indicating that all elements of a Files collection * will be FileResources. */ + @Override public boolean isFilesystemOnly() { return true; } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/First.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/First.java b/src/main/org/apache/tools/ant/types/resources/First.java index ea9e7d0..05cf1e2 100644 --- a/src/main/org/apache/tools/ant/types/resources/First.java +++ b/src/main/org/apache/tools/ant/types/resources/First.java @@ -17,10 +17,8 @@ */ package org.apache.tools.ant.types.resources; -import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; -import java.util.List; +import java.util.stream.Collectors; import org.apache.tools.ant.types.Resource; @@ -35,14 +33,10 @@ public class First extends SizeLimitCollection { * Take the first <code>count</code> elements. * @return a Collection of Resources. */ + @Override protected Collection<Resource> getCollection() { - int ct = getValidCount(); - Iterator<Resource> iter = getResourceCollection().iterator(); - List<Resource> al = new ArrayList<Resource>(ct); - for (int i = 0; i < ct && iter.hasNext(); i++) { - al.add(iter.next()); - } - return al; + return getResourceCollection().stream().limit(getValidCount()) + .collect(Collectors.toList()); } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/GZipResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/GZipResource.java b/src/main/org/apache/tools/ant/types/resources/GZipResource.java index 3f95a69..e37b539 100644 --- a/src/main/org/apache/tools/ant/types/resources/GZipResource.java +++ b/src/main/org/apache/tools/ant/types/resources/GZipResource.java @@ -23,6 +23,8 @@ import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; +import org.apache.tools.ant.types.ResourceCollection; + /** * A GZip compressed resource. * @@ -41,7 +43,7 @@ public class GZipResource extends CompressedResource { * Constructor with another resource to wrap. * @param other the resource to wrap. */ - public GZipResource(org.apache.tools.ant.types.ResourceCollection other) { + public GZipResource(ResourceCollection other) { super(other); } @@ -51,6 +53,7 @@ public class GZipResource extends CompressedResource { * @return the wrapped stream. * @throws IOException if there is a problem. */ + @Override protected InputStream wrapStream(InputStream in) throws IOException { return new GZIPInputStream(in); } @@ -61,7 +64,8 @@ public class GZipResource extends CompressedResource { * @return the wrapped stream. * @throws IOException if there is a problem. */ - protected OutputStream wrapStream(OutputStream out) throws IOException { + @Override + protected OutputStream wrapStream(OutputStream out) throws IOException { return new GZIPOutputStream(out); } @@ -69,6 +73,7 @@ public class GZipResource extends CompressedResource { * Get the name of the compression method. * @return the string "GZip". */ + @Override protected String getCompressionName() { return "GZip"; } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Intersect.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Intersect.java b/src/main/org/apache/tools/ant/types/resources/Intersect.java index cdbeed0..667d259 100644 --- a/src/main/org/apache/tools/ant/types/resources/Intersect.java +++ b/src/main/org/apache/tools/ant/types/resources/Intersect.java @@ -22,11 +22,14 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.ResourceCollection; + /** * ResourceCollection representing the intersection * of multiple nested ResourceCollections. @@ -38,27 +41,23 @@ public class Intersect extends BaseResourceCollectionContainer { * Calculate the intersection of the nested ResourceCollections. * @return a Collection of Resources. */ + @Override protected Collection<Resource> getCollection() { List<ResourceCollection> rcs = getResourceCollections(); int size = rcs.size(); if (size < 2) { - throw new BuildException("The intersection of " + size - + " resource collection" + ((size == 1) ? "" : "s") - + " is undefined."); + throw new BuildException( + "The intersection of %d resource %s is undefined.", size, + size == 1 ? "collection" : "collections"); } + + final Function<ResourceCollection, Set<Resource>> toSet = + c -> c.stream().collect(Collectors.toSet()); + Iterator<ResourceCollection> rc = rcs.iterator(); - Set<Resource> s = new LinkedHashSet<Resource>(collect(rc.next())); - while (rc.hasNext()) { - s.retainAll(collect(rc.next())); - } + Set<Resource> s = new LinkedHashSet<>(toSet.apply(rc.next())); + rc.forEachRemaining(c -> s.retainAll(toSet.apply(c))); return s; } - private Set<Resource> collect(ResourceCollection rc) { - Set<Resource> result = new LinkedHashSet<Resource>(); - for (Resource r : rc) { - result.add(r); - } - return result; - } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java b/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java index e8c8f02..9ce721b 100644 --- a/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java +++ b/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java @@ -36,6 +36,7 @@ public class JavaConstantResource extends AbstractClasspathResource { * @return an open input stream for the resource * @throws IOException if an error occurs. */ + @Override protected InputStream openInputStream(ClassLoader cl) throws IOException { String constant = getName(); if (constant == null) { http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/JavaResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/JavaResource.java b/src/main/org/apache/tools/ant/types/resources/JavaResource.java index a927d3f..d6467e8 100644 --- a/src/main/org/apache/tools/ant/types/resources/JavaResource.java +++ b/src/main/org/apache/tools/ant/types/resources/JavaResource.java @@ -79,20 +79,20 @@ public class JavaResource extends AbstractClasspathResource * Get the URL represented by this Resource. * @since Ant 1.8.0 */ + @Override public URL getURL() { if (isReference()) { - return ((JavaResource) getCheckedRef()).getURL(); + return getCheckedRef().getURL(); } AbstractClasspathResource.ClassLoaderWithFlag classLoader = getClassLoader(); if (classLoader.getLoader() == null) { return ClassLoader.getSystemResource(getName()); - } else { - try { - return classLoader.getLoader().getResource(getName()); - } finally { - classLoader.cleanup(); - } + } + try { + return classLoader.getLoader().getResource(getName()); + } finally { + classLoader.cleanup(); } } @@ -103,9 +103,10 @@ public class JavaResource extends AbstractClasspathResource * JavaResource is less than, equal to, or greater than the * specified Resource. */ + @Override public int compareTo(Resource another) { if (isReference()) { - return ((Resource) getCheckedRef()).compareTo(another); + return getCheckedRef().compareTo(another); } if (another.getClass().equals(getClass())) { JavaResource otherjr = (JavaResource) another; @@ -138,4 +139,8 @@ public class JavaResource extends AbstractClasspathResource return super.compareTo(another); } + @Override + protected JavaResource getCheckedRef() { + return (JavaResource) super.getCheckedRef(); + } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Last.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Last.java b/src/main/org/apache/tools/ant/types/resources/Last.java index 312271b..575e768 100644 --- a/src/main/org/apache/tools/ant/types/resources/Last.java +++ b/src/main/org/apache/tools/ant/types/resources/Last.java @@ -17,10 +17,9 @@ */ package org.apache.tools.ant.types.resources; -import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; import java.util.List; +import java.util.stream.Collectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; @@ -38,33 +37,29 @@ public class Last extends SizeLimitCollection { * Take the last <code>count</code> elements. * @return a Collection of Resources. */ + @Override protected Collection<Resource> getCollection() { int count = getValidCount(); ResourceCollection rc = getResourceCollection(); - int i = count; - Iterator<Resource> iter = rc.iterator(); int size = rc.size(); - for (; i < size; i++) { - iter.next(); - } + int skip = Math.max(0, size - count); - List<Resource> al = new ArrayList<Resource>(count); - for (; iter.hasNext(); i++) { - al.add(iter.next()); - } - int found = al.size(); + List<Resource> result = + rc.stream().skip(skip).collect(Collectors.toList()); + + int found = result.size(); if (found == count || (size < count && found == size)) { - return al; + return result; } - //mismatch: - String msg = "Resource collection " + rc + " reports size " + size - + " but returns " + i + " elements."; + String msg = String.format( + "Resource collection %s reports size %d but returns %d elements.", + rc, size, found + skip); //size was understated -> too many results; warn and continue: if (found > count) { log(msg, Project.MSG_WARN); - return al.subList(found - count, found); + return result.subList(found - count, found); } //size was overstated; we missed some and are now in error-land: throw new BuildException(msg); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/LazyResourceCollectionWrapper.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/LazyResourceCollectionWrapper.java b/src/main/org/apache/tools/ant/types/resources/LazyResourceCollectionWrapper.java index 4f9acd3..e7b2fca 100644 --- a/src/main/org/apache/tools/ant/types/resources/LazyResourceCollectionWrapper.java +++ b/src/main/org/apache/tools/ant/types/resources/LazyResourceCollectionWrapper.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; +import java.util.function.Supplier; import org.apache.tools.ant.types.Resource; @@ -32,24 +33,23 @@ public class LazyResourceCollectionWrapper extends AbstractResourceCollectionWrapper { /** List of cached resources */ - private final List<Resource> cachedResources = new ArrayList<Resource>(); + private final List<Resource> cachedResources = new ArrayList<>(); - private FilteringIterator filteringIterator; + private Iterator<Resource> filteringIterator; + + private final Supplier<Iterator<Resource>> filteringIteratorSupplier = + () -> new FilteringIterator(getResourceCollection().iterator()); @Override protected Iterator<Resource> createIterator() { - Iterator<Resource> iterator; if (isCache()) { if (filteringIterator == null) { // no worry of thread safety here, see function's contract - filteringIterator = new FilteringIterator( - getResourceCollection().iterator()); + filteringIterator = filteringIteratorSupplier.get(); } - iterator = new CachedIterator(filteringIterator); - } else { - iterator = new FilteringIterator(getResourceCollection().iterator()); + return new CachedIterator(filteringIterator); } - return iterator; + return filteringIteratorSupplier.get(); } @Override @@ -84,10 +84,11 @@ public class LazyResourceCollectionWrapper extends protected final Iterator<Resource> it; - public FilteringIterator(final Iterator<Resource> it) { + FilteringIterator(final Iterator<Resource> it) { this.it = it; } + @Override public boolean hasNext() { if (ended) { return false; @@ -105,6 +106,7 @@ public class LazyResourceCollectionWrapper extends return true; } + @Override public Resource next() { if (!hasNext()) { throw new UnsupportedOperationException(); @@ -114,9 +116,6 @@ public class LazyResourceCollectionWrapper extends return r; } - public void remove() { - throw new UnsupportedOperationException(); - } } /** @@ -125,7 +124,7 @@ public class LazyResourceCollectionWrapper extends */ private class CachedIterator implements Iterator<Resource> { - int cusrsor = 0; + int cursor = 0; private final Iterator<Resource> it; @@ -140,10 +139,11 @@ public class LazyResourceCollectionWrapper extends this.it = it; } + @Override public boolean hasNext() { synchronized (cachedResources) { // have we already cached the next entry ? - if (cachedResources.size() > cusrsor) { + if (cachedResources.size() > cursor) { return true; } // does the wrapped iterator any more resource ? @@ -157,6 +157,7 @@ public class LazyResourceCollectionWrapper extends return true; } + @Override public Resource next() { // first check that we have some to deliver if (!hasNext()) { @@ -165,10 +166,11 @@ public class LazyResourceCollectionWrapper extends synchronized (cachedResources) { // return the cached entry as hasNext should have put one for // this iterator - return cachedResources.get(cusrsor++); + return cachedResources.get(cursor++); } } + @Override public void remove() { throw new UnsupportedOperationException(); } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/LogOutputResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/LogOutputResource.java b/src/main/org/apache/tools/ant/types/resources/LogOutputResource.java index cd19c9c..1e91a88 100644 --- a/src/main/org/apache/tools/ant/types/resources/LogOutputResource.java +++ b/src/main/org/apache/tools/ant/types/resources/LogOutputResource.java @@ -55,6 +55,7 @@ public class LogOutputResource extends Resource implements Appendable { /** * {@inheritDoc} */ + @Override public OutputStream getAppendOutputStream() throws IOException { return outputStream; } @@ -62,6 +63,7 @@ public class LogOutputResource extends Resource implements Appendable { /** * {@inheritDoc} */ + @Override public OutputStream getOutputStream() throws IOException { return outputStream; } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/MappedResourceCollection.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/MappedResourceCollection.java b/src/main/org/apache/tools/ant/types/resources/MappedResourceCollection.java index 2f1a926..13d84d1 100644 --- a/src/main/org/apache/tools/ant/types/resources/MappedResourceCollection.java +++ b/src/main/org/apache/tools/ant/types/resources/MappedResourceCollection.java @@ -18,10 +18,11 @@ package org.apache.tools.ant.types.resources; import java.io.File; -import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Stack; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; @@ -121,10 +122,10 @@ public class MappedResourceCollection /** * {@inheritDoc} */ + @Override public boolean isFilesystemOnly() { if (isReference()) { - return ((MappedResourceCollection) getCheckedRef()) - .isFilesystemOnly(); + return getCheckedRef().isFilesystemOnly(); } checkInitialized(); return false; @@ -133,9 +134,10 @@ public class MappedResourceCollection /** * {@inheritDoc} */ + @Override public int size() { if (isReference()) { - return ((MappedResourceCollection) getCheckedRef()).size(); + return getCheckedRef().size(); } checkInitialized(); return cacheCollection().size(); @@ -144,9 +146,10 @@ public class MappedResourceCollection /** * {@inheritDoc} */ + @Override public Iterator<Resource> iterator() { if (isReference()) { - return ((MappedResourceCollection) getCheckedRef()).iterator(); + return getCheckedRef().iterator(); } checkInitialized(); return cacheCollection().iterator(); @@ -156,6 +159,7 @@ public class MappedResourceCollection * Overrides the base version. * @param r the Reference to set. */ + @Override public void setRefid(Reference r) { if (nested != null || mapper != null) { throw tooManyAttributes(); @@ -167,7 +171,8 @@ public class MappedResourceCollection * Implement clone. The nested resource collection and mapper are copied. * @return a cloned instance. */ - public Object clone() { + @Override + public MappedResourceCollection clone() { try { MappedResourceCollection c = (MappedResourceCollection) super.clone(); @@ -187,6 +192,7 @@ public class MappedResourceCollection * @param p the project to use to dereference the references. * @throws BuildException on error. */ + @Override protected synchronized void dieOnCircularReference(Stack<Object> stk, Project p) throws BuildException { if (isChecked()) { @@ -208,8 +214,9 @@ public class MappedResourceCollection private void checkInitialized() { if (nested == null) { - throw new BuildException("A nested resource collection element is" - + " required", getLocation()); + throw new BuildException( + "A nested resource collection element is required", + getLocation()); } dieOnCircularReference(); } @@ -222,46 +229,36 @@ public class MappedResourceCollection } private Collection<Resource> getCollection() { - Collection<Resource> collected = new ArrayList<Resource>(); FileNameMapper m = - mapper != null ? mapper.getImplementation() : new IdentityMapper(); - for (Resource r : nested) { - if (enableMultipleMappings) { - String[] n = m.mapFileName(r.getName()); - if (n != null) { - for (int i = 0; i < n.length; i++) { - collected.add(new MappedResource(r, - new MergingMapper(n[i])) - ); - } - } - } else { - collected.add(new MappedResource(r, m)); - } + mapper == null ? new IdentityMapper() : mapper.getImplementation(); + + Stream<MappedResource> stream; + if (enableMultipleMappings) { + stream = nested.stream() + .flatMap(r -> Stream.of(m.mapFileName(r.getName())) + .map(MergingMapper::new) + .map(mm -> new MappedResource(r, mm))); + } else { + stream = nested.stream().map(r -> new MappedResource(r, m)); } - return collected; + return stream.collect(Collectors.toList()); } /** * Format this resource collection as a String. * @return a descriptive <code>String</code>. */ + @Override public String toString() { if (isReference()) { return getCheckedRef().toString(); } - Iterator<Resource> i = iterator(); - if (!i.hasNext()) { - return ""; - } - StringBuffer sb = new StringBuffer(); - while (i.hasNext()) { - if (sb.length() > 0) { - sb.append(File.pathSeparatorChar); - } - sb.append(i.next()); - } - return sb.toString(); + return isEmpty() ? "" : stream().map(Object::toString) + .collect(Collectors.joining(File.pathSeparator)); } + @Override + protected MappedResourceCollection getCheckedRef() { + return (MappedResourceCollection) super.getCheckedRef(); + } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/MultiRootFileSet.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/MultiRootFileSet.java b/src/main/org/apache/tools/ant/types/resources/MultiRootFileSet.java index d793890..4628bdf 100644 --- a/src/main/org/apache/tools/ant/types/resources/MultiRootFileSet.java +++ b/src/main/org/apache/tools/ant/types/resources/MultiRootFileSet.java @@ -112,21 +112,21 @@ public class MultiRootFileSet extends AbstractFileSet * @return the cloned MultiRootFileSet. */ @Override - public Object clone() { + public MultiRootFileSet clone() { if (isReference()) { return ((MultiRootFileSet) getRef(getProject())).clone(); - } else { - final MultiRootFileSet fs = (MultiRootFileSet) super.clone(); - fs.baseDirs = new ArrayList<File>(baseDirs); - fs.union = null; - return fs; } + final MultiRootFileSet fs = (MultiRootFileSet) super.clone(); + fs.baseDirs = new ArrayList<>(baseDirs); + fs.union = null; + return fs; } /** * Fulfill the ResourceCollection contract. * @return an Iterator of Resources. */ + @Override public Iterator<Resource> iterator() { if (isReference()) { return ((MultiRootFileSet) getRef(getProject())).iterator(); @@ -138,6 +138,7 @@ public class MultiRootFileSet extends AbstractFileSet * Fulfill the ResourceCollection contract. * @return number of elements as int. */ + @Override public int size() { if (isReference()) { return ((MultiRootFileSet) getRef(getProject())).size(); @@ -149,6 +150,7 @@ public class MultiRootFileSet extends AbstractFileSet * Always returns true. * @return true indicating that all elements will be FileResources. */ + @Override public boolean isFilesystemOnly() { return true; } @@ -202,10 +204,12 @@ public class MultiRootFileSet extends AbstractFileSet setDir(dir); } + @Override public boolean isFilesystemOnly() { return true; } + @Override public Iterator<Resource> iterator() { final DirectoryScanner ds = getDirectoryScanner(getProject()); String[] names = type == SetType.file @@ -222,6 +226,7 @@ public class MultiRootFileSet extends AbstractFileSet names); } + @Override public int size() { final DirectoryScanner ds = getDirectoryScanner(getProject()); int count = type == SetType.file http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/PropertyResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/PropertyResource.java b/src/main/org/apache/tools/ant/types/resources/PropertyResource.java index a7cecb4..d72881d 100644 --- a/src/main/org/apache/tools/ant/types/resources/PropertyResource.java +++ b/src/main/org/apache/tools/ant/types/resources/PropertyResource.java @@ -39,6 +39,7 @@ public class PropertyResource extends Resource { = Resource.getMagicNumber("PropertyResource".getBytes()); private static final InputStream UNSET = new InputStream() { + @Override public int read() { return -1; } @@ -66,7 +67,7 @@ public class PropertyResource extends Resource { */ public String getValue() { if (isReference()) { - return ((PropertyResource) getCheckedRef()).getValue(); + return getCheckedRef().getValue(); } Project p = getProject(); return p == null ? null : p.getProperty(getName()); @@ -79,7 +80,7 @@ public class PropertyResource extends Resource { */ public Object getObjectValue() { if (isReference()) { - return ((PropertyResource) getCheckedRef()).getObjectValue(); + return getCheckedRef().getObjectValue(); } Project p = getProject(); return p == null ? null : PropertyHelper.getProperty(p, getName()); @@ -89,6 +90,7 @@ public class PropertyResource extends Resource { * Find out whether this Resource exists. * @return true if the Property is set, false otherwise. */ + @Override public boolean isExists() { if (isReferenceOrProxy()) { return getReferencedOrProxied().isExists(); @@ -101,6 +103,7 @@ public class PropertyResource extends Resource { * @return the size, as a long, 0 if the Resource does not exist (for * compatibility with java.io.File), or UNKNOWN_SIZE if not known. */ + @Override public long getSize() { if (isReferenceOrProxy()) { return getReferencedOrProxied().getSize(); @@ -115,6 +118,7 @@ public class PropertyResource extends Resource { * @param o object to compare * @return true if equal to o */ + @Override public boolean equals(Object o) { if (super.equals(o)) { return true; @@ -126,6 +130,7 @@ public class PropertyResource extends Resource { * Get the hash code for this Resource. * @return hash code as int. */ + @Override public int hashCode() { if (isReferenceOrProxy()) { return getReferencedOrProxied().hashCode(); @@ -136,6 +141,7 @@ public class PropertyResource extends Resource { /** * {@inheritDoc} */ + @Override public String toString() { if (isReferenceOrProxy()) { return getReferencedOrProxied().toString(); @@ -151,6 +157,7 @@ public class PropertyResource extends Resource { * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ + @Override public InputStream getInputStream() throws IOException { if (isReferenceOrProxy()) { return getReferencedOrProxied().getInputStream(); @@ -167,6 +174,7 @@ public class PropertyResource extends Resource { * @throws UnsupportedOperationException if OutputStreams are not * supported for this Resource type. */ + @Override public OutputStream getOutputStream() throws IOException { if (isReferenceOrProxy()) { return getReferencedOrProxied().getOutputStream(); @@ -194,7 +202,7 @@ public class PropertyResource extends Resource { */ protected Resource getReferencedOrProxied() { if (isReference()) { - return (Resource) getCheckedRef(Resource.class, "resource"); + return getCheckedRef(Resource.class, "resource"); } Object o = getObjectValue(); if (o instanceof Resource) { @@ -203,4 +211,9 @@ public class PropertyResource extends Resource { throw new IllegalStateException( "This PropertyResource does not reference or proxy another Resource"); } + + @Override + protected PropertyResource getCheckedRef() { + return (PropertyResource) super.getCheckedRef(); + } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/ResourceDecorator.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/ResourceDecorator.java b/src/main/org/apache/tools/ant/types/resources/ResourceDecorator.java index 9eb8c1d..1856d85 100644 --- a/src/main/org/apache/tools/ant/types/resources/ResourceDecorator.java +++ b/src/main/org/apache/tools/ant/types/resources/ResourceDecorator.java @@ -59,12 +59,12 @@ public abstract class ResourceDecorator extends Resource { public final void addConfigured(ResourceCollection a) { checkChildrenAllowed(); if (resource != null) { - throw new BuildException("you must not specify more than one" - + " resource"); + throw new BuildException( + "you must not specify more than one resource"); } if (a.size() != 1) { - throw new BuildException("only single argument resource collections" - + " are supported"); + throw new BuildException( + "only single argument resource collections are supported"); } setChecked(false); resource = a.iterator().next(); @@ -237,6 +237,7 @@ public abstract class ResourceDecorator extends Resource { * @param name not used. * @throws BuildException always. */ + @Override public void setName(String name) throws BuildException { throw new BuildException("you can't change the name of a " + getDataTypeName()); @@ -246,6 +247,7 @@ public abstract class ResourceDecorator extends Resource { * Set the exists attribute. * @param exists if true, this resource exists. */ + @Override public void setExists(boolean exists) { throw new BuildException("you can't change the exists state of a " + getDataTypeName()); @@ -256,6 +258,7 @@ public abstract class ResourceDecorator extends Resource { * @param lastmodified not used. * @throws BuildException always. */ + @Override public void setLastModified(long lastmodified) throws BuildException { throw new BuildException("you can't change the timestamp of a " + getDataTypeName()); @@ -266,6 +269,7 @@ public abstract class ResourceDecorator extends Resource { * @param directory not used. * @throws BuildException always. */ + @Override public void setDirectory(boolean directory) throws BuildException { throw new BuildException("you can't change the directory state of a " + getDataTypeName()); @@ -276,6 +280,7 @@ public abstract class ResourceDecorator extends Resource { * @param size not used. * @throws BuildException always. */ + @Override public void setSize(long size) throws BuildException { throw new BuildException("you can't change the size of a " + getDataTypeName()); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/ResourceList.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/ResourceList.java b/src/main/org/apache/tools/ant/types/resources/ResourceList.java index fbb226c..204db1d 100644 --- a/src/main/org/apache/tools/ant/types/resources/ResourceList.java +++ b/src/main/org/apache/tools/ant/types/resources/ResourceList.java @@ -22,6 +22,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Iterator; import java.util.Stack; @@ -36,7 +37,6 @@ import org.apache.tools.ant.types.FilterChain; import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.ResourceCollection; -import org.apache.tools.ant.util.FileUtils; /** * Reads a resource as text document and creates a resource for each @@ -44,8 +44,8 @@ import org.apache.tools.ant.util.FileUtils; * @since Ant 1.8.0 */ public class ResourceList extends DataType implements ResourceCollection { - private final Vector<FilterChain> filterChains = new Vector<FilterChain>(); - private final ArrayList<ResourceCollection> textDocuments = new ArrayList<ResourceCollection>(); + private final Vector<FilterChain> filterChains = new Vector<>(); + private final ArrayList<ResourceCollection> textDocuments = new ArrayList<>(); private final Union cachedResources = new Union(); private volatile boolean cached = false; private String encoding = null; @@ -96,11 +96,12 @@ public class ResourceList extends DataType implements ResourceCollection { * Makes this instance in effect a reference to another ResourceList * instance. */ + @Override public void setRefid(Reference r) throws BuildException { if (encoding != null) { throw tooManyAttributes(); } - if (filterChains.size() > 0 || textDocuments.size() > 0) { + if (!(filterChains.isEmpty() && textDocuments.isEmpty())) { throw noChildrenAllowed(); } super.setRefid(r); @@ -112,9 +113,10 @@ public class ResourceList extends DataType implements ResourceCollection { * are added to this container while the Iterator is in use. * @return a "fail-fast" Iterator. */ + @Override public final synchronized Iterator<Resource> iterator() { if (isReference()) { - return ((ResourceList) getCheckedRef()).iterator(); + return getCheckedRef().iterator(); } return cache().iterator(); } @@ -123,9 +125,10 @@ public class ResourceList extends DataType implements ResourceCollection { * Fulfill the ResourceCollection contract. * @return number of elements as int. */ + @Override public synchronized int size() { if (isReference()) { - return ((ResourceList) getCheckedRef()).size(); + return getCheckedRef().size(); } return cache().size(); } @@ -134,9 +137,10 @@ public class ResourceList extends DataType implements ResourceCollection { * Fulfill the ResourceCollection contract. * @return whether this is a filesystem-only resource collection. */ + @Override public synchronized boolean isFilesystemOnly() { if (isReference()) { - return ((ResourceList) getCheckedRef()).isFilesystemOnly(); + return getCheckedRef().isFilesystemOnly(); } return cache().isFilesystemOnly(); } @@ -148,6 +152,7 @@ public class ResourceList extends DataType implements ResourceCollection { * @param p the project to use to dereference the references. * @throws BuildException on error. */ + @Override protected synchronized void dieOnCircularReference(Stack<Object> stk, Project p) throws BuildException { if (isChecked()) { @@ -168,62 +173,53 @@ public class ResourceList extends DataType implements ResourceCollection { } } + @Override + protected ResourceList getCheckedRef() { + return (ResourceList) super.getCheckedRef(); + } + private synchronized ResourceCollection cache() { if (!cached) { dieOnCircularReference(); - for (ResourceCollection rc : textDocuments) { - for (Resource r : rc) { - cachedResources.add(read(r)); - } - } + textDocuments.stream().flatMap(ResourceCollection::stream) + .map(this::read).forEach(cachedResources::add); cached = true; } return cachedResources; } private ResourceCollection read(Resource r) { - BufferedInputStream bis = null; - try { - bis = new BufferedInputStream(r.getInputStream()); - Reader input = null; - if (encoding == null) { - input = new InputStreamReader(bis); - } else { - input = new InputStreamReader(bis, encoding); - } - ChainReaderHelper crh = new ChainReaderHelper(); - crh.setPrimaryReader(input); - crh.setFilterChains(filterChains); - crh.setProject(getProject()); + try (BufferedReader reader = new BufferedReader(open(r))) { Union streamResources = new Union(); - try (BufferedReader reader = new BufferedReader(crh.getAssembledReader())) { - streamResources.setCache(true); - - String line = null; - while ((line = reader.readLine()) != null) { - streamResources.add(parse(line)); - } - } - + streamResources.setCache(true); + reader.lines().map(this::parse).forEach(streamResources::add); return streamResources; } catch (final IOException ioe) { throw new BuildException("Unable to read resource " + r.getName() + ": " + ioe, ioe, getLocation()); - } finally { - FileUtils.close(bis); } } + private Reader open(Resource r) throws IOException { + ChainReaderHelper crh = new ChainReaderHelper(); + crh.setPrimaryReader(new InputStreamReader( + new BufferedInputStream(r.getInputStream()), encoding == null + ? Charset.defaultCharset() : Charset.forName(encoding))); + crh.setFilterChains(filterChains); + crh.setProject(getProject()); + return crh.getAssembledReader(); + } + private Resource parse(final String line) { - PropertyHelper propertyHelper - = (PropertyHelper) PropertyHelper.getPropertyHelper(getProject()); + PropertyHelper propertyHelper = + PropertyHelper.getPropertyHelper(getProject()); Object expanded = propertyHelper.parseProperties(line); if (expanded instanceof Resource) { return (Resource) expanded; } String expandedLine = expanded.toString(); - int colon = expandedLine.indexOf(":"); - if (colon != -1) { + int colon = expandedLine.indexOf(':'); + if (colon >= 0) { // could be an URL or an absolute file on an OS with drives try { return new URLResource(expandedLine); @@ -236,4 +232,5 @@ public class ResourceList extends DataType implements ResourceCollection { } return new FileResource(getProject(), expandedLine); } + } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Resources.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Resources.java b/src/main/org/apache/tools/ant/types/resources/Resources.java index 1dd888d..b0acfa3 100644 --- a/src/main/org/apache/tools/ant/types/resources/Resources.java +++ b/src/main/org/apache/tools/ant/types/resources/Resources.java @@ -20,13 +20,14 @@ package org.apache.tools.ant.types.resources; import java.io.File; import java.util.AbstractCollection; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.Stack; -import java.util.Vector; +import java.util.stream.Collectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; @@ -43,12 +44,15 @@ import org.apache.tools.ant.util.CollectionUtils; public class Resources extends DataType implements ResourceCollection { /** static empty ResourceCollection */ public static final ResourceCollection NONE = new ResourceCollection() { + @Override public boolean isFilesystemOnly() { return true; } + @Override public Iterator<Resource> iterator() { return EMPTY_ITERATOR; } + @Override public int size() { return 0; } @@ -56,12 +60,15 @@ public class Resources extends DataType implements ResourceCollection { /** static empty Iterator */ public static final Iterator<Resource> EMPTY_ITERATOR = new Iterator<Resource>() { + @Override public Resource next() { throw new NoSuchElementException(); } + @Override public boolean hasNext() { return false; } + @Override public void remove() { throw new UnsupportedOperationException(); } @@ -72,9 +79,11 @@ public class Resources extends DataType implements ResourceCollection { MyCollection() { } + @Override public int size() { return getCache().size(); } + @Override public Iterator<Resource> iterator() { return getCache().iterator(); } @@ -92,27 +101,30 @@ public class Resources extends DataType implements ResourceCollection { private Iterator<ResourceCollection> rci = getNested().iterator(); private Iterator<Resource> ri = null; + @Override public boolean hasNext() { boolean result = ri != null && ri.hasNext(); while (!result && rci.hasNext()) { - ri = ((ResourceCollection) rci.next()).iterator(); + ri = rci.next().iterator(); result = ri.hasNext(); } return result; } + @Override public Resource next() { if (!hasNext()) { throw new NoSuchElementException(); } return ri.next(); } + @Override public void remove() { throw new UnsupportedOperationException(); } } } - private Vector<ResourceCollection> rc; + private List<ResourceCollection> rc; private Collection<Resource> coll; private boolean cache = false; @@ -151,7 +163,7 @@ public class Resources extends DataType implements ResourceCollection { return; } if (rc == null) { - rc = new Vector<ResourceCollection>(); + rc = Collections.synchronizedList(new ArrayList<>()); } rc.add(c); invalidateExistingIterators(); @@ -163,6 +175,7 @@ public class Resources extends DataType implements ResourceCollection { * Fulfill the ResourceCollection contract. * @return an Iterator of Resources. */ + @Override public synchronized Iterator<Resource> iterator() { if (isReference()) { return getRef().iterator(); @@ -175,6 +188,7 @@ public class Resources extends DataType implements ResourceCollection { * Fulfill the ResourceCollection contract. * @return number of elements as int. */ + @Override public synchronized int size() { if (isReference()) { return getRef().size(); @@ -187,24 +201,21 @@ public class Resources extends DataType implements ResourceCollection { * Fulfill the ResourceCollection contract. * @return true if all Resources represent files. */ + @Override public boolean isFilesystemOnly() { if (isReference()) { return getRef().isFilesystemOnly(); } validate(); - - for (Iterator<ResourceCollection> i = getNested().iterator(); i.hasNext();) { - if (!i.next().isFilesystemOnly()) { - return false; - } - } - return true; + return getNested().stream() + .allMatch(ResourceCollection::isFilesystemOnly); } /** * Format this <code>Resources</code> as a String. * @return a descriptive <code>String</code>. */ + @Override public synchronized String toString() { if (isReference()) { return getCheckedRef().toString(); @@ -213,14 +224,8 @@ public class Resources extends DataType implements ResourceCollection { if (coll == null || coll.isEmpty()) { return ""; } - StringBuffer sb = new StringBuffer(); - for (Resource r : coll) { - if (sb.length() > 0) { - sb.append(File.pathSeparatorChar); - } - sb.append(r); - } - return sb.toString(); + return coll.stream().map(Object::toString) + .collect(Collectors.joining(File.pathSeparator)); } /** @@ -230,6 +235,7 @@ public class Resources extends DataType implements ResourceCollection { * @param p the project to use to dereference the references. * @throws BuildException on error. */ + @Override protected void dieOnCircularReference(Stack<Object> stk, Project p) throws BuildException { if (isChecked()) { @@ -259,8 +265,7 @@ public class Resources extends DataType implements ResourceCollection { * @return the referenced ResourceCollection. */ private ResourceCollection getRef() { - return (ResourceCollection) getCheckedRef( - ResourceCollection.class, "ResourceCollection"); + return getCheckedRef(ResourceCollection.class, "ResourceCollection"); } private synchronized void validate() { http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Restrict.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Restrict.java b/src/main/org/apache/tools/ant/types/resources/Restrict.java index 2ea1a86..cc9cc26 100644 --- a/src/main/org/apache/tools/ant/types/resources/Restrict.java +++ b/src/main/org/apache/tools/ant/types/resources/Restrict.java @@ -39,13 +39,9 @@ public class Restrict /** * Restrict the nested ResourceCollection based on the nested selectors. */ + @Override protected boolean filterResource(Resource r) { - for (Iterator<ResourceSelector> i = getSelectors(); i.hasNext();) { - if (!i.next().isSelected(r)) { - return true; - } - } - return false; + return getResourceSelectors().stream().anyMatch(rsel -> !rsel.isSelected(r)); } }; @@ -84,6 +80,7 @@ public class Restrict * Add a ResourceSelector. * @param s the ResourceSelector to add. */ + @Override public synchronized void add(ResourceSelector s) { if (s == null) { return; @@ -96,9 +93,10 @@ public class Restrict * Fulfill the ResourceCollection contract. * @return an Iterator of Resources. */ + @Override public final synchronized Iterator<Resource> iterator() { if (isReference()) { - return ((Restrict) getCheckedRef()).iterator(); + return getCheckedRef().iterator(); } dieOnCircularReference(); return w.iterator(); @@ -108,9 +106,10 @@ public class Restrict * Fulfill the ResourceCollection contract. * @return number of elements as int. */ + @Override public synchronized int size() { if (isReference()) { - return ((Restrict) getCheckedRef()).size(); + return getCheckedRef().size(); } dieOnCircularReference(); return w.size(); @@ -120,9 +119,10 @@ public class Restrict * Fulfill the ResourceCollection contract. * @return whether this is a filesystem-only resource collection. */ + @Override public synchronized boolean isFilesystemOnly() { if (isReference()) { - return ((Restrict) getCheckedRef()).isFilesystemOnly(); + return getCheckedRef().isFilesystemOnly(); } dieOnCircularReference(); return w.isFilesystemOnly(); @@ -132,6 +132,7 @@ public class Restrict * Format this Restrict collection as a String. * @return the String value of this collection. */ + @Override public synchronized String toString() { if (isReference()) { return getCheckedRef().toString(); @@ -140,6 +141,7 @@ public class Restrict return w.toString(); } + @Override protected synchronized void dieOnCircularReference(Stack<Object> stk, Project p) { if (isChecked()) { return; @@ -153,4 +155,9 @@ public class Restrict setChecked(true); } } + + @Override + protected Restrict getCheckedRef() { + return (Restrict) super.getCheckedRef(); + } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java b/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java index c8e772b..6177f2e 100644 --- a/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java +++ b/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java @@ -50,10 +50,9 @@ public abstract class SizeLimitCollection extends BaseResourceCollectionWrapper * Efficient size implementation. * @return int size */ + @Override public synchronized int size() { - int sz = getResourceCollection().size(); - int ct = getValidCount(); - return sz < ct ? sz : ct; + return Math.min(getResourceCollection().size(), getValidCount()); } /** http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Sort.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Sort.java b/src/main/org/apache/tools/ant/types/resources/Sort.java index b4dc88c..341b6c9 100644 --- a/src/main/org/apache/tools/ant/types/resources/Sort.java +++ b/src/main/org/apache/tools/ant/types/resources/Sort.java @@ -18,19 +18,15 @@ package org.apache.tools.ant.types.resources; import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; import java.util.Stack; +import java.util.stream.Collectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.DataType; import org.apache.tools.ant.types.Resource; -import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator; import org.apache.tools.ant.types.resources.comparators.ResourceComparator; -import org.apache.tools.ant.util.CollectionUtils; /** * ResourceCollection that sorts another ResourceCollection. @@ -48,15 +44,10 @@ public class Sort extends BaseResourceCollectionWrapper { * Sort the contained elements. * @return a Collection of Resources. */ + @Override protected synchronized Collection<Resource> getCollection() { - ResourceCollection rc = getResourceCollection(); - Iterator<Resource> iter = rc.iterator(); - if (!(iter.hasNext())) { - return Collections.emptySet(); - } - List<Resource> result = (List<Resource>) CollectionUtils.asCollection(iter); - Collections.sort(result, comp); - return result; + return getResourceCollection().stream().map(Resource.class::cast) + .sorted(comp).collect(Collectors.toList()); } /** @@ -80,6 +71,7 @@ public class Sort extends BaseResourceCollectionWrapper { * @param p the project to use to dereference the references. * @throws BuildException on error. */ + @Override protected synchronized void dieOnCircularReference(Stack<Object> stk, Project p) throws BuildException { if (isChecked()) { http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/StringResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/StringResource.java b/src/main/org/apache/tools/ant/types/resources/StringResource.java index 9a52982..9a2a0ad 100644 --- a/src/main/org/apache/tools/ant/types/resources/StringResource.java +++ b/src/main/org/apache/tools/ant/types/resources/StringResource.java @@ -149,8 +149,8 @@ public class StringResource extends Resource { */ @Override public synchronized long getSize() { - return isReference() ? ((Resource) getCheckedRef()).getSize() - : getContent().length(); + return isReference() ? getCheckedRef().getSize() + : getContent().length(); } /** @@ -188,7 +188,7 @@ public class StringResource extends Resource { @Override public synchronized InputStream getInputStream() throws IOException { if (isReference()) { - return ((Resource) getCheckedRef()).getInputStream(); + return getCheckedRef().getInputStream(); } String content = getContent(); if (content == null) { @@ -209,7 +209,7 @@ public class StringResource extends Resource { @Override public synchronized OutputStream getOutputStream() throws IOException { if (isReference()) { - return ((Resource) getCheckedRef()).getOutputStream(); + return getCheckedRef().getOutputStream(); } if (getValue() != null) { throw new ImmutableResourceException(); @@ -237,19 +237,9 @@ public class StringResource extends Resource { return getValue(); } - /** - * This method is only for use by our private helper output stream. - * It contains specific logic for expanding properties. - * @param output the output - */ - private void setValueFromOutputStream(String output) { - String value; - if (getProject() != null) { - value = getProject().replaceProperties(output); - } else { - value = output; - } - setValue(value); + @Override + protected StringResource getCheckedRef() { + return (StringResource) super.getCheckedRef(); } private class StringResourceFilterOutputStream extends FilterOutputStream { @@ -266,7 +256,18 @@ public class StringResource extends Resource { String result = encoding == null ? baos.toString() : baos.toString(encoding); - StringResource.this.setValueFromOutputStream(result); + setValueFromOutputStream(result); } + + private void setValueFromOutputStream(String output) { + String value; + if (getProject() != null) { + value = getProject().replaceProperties(output); + } else { + value = output; + } + setValue(value); + } + } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/TarResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/TarResource.java b/src/main/org/apache/tools/ant/types/resources/TarResource.java index b906a65..7713e44 100644 --- a/src/main/org/apache/tools/ant/types/resources/TarResource.java +++ b/src/main/org/apache/tools/ant/types/resources/TarResource.java @@ -74,13 +74,14 @@ public class TarResource extends ArchiveResource { * @throws IOException if the tar file cannot be opened, * or the entry cannot be read. */ + @Override public InputStream getInputStream() throws IOException { if (isReference()) { - return ((Resource) getCheckedRef()).getInputStream(); + return getCheckedRef().getInputStream(); } Resource archive = getArchive(); final TarInputStream i = new TarInputStream(archive.getInputStream()); - TarEntry te = null; + TarEntry te; while ((te = i.getNextEntry()) != null) { if (te.getName().equals(getName())) { return i; @@ -100,9 +101,10 @@ public class TarResource extends ArchiveResource { * @throws UnsupportedOperationException if OutputStreams are not * supported for this Resource type. */ + @Override public OutputStream getOutputStream() throws IOException { if (isReference()) { - return ((Resource) getCheckedRef()).getOutputStream(); + return getCheckedRef().getOutputStream(); } throw new UnsupportedOperationException( "Use the tar task for tar output."); @@ -113,7 +115,7 @@ public class TarResource extends ArchiveResource { */ public String getUserName() { if (isReference()) { - return ((TarResource) getCheckedRef()).getUserName(); + return getCheckedRef().getUserName(); } checkEntry(); return userName; @@ -124,7 +126,7 @@ public class TarResource extends ArchiveResource { */ public String getGroup() { if (isReference()) { - return ((TarResource) getCheckedRef()).getGroup(); + return getCheckedRef().getGroup(); } checkEntry(); return groupName; @@ -135,7 +137,7 @@ public class TarResource extends ArchiveResource { */ public int getUid() { if (isReference()) { - return ((TarResource) getCheckedRef()).getUid(); + return getCheckedRef().getUid(); } checkEntry(); return uid; @@ -146,7 +148,7 @@ public class TarResource extends ArchiveResource { */ public int getGid() { if (isReference()) { - return ((TarResource) getCheckedRef()).getGid(); + return getCheckedRef().getGid(); } checkEntry(); return gid; @@ -155,11 +157,10 @@ public class TarResource extends ArchiveResource { /** * fetches information from the named entry inside the archive. */ + @Override protected void fetchEntry() { Resource archive = getArchive(); - TarInputStream i = null; - try { - i = new TarInputStream(archive.getInputStream()); + try (TarInputStream i = new TarInputStream(archive.getInputStream())) { TarEntry te = null; while ((te = i.getNextEntry()) != null) { if (te.getName().equals(getName())) { @@ -170,12 +171,15 @@ public class TarResource extends ArchiveResource { } catch (IOException e) { log(e.getMessage(), Project.MSG_DEBUG); throw new BuildException(e); - } finally { - FileUtils.close(i); } setEntry(null); } + @Override + protected TarResource getCheckedRef() { + return (TarResource) super.getCheckedRef(); + } + private void setEntry(TarEntry e) { if (e == null) { setExists(false); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Tokens.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Tokens.java b/src/main/org/apache/tools/ant/types/resources/Tokens.java index 458f8c1..c8e9110 100644 --- a/src/main/org/apache/tools/ant/types/resources/Tokens.java +++ b/src/main/org/apache/tools/ant/types/resources/Tokens.java @@ -19,10 +19,11 @@ package org.apache.tools.ant.types.resources; import java.io.IOException; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Stack; import org.apache.tools.ant.BuildException; @@ -31,7 +32,6 @@ import org.apache.tools.ant.types.DataType; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.util.ConcatResourceInputStream; -import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.LineTokenizer; import org.apache.tools.ant.util.Tokenizer; @@ -51,38 +51,26 @@ public class Tokens extends BaseResourceCollectionWrapper { */ protected synchronized Collection<Resource> getCollection() { ResourceCollection rc = getResourceCollection(); - if (rc.size() == 0) { + if (rc.isEmpty()) { return Collections.emptySet(); } if (tokenizer == null) { tokenizer = new LineTokenizer(); } - ConcatResourceInputStream cat = new ConcatResourceInputStream(rc); - cat.setManagingComponent(this); - - InputStreamReader rdr = null; - try { - if (encoding == null) { - rdr = new InputStreamReader(cat); - } else { - try { - rdr = new InputStreamReader(cat, encoding); - } catch (UnsupportedEncodingException e) { - throw new BuildException(e); - } - } - ArrayList<Resource> result = new ArrayList<Resource>(); - for (String s = tokenizer.getToken(rdr); s != null; s = tokenizer.getToken(rdr)) { - StringResource resource = new StringResource(s); - resource.setProject(getProject()); + try (ConcatResourceInputStream cat = new ConcatResourceInputStream(rc); + InputStreamReader rdr = new InputStreamReader(cat, + encoding == null ? Charset.defaultCharset() + : Charset.forName(encoding))) { + cat.setManagingComponent(this); + List<Resource> result = new ArrayList<>(); + for (String s = tokenizer.getToken(rdr); s != null; s = + tokenizer.getToken(rdr)) { + StringResource resource = new StringResource(getProject(), s); result.add(resource); } return result; } catch (IOException e) { throw new BuildException("Error reading tokens", e); - } finally { - FileUtils.close(rdr); - FileUtils.close(cat); } } @@ -117,6 +105,7 @@ public class Tokens extends BaseResourceCollectionWrapper { * @param p the project to use to dereference the references. * @throws BuildException on error. */ + @Override protected synchronized void dieOnCircularReference(Stack<Object> stk, Project p) throws BuildException { if (isChecked()) { http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/Union.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/Union.java b/src/main/org/apache/tools/ant/types/resources/Union.java index e2f2f9f..9c10658 100644 --- a/src/main/org/apache/tools/ant/types/resources/Union.java +++ b/src/main/org/apache/tools/ant/types/resources/Union.java @@ -17,12 +17,12 @@ */ package org.apache.tools.ant.types.resources; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.LinkedHashSet; -import java.util.List; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Resource; @@ -84,8 +84,7 @@ public class Union extends BaseResourceCollectionContainer { if (isReference()) { return getCheckedRef(Union.class, getDataTypeName()).list(); } - final Collection<String> result = getAllToStrings(); - return result.toArray(new String[result.size()]); + return streamResources().map(Object::toString).toArray(String[]::new); } /** @@ -96,14 +95,14 @@ public class Union extends BaseResourceCollectionContainer { if (isReference()) { return getCheckedRef(Union.class, getDataTypeName()).listResources(); } - final Collection<Resource> result = getAllResources(); - return result.toArray(new Resource[result.size()]); + return streamResources().toArray(Resource[]::new); } /** * Unify the contained Resources. * @return a Collection of Resources. */ + @Override protected Collection<Resource> getCollection() { return getAllResources(); } @@ -117,7 +116,8 @@ public class Union extends BaseResourceCollectionContainer { @Deprecated @SuppressWarnings("unchecked") protected <T> Collection<T> getCollection(boolean asString) { // TODO untypable - return asString ? (Collection<T>) getAllToStrings() : (Collection<T>) getAllResources(); + return asString ? (Collection<T>) getAllToStrings() + : (Collection<T>) getAllResources(); } /** @@ -125,12 +125,8 @@ public class Union extends BaseResourceCollectionContainer { * @return Collection<String> */ protected Collection<String> getAllToStrings() { - final Set<Resource> allResources = getAllResources(); - final ArrayList<String> result = new ArrayList<String>(allResources.size()); - for (Resource r : allResources) { - result.add(r.toString()); - } - return result; + return streamResources(Object::toString) + .collect(Collectors.toCollection(LinkedHashSet::new)); } /** @@ -138,19 +134,17 @@ public class Union extends BaseResourceCollectionContainer { * @return Set<Resource> */ protected Set<Resource> getAllResources() { - final List<ResourceCollection> resourceCollections = getResourceCollections(); - if (resourceCollections.isEmpty()) { - return Collections.emptySet(); - } - final LinkedHashSet<Resource> result = new LinkedHashSet<Resource>( - resourceCollections.size() * 2); - for (ResourceCollection resourceCollection : resourceCollections) { - for (Resource r : resourceCollection) { - result.add(r); - } - } - return result; + return streamResources() + .collect(Collectors.toCollection(LinkedHashSet::new)); } -} + private Stream<? extends Resource> streamResources() { + return streamResources(Function.identity()); + } + private <T> Stream<? extends T> streamResources( + Function<? super Resource, ? extends T> mapper) { + return getResourceCollections().stream() + .flatMap(ResourceCollection::stream).map(mapper).distinct(); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/comparators/Content.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/comparators/Content.java b/src/main/org/apache/tools/ant/types/resources/comparators/Content.java index 1810b64..07dd315 100644 --- a/src/main/org/apache/tools/ant/types/resources/comparators/Content.java +++ b/src/main/org/apache/tools/ant/types/resources/comparators/Content.java @@ -57,7 +57,7 @@ public class Content extends ResourceComparator { * @return a negative integer, zero, or a positive integer as the first * argument is less than, equal to, or greater than the second. * @throws BuildException if I/O errors occur. - * @see org.apache.tools.ant.util.ResourceUtils#compareContent(Resource, Resource, boolean). + * @see org.apache.tools.ant.util.ResourceUtils#compareContent(Resource, Resource, boolean) */ protected int resourceCompare(Resource foo, Resource bar) { try { http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/types/resources/comparators/Date.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/comparators/Date.java b/src/main/org/apache/tools/ant/types/resources/comparators/Date.java index b6be66b..ab7ed25 100644 --- a/src/main/org/apache/tools/ant/types/resources/comparators/Date.java +++ b/src/main/org/apache/tools/ant/types/resources/comparators/Date.java @@ -17,6 +17,8 @@ */ package org.apache.tools.ant.types.resources.comparators; +import java.util.Comparator; + import org.apache.tools.ant.types.Resource; /** @@ -32,14 +34,8 @@ public class Date extends ResourceComparator { * argument is less than, equal to, or greater than the second. */ protected int resourceCompare(Resource foo, Resource bar) { - long diff = foo.getLastModified() - bar.getLastModified(); - if (diff > 0) { - return +1; - } else if (diff < 0) { - return -1; - } else { - return 0; - } + return Comparator.comparingLong(Resource::getLastModified).compare(foo, + bar); } }
