KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
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 JavaDoc getCollection() {
41         List JavaDoc 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 JavaDoc hs = new HashSet JavaDoc();
49         ArrayList JavaDoc al = new ArrayList JavaDoc();
50         for (Iterator JavaDoc rcIter = rc.iterator(); rcIter.hasNext();) {
51             for (Iterator JavaDoc r = nextRC(rcIter).iterator(); r.hasNext();) {
52                 Object JavaDoc 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 JavaDoc i) {
64         return (ResourceCollection) i.next();
65     }
66
67 }
68
Popular Tags