01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
21: import java.util.Iterator;
22: import java.util.ArrayList;
23: import java.util.Collection;
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 getCollection() {
40: List 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 al = new ArrayList();
48: Iterator 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 collect(Object o) {
57: ArrayList result = new ArrayList();
58: for (Iterator i = ((ResourceCollection) o).iterator(); i
59: .hasNext();) {
60: result.add(i.next());
61: }
62: return result;
63: }
64: }
|