From bbcec8567ceb7e3b089090c51d18a65235616caf Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 21 Aug 2009 09:15:45 +0000 Subject: [PATCH] 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 --- .../tools/ant/types/resources/Resources.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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 f285616db..6ffba542e 100644 --- a/src/main/org/apache/tools/ant/types/resources/Resources.java +++ b/src/main/org/apache/tools/ant/types/resources/Resources.java @@ -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();