Browse Source

Add option to disable collection caching; refactor cache usage for smaller code.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278492 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 20 years ago
parent
commit
4662a4c1e4
1 changed files with 28 additions and 12 deletions
  1. +28
    -12
      src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionContainer.java

+ 28
- 12
src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionContainer.java View File

@@ -37,6 +37,23 @@ public abstract class BaseResourceCollectionContainer
extends DataType implements ResourceCollection, Cloneable { extends DataType implements ResourceCollection, Cloneable {
private List rc = new ArrayList(); private List rc = new ArrayList();
private Collection coll = null; private Collection coll = null;
private boolean cache = true;

/**
* Set whether to cache collections.
* @param b boolean cache flag.
*/
public synchronized void setCache(boolean b) {
cache = b;
}

/**
* Learn whether to cache collections. Default is <code>true</code>.
* @return boolean cache flag.
*/
public synchronized boolean isCache() {
return cache;
}


/** /**
* Add a ResourceCollection to the container. * Add a ResourceCollection to the container.
@@ -85,8 +102,7 @@ public abstract class BaseResourceCollectionContainer
return ((BaseResourceCollectionContainer) getCheckedRef()).iterator(); return ((BaseResourceCollectionContainer) getCheckedRef()).iterator();
} }
dieOnCircularReference(); dieOnCircularReference();
cacheCollection();
return new FailFast(this, coll.iterator());
return new FailFast(this, cacheCollection().iterator());
} }


/** /**
@@ -98,8 +114,7 @@ public abstract class BaseResourceCollectionContainer
return ((BaseResourceCollectionContainer) getCheckedRef()).size(); return ((BaseResourceCollectionContainer) getCheckedRef()).size();
} }
dieOnCircularReference(); dieOnCircularReference();
cacheCollection();
return coll.size();
return cacheCollection().size();
} }


/** /**
@@ -121,8 +136,7 @@ public abstract class BaseResourceCollectionContainer
} }
/* now check each Resource in case the child only /* now check each Resource in case the child only
lets through files from any children IT may have: */ lets through files from any children IT may have: */
cacheCollection();
for (Iterator i = coll.iterator(); i.hasNext();) {
for (Iterator i = cacheCollection().iterator(); i.hasNext();) {
if (!(i.next() instanceof FileResource)) { if (!(i.next() instanceof FileResource)) {
return false; return false;
} }
@@ -137,7 +151,7 @@ public abstract class BaseResourceCollectionContainer
* @param p the project to use to dereference the references. * @param p the project to use to dereference the references.
* @throws BuildException on error. * @throws BuildException on error.
*/ */
protected void dieOnCircularReference(Stack stk, Project p)
protected synchronized void dieOnCircularReference(Stack stk, Project p)
throws BuildException { throws BuildException {
if (isChecked()) { if (isChecked()) {
return; return;
@@ -193,12 +207,11 @@ public abstract class BaseResourceCollectionContainer
* Format this BaseResourceCollectionContainer as a String. * Format this BaseResourceCollectionContainer as a String.
* @return a descriptive <code>String</code>. * @return a descriptive <code>String</code>.
*/ */
public String toString() {
public synchronized String toString() {
if (isReference()) { if (isReference()) {
return getCheckedRef().toString(); return getCheckedRef().toString();
} }
cacheCollection();
if (coll.size() == 0) {
if (cacheCollection().size() == 0) {
return ""; return "";
} }
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
@@ -211,8 +224,11 @@ public abstract class BaseResourceCollectionContainer
return sb.toString(); return sb.toString();
} }


private synchronized void cacheCollection() {
coll = (coll == null) ? getCollection() : coll;
private synchronized Collection cacheCollection() {
if (coll == null || !isCache()) {
coll = getCollection();
}
return coll;
} }


} }

Loading…
Cancel
Save