KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > Restrict


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types.resources;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import org.apache.tools.ant.types.Resource;
25 import org.apache.tools.ant.types.ResourceCollection;
26 import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
27 import org.apache.tools.ant.types.resources.selectors.ResourceSelectorContainer;
28
29 /**
30  * ResourceCollection that allows a number of selectors to be
31  * applied to a single ResourceCollection for the purposes of
32  * restricting or narrowing results.
33  * @since Ant 1.7
34  */

35 public class Restrict
36     extends ResourceSelectorContainer implements ResourceCollection {
37
38     private BaseResourceCollectionWrapper w = new BaseResourceCollectionWrapper() {
39         /**
40          * Restrict the nested ResourceCollection based on the nested selectors.
41          * @return a Collection of Resources.
42          */

43         protected Collection JavaDoc getCollection() {
44             ArrayList JavaDoc result = new ArrayList JavaDoc();
45 outer: for (Iterator JavaDoc ri = w.getResourceCollection().iterator(); ri.hasNext();) {
46                 Resource r = (Resource) ri.next();
47                 for (Iterator JavaDoc i = getSelectors(); i.hasNext();) {
48                     if (!((ResourceSelector) (i.next())).isSelected(r)) {
49                         continue outer;
50                     }
51                 }
52                 result.add(r);
53             }
54             return result;
55         }
56     };
57
58     /**
59      * Add the ResourceCollection.
60      * @param c the ResourceCollection to add.
61      */

62     public synchronized void add(ResourceCollection c) {
63         if (isReference()) {
64             throw noChildrenAllowed();
65         }
66         if (c == null) {
67             return;
68         }
69         w.add(c);
70     }
71
72     /**
73      * Set whether to cache collections.
74      * @param b boolean cache flag.
75      */

76     public synchronized void setCache(boolean b) {
77         w.setCache(b);
78     }
79
80     /**
81      * Learn whether to cache collections. Default is <code>true</code>.
82      * @return boolean cache flag.
83      */

84     public synchronized boolean isCache() {
85         return w.isCache();
86     }
87
88     /**
89      * Add a ResourceSelector.
90      * @param s the ResourceSelector to add.
91      */

92     public synchronized void add(ResourceSelector s) {
93         if (s == null) {
94             return;
95         }
96         super.add(s);
97         FailFast.invalidate(this);
98     }
99
100     /**
101      * Fulfill the ResourceCollection contract.
102      * @return an Iterator of Resources.
103      */

104     public final synchronized Iterator JavaDoc iterator() {
105         if (isReference()) {
106             return ((Restrict) getCheckedRef()).iterator();
107         }
108         dieOnCircularReference();
109         return w.iterator();
110     }
111
112     /**
113      * Fulfill the ResourceCollection contract.
114      * @return number of elements as int.
115      */

116     public synchronized int size() {
117         if (isReference()) {
118             return ((Restrict) getCheckedRef()).size();
119         }
120         dieOnCircularReference();
121         return w.size();
122     }
123
124     /**
125      * Fulfill the ResourceCollection contract.
126      * @return whether this is a filesystem-only resource collection.
127      */

128     public synchronized boolean isFilesystemOnly() {
129         if (isReference()) {
130             return ((Restrict) getCheckedRef()).isFilesystemOnly();
131         }
132         dieOnCircularReference();
133         return w.isFilesystemOnly();
134     }
135
136     /**
137      * Format this Restrict collection as a String.
138      * @return the String value of this collection.
139      */

140     public synchronized String JavaDoc toString() {
141         if (isReference()) {
142             return getCheckedRef().toString();
143         }
144         dieOnCircularReference();
145         return w.toString();
146     }
147
148 }
149
Popular Tags