mbenson 2005/08/05 10:58:59 Modified: src/main/org/apache/tools/ant/types/resources Restrict.java Log: Refactor single-nested-collection enforcement into a BaseResourceCollectionWrapper via composition. Revision Changes Path 1.3 +24 -46 ant/src/main/org/apache/tools/ant/types/resources/Restrict.java Index: Restrict.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/resources/Restrict.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Restrict.java 3 Jun 2005 16:46:55 -0000 1.2 +++ Restrict.java 5 Aug 2005 17:58:59 -0000 1.3 @@ -35,10 +35,26 @@ public class Restrict extends ResourceSelectorContainer implements ResourceCollection { - private static final String ONE_NESTED_MESSAGE - = "Restriction is to be applied to exactly one nested resource collection."; + private BaseResourceCollectionWrapper w = new BaseResourceCollectionWrapper() { + /** + * Restrict the nested ResourceCollection based on the nested selectors. + * @return a Collection of Resources. + */ + protected Collection getCollection() { + ArrayList result = new ArrayList(); +outer: for (Iterator ri = w.getResourceCollection().iterator(); ri.hasNext();) { + Resource r = (Resource) ri.next(); + for (Iterator i = getSelectors(); i.hasNext();) { + if (!((ResourceSelector) (i.next())).isSelected(r)) { + continue outer; + } + } + result.add(r); + } + return result; + } - private ResourceCollection rc; + }; /** * Add the ResourceCollection. @@ -48,10 +64,7 @@ if (isReference()) { throw noChildrenAllowed(); } - if (rc != null) { - throw new BuildException(ONE_NESTED_MESSAGE); - } - rc = c; + w.add(c); } /** @@ -72,10 +85,7 @@ return ((Restrict) getCheckedRef()).iterator(); } dieOnCircularReference(); - if (rc == null) { - throw new BuildException(ONE_NESTED_MESSAGE); - } - return new FailFast(this, getCollection().iterator()); + return new FailFast(this, w.iterator()); } /** @@ -87,7 +97,7 @@ return ((Restrict) getCheckedRef()).size(); } dieOnCircularReference(); - return getCollection().size(); + return w.size(); } /** @@ -99,39 +109,7 @@ return ((Restrict) getCheckedRef()).isFilesystemOnly(); } dieOnCircularReference(); - if (rc == null) { - throw new BuildException(ONE_NESTED_MESSAGE); - } - //first the easy way, if child is filesystem-only, return true: - if (rc.isFilesystemOnly()) { - return true; - } - /* now check each Resource in case the child only - lets through files from any children IT may have: */ - for (Iterator i = getCollection().iterator(); i.hasNext();) { - if (!(i.next() instanceof FileResource)) { - return false; - } - } - return true; - } - - /** - * Restrict the nested ResourceCollection based on the nested selectors. - * @return a Collection of Resources. - */ - protected Collection getCollection() { - ArrayList result = new ArrayList(); -outer: for (Iterator ri = rc.iterator(); ri.hasNext();) { - Resource r = (Resource) ri.next(); - for (Iterator i = getSelectors(); i.hasNext();) { - if (!((ResourceSelector) (i.next())).isSelected(r)) { - continue outer; - } - } - result.add(r); - } - return result; + return w.isFilesystemOnly(); } -} \ No newline at end of file +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]