KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collections JavaDoc;
25
26 import org.apache.tools.ant.types.Resource;
27 import org.apache.tools.ant.types.ResourceCollection;
28
29 /**
30  * ResourceCollection representing the union of multiple nested ResourceCollections.
31  * @since Ant 1.7
32  */

33 public class Union extends BaseResourceCollectionContainer {
34
35     /**
36      * Static convenience method to union an arbitrary set of Resources.
37      * @param rc a ResourceCollection.
38      * @return a Union.
39      */

40     public static Union getInstance(ResourceCollection rc) {
41         return rc instanceof Union ? (Union) rc : new Union(rc);
42     }
43
44     /**
45      * Default constructor.
46      */

47     public Union() {
48     }
49
50     /**
51      * Convenience constructor.
52      * @param rc the ResourceCollection to add.
53      */

54     public Union(ResourceCollection rc) {
55         add(rc);
56     }
57
58     /**
59      * Returns all Resources in String format. Provided for
60      * convenience in implementing Path.
61      * @return String array of Resources.
62      */

63     public String JavaDoc[] list() {
64         if (isReference()) {
65             return ((Union) getCheckedRef()).list();
66         }
67         Collection JavaDoc result = getCollection(true);
68         return (String JavaDoc[]) (result.toArray(new String JavaDoc[result.size()]));
69     }
70
71     /**
72      * Convenience method.
73      * @return Resource[]
74      */

75     public Resource[] listResources() {
76         if (isReference()) {
77             return ((Union) getCheckedRef()).listResources();
78         }
79         Collection JavaDoc result = getCollection();
80         return (Resource[]) (result.toArray(new Resource[result.size()]));
81     }
82
83     /**
84      * Unify the contained Resources.
85      * @return a Collection of Resources.
86      */

87     protected Collection JavaDoc getCollection() {
88         return getCollection(false);
89     }
90
91     /**
92      * Unify the contained Resources.
93      * @param asString indicates whether the resulting Collection
94      * should contain Strings instead of Resources.
95      * @return a Collection of Resources.
96      */

97     protected Collection JavaDoc getCollection(boolean asString) {
98         List JavaDoc rc = getResourceCollections();
99         if (rc.isEmpty()) {
100             return Collections.EMPTY_LIST;
101         }
102         //preserve order-encountered using a list; enforce set logic manually:
103
ArrayList JavaDoc union = new ArrayList JavaDoc(rc.size() * 2);
104         for (Iterator JavaDoc rcIter = rc.iterator(); rcIter.hasNext();) {
105             for (Iterator JavaDoc r = nextRC(rcIter).iterator(); r.hasNext();) {
106                 Object JavaDoc o = r.next();
107                 if (asString) {
108                     o = o.toString();
109                 }
110                 if (!(union.contains(o))) {
111                     union.add(o);
112                 }
113             }
114         }
115         return union;
116     }
117
118     private static ResourceCollection nextRC(Iterator JavaDoc i) {
119         return (ResourceCollection) i.next();
120     }
121 }
122
123
Popular Tags