001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.resources;
019:
020: import java.util.List;
021: import java.util.Iterator;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025:
026: import org.apache.tools.ant.types.Resource;
027: import org.apache.tools.ant.types.ResourceCollection;
028:
029: /**
030: * ResourceCollection representing the union of multiple nested ResourceCollections.
031: * @since Ant 1.7
032: */
033: public class Union extends BaseResourceCollectionContainer {
034:
035: /**
036: * Static convenience method to union an arbitrary set of Resources.
037: * @param rc a ResourceCollection.
038: * @return a Union.
039: */
040: public static Union getInstance(ResourceCollection rc) {
041: return rc instanceof Union ? (Union) rc : new Union(rc);
042: }
043:
044: /**
045: * Default constructor.
046: */
047: public Union() {
048: }
049:
050: /**
051: * Convenience constructor.
052: * @param rc the ResourceCollection to add.
053: */
054: public Union(ResourceCollection rc) {
055: add(rc);
056: }
057:
058: /**
059: * Returns all Resources in String format. Provided for
060: * convenience in implementing Path.
061: * @return String array of Resources.
062: */
063: public String[] list() {
064: if (isReference()) {
065: return ((Union) getCheckedRef()).list();
066: }
067: Collection result = getCollection(true);
068: return (String[]) (result.toArray(new String[result.size()]));
069: }
070:
071: /**
072: * Convenience method.
073: * @return Resource[]
074: */
075: public Resource[] listResources() {
076: if (isReference()) {
077: return ((Union) getCheckedRef()).listResources();
078: }
079: Collection result = getCollection();
080: return (Resource[]) (result
081: .toArray(new Resource[result.size()]));
082: }
083:
084: /**
085: * Unify the contained Resources.
086: * @return a Collection of Resources.
087: */
088: protected Collection getCollection() {
089: return getCollection(false);
090: }
091:
092: /**
093: * Unify the contained Resources.
094: * @param asString indicates whether the resulting Collection
095: * should contain Strings instead of Resources.
096: * @return a Collection of Resources.
097: */
098: protected Collection getCollection(boolean asString) {
099: List rc = getResourceCollections();
100: if (rc.isEmpty()) {
101: return Collections.EMPTY_LIST;
102: }
103: //preserve order-encountered using a list; enforce set logic manually:
104: ArrayList union = new ArrayList(rc.size() * 2);
105: for (Iterator rcIter = rc.iterator(); rcIter.hasNext();) {
106: for (Iterator r = nextRC(rcIter).iterator(); r.hasNext();) {
107: Object o = r.next();
108: if (asString) {
109: o = o.toString();
110: }
111: if (!(union.contains(o))) {
112: union.add(o);
113: }
114: }
115: }
116: return union;
117: }
118:
119: private static ResourceCollection nextRC(Iterator i) {
120: return (ResourceCollection) i.next();
121: }
122: }
|