Browse Source

cache iterator in Resources if asked for size

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@806471 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
bbcec8567c
1 changed files with 11 additions and 8 deletions
  1. +11
    -8
      src/main/org/apache/tools/ant/types/resources/Resources.java

+ 11
- 8
src/main/org/apache/tools/ant/types/resources/Resources.java View File

@@ -32,6 +32,7 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.DataType;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.util.CollectionUtils;

/**
* Generic ResourceCollection: Either stores nested ResourceCollections,
@@ -66,19 +67,21 @@ public class Resources extends DataType implements ResourceCollection {
};

private class MyCollection extends AbstractCollection {
private int size;
private Collection cache;

MyCollection() {
size = 0;
for (Iterator rci = getNested().iterator(); rci.hasNext();) {
size += ((ResourceCollection) rci.next()).size();
}
}
public int size() {
return size;
return getCache().size();
}
public Iterator iterator() {
return new MyIterator();
public synchronized Iterator iterator() {
return cache != null ? cache.iterator() : new MyIterator();
}
private synchronized Collection getCache() {
if (cache == null) {
cache = CollectionUtils.asCollection(new MyIterator());
}
return cache;
}
private class MyIterator implements Iterator {
private Iterator rci = getNested().iterator();


Loading…
Cancel
Save