001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.resources;
019:
020: import java.util.Iterator;
021: import java.util.ArrayList;
022: import java.util.Collection;
023:
024: import org.apache.tools.ant.types.Resource;
025: import org.apache.tools.ant.types.ResourceCollection;
026: import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
027: import org.apache.tools.ant.types.resources.selectors.ResourceSelectorContainer;
028:
029: /**
030: * ResourceCollection that allows a number of selectors to be
031: * applied to a single ResourceCollection for the purposes of
032: * restricting or narrowing results.
033: * @since Ant 1.7
034: */
035: public class Restrict extends ResourceSelectorContainer implements
036: ResourceCollection {
037:
038: private BaseResourceCollectionWrapper w = new BaseResourceCollectionWrapper() {
039: /**
040: * Restrict the nested ResourceCollection based on the nested selectors.
041: * @return a Collection of Resources.
042: */
043: protected Collection getCollection() {
044: ArrayList result = new ArrayList();
045: outer: for (Iterator ri = w.getResourceCollection()
046: .iterator(); ri.hasNext();) {
047: Resource r = (Resource) ri.next();
048: for (Iterator i = getSelectors(); i.hasNext();) {
049: if (!((ResourceSelector) (i.next())).isSelected(r)) {
050: continue outer;
051: }
052: }
053: result.add(r);
054: }
055: return result;
056: }
057: };
058:
059: /**
060: * Add the ResourceCollection.
061: * @param c the ResourceCollection to add.
062: */
063: public synchronized void add(ResourceCollection c) {
064: if (isReference()) {
065: throw noChildrenAllowed();
066: }
067: if (c == null) {
068: return;
069: }
070: w.add(c);
071: }
072:
073: /**
074: * Set whether to cache collections.
075: * @param b boolean cache flag.
076: */
077: public synchronized void setCache(boolean b) {
078: w.setCache(b);
079: }
080:
081: /**
082: * Learn whether to cache collections. Default is <code>true</code>.
083: * @return boolean cache flag.
084: */
085: public synchronized boolean isCache() {
086: return w.isCache();
087: }
088:
089: /**
090: * Add a ResourceSelector.
091: * @param s the ResourceSelector to add.
092: */
093: public synchronized void add(ResourceSelector s) {
094: if (s == null) {
095: return;
096: }
097: super .add(s);
098: FailFast.invalidate(this );
099: }
100:
101: /**
102: * Fulfill the ResourceCollection contract.
103: * @return an Iterator of Resources.
104: */
105: public final synchronized Iterator iterator() {
106: if (isReference()) {
107: return ((Restrict) getCheckedRef()).iterator();
108: }
109: dieOnCircularReference();
110: return w.iterator();
111: }
112:
113: /**
114: * Fulfill the ResourceCollection contract.
115: * @return number of elements as int.
116: */
117: public synchronized int size() {
118: if (isReference()) {
119: return ((Restrict) getCheckedRef()).size();
120: }
121: dieOnCircularReference();
122: return w.size();
123: }
124:
125: /**
126: * Fulfill the ResourceCollection contract.
127: * @return whether this is a filesystem-only resource collection.
128: */
129: public synchronized boolean isFilesystemOnly() {
130: if (isReference()) {
131: return ((Restrict) getCheckedRef()).isFilesystemOnly();
132: }
133: dieOnCircularReference();
134: return w.isFilesystemOnly();
135: }
136:
137: /**
138: * Format this Restrict collection as a String.
139: * @return the String value of this collection.
140: */
141: public synchronized String toString() {
142: if (isReference()) {
143: return getCheckedRef().toString();
144: }
145: dieOnCircularReference();
146: return w.toString();
147: }
148:
149: }
|