KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.types.ResourceCollection;
27
28 /**
29  * ResourceCollection representing the intersection
30  * of multiple nested ResourceCollections.
31  * @since Ant 1.7
32  */

33 public class Intersect extends BaseResourceCollectionContainer {
34
35     /**
36      * Calculate the intersection of the nested ResourceCollections.
37      * @return a Collection of Resources.
38      */

39     protected Collection JavaDoc getCollection() {
40         List JavaDoc rcs = getResourceCollections();
41         int size = rcs.size();
42         if (size < 2) {
43             throw new BuildException("The intersection of " + size
44                 + " resource collection" + ((size == 1) ? "" : "s")
45                 + " is undefined.");
46         }
47         ArrayList JavaDoc al = new ArrayList JavaDoc();
48         Iterator JavaDoc rc = rcs.iterator();
49         al.addAll(collect(rc.next()));
50         while (rc.hasNext()) {
51             al.retainAll(collect(rc.next()));
52         }
53         return al;
54     }
55
56     private ArrayList JavaDoc collect(Object JavaDoc o) {
57         ArrayList JavaDoc result = new ArrayList JavaDoc();
58         for (Iterator JavaDoc i = ((ResourceCollection) o).iterator(); i.hasNext();) {
59             result.add(i.next());
60         }
61         return result;
62     }
63 }
64
Popular Tags