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.HashSet;
22: import java.util.Iterator;
23: import java.util.ArrayList;
24: import java.util.Collection;
25:
26: import org.apache.tools.ant.BuildException;
27: import org.apache.tools.ant.types.ResourceCollection;
28:
29: /**
30: * ResourceCollection representing the difference between
31: * two or more nested ResourceCollections.
32: * @since Ant 1.7
33: */
34: public class Difference extends BaseResourceCollectionContainer {
35:
36: /**
37: * Calculate the difference of the nested ResourceCollections.
38: * @return a Collection of Resources.
39: */
40: protected Collection getCollection() {
41: List rc = getResourceCollections();
42: int size = rc.size();
43: if (size < 2) {
44: throw new BuildException("The difference of " + size
45: + " resource collection" + ((size == 1) ? "" : "s")
46: + " is undefined.");
47: }
48: HashSet hs = new HashSet();
49: ArrayList al = new ArrayList();
50: for (Iterator rcIter = rc.iterator(); rcIter.hasNext();) {
51: for (Iterator r = nextRC(rcIter).iterator(); r.hasNext();) {
52: Object next = r.next();
53: if (hs.add(next)) {
54: al.add(next);
55: } else {
56: al.remove(next);
57: }
58: }
59: }
60: return al;
61: }
62:
63: private static ResourceCollection nextRC(Iterator i) {
64: return (ResourceCollection) i.next();
65: }
66:
67: }
|