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.util;
019:
020: import java.util.Vector;
021: import java.util.Iterator;
022: import java.util.Dictionary;
023: import java.util.Enumeration;
024: import java.util.NoSuchElementException;
025:
026: // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
027:
028: /**
029: * A set of helper methods related to collection manipulation.
030: *
031: * @since Ant 1.5
032: */
033: public class CollectionUtils {
034:
035: /**
036: * Please use Vector.equals() or List.equals().
037: * @param v1 the first vector.
038: * @param v2 the second vector.
039: * @return true if the vectors are equal.
040: * @since Ant 1.5
041: * @deprecated since 1.6.x.
042: */
043: public static boolean equals(Vector v1, Vector v2) {
044: if (v1 == v2) {
045: return true;
046: }
047:
048: if (v1 == null || v2 == null) {
049: return false;
050: }
051:
052: return v1.equals(v2);
053: }
054:
055: /**
056: * Dictionary does not have an equals.
057: * Please use Map.equals().
058: *
059: * <p>Follows the equals contract of Java 2's Map.</p>
060: * @param d1 the first directory.
061: * @param d2 the second directory.
062: * @return true if the directories are equal.
063: * @since Ant 1.5
064: * @deprecated since 1.6.x.
065: */
066: public static boolean equals(Dictionary d1, Dictionary d2) {
067: if (d1 == d2) {
068: return true;
069: }
070:
071: if (d1 == null || d2 == null) {
072: return false;
073: }
074:
075: if (d1.size() != d2.size()) {
076: return false;
077: }
078:
079: Enumeration e1 = d1.keys();
080: while (e1.hasMoreElements()) {
081: Object key = e1.nextElement();
082: Object value1 = d1.get(key);
083: Object value2 = d2.get(key);
084: if (value2 == null || !value1.equals(value2)) {
085: return false;
086: }
087: }
088:
089: // don't need the opposite check as the Dictionaries have the
090: // same size, so we've also covered all keys of d2 already.
091:
092: return true;
093: }
094:
095: /**
096: * Dictionary does not know the putAll method. Please use Map.putAll().
097: * @param m1 the to directory.
098: * @param m2 the from directory.
099: * @since Ant 1.6
100: * @deprecated since 1.6.x.
101: */
102: public static void putAll(Dictionary m1, Dictionary m2) {
103: for (Enumeration it = m2.keys(); it.hasMoreElements();) {
104: Object key = it.nextElement();
105: m1.put(key, m2.get(key));
106: }
107: }
108:
109: /**
110: * An empty enumeration.
111: * @since Ant 1.6
112: */
113: public static final class EmptyEnumeration implements Enumeration {
114: /** Constructor for the EmptyEnumeration */
115: public EmptyEnumeration() {
116: }
117:
118: /**
119: * @return false always.
120: */
121: public boolean hasMoreElements() {
122: return false;
123: }
124:
125: /**
126: * @return nothing.
127: * @throws NoSuchElementException always.
128: */
129: public Object nextElement() throws NoSuchElementException {
130: throw new NoSuchElementException();
131: }
132: }
133:
134: /**
135: * Append one enumeration to another.
136: * Elements are evaluated lazily.
137: * @param e1 the first enumeration.
138: * @param e2 the subsequent enumeration.
139: * @return an enumeration representing e1 followed by e2.
140: * @since Ant 1.6.3
141: */
142: public static Enumeration append(Enumeration e1, Enumeration e2) {
143: return new CompoundEnumeration(e1, e2);
144: }
145:
146: /**
147: * Adapt the specified Iterator to the Enumeration interface.
148: * @param iter the Iterator to adapt.
149: * @return an Enumeration.
150: */
151: public static Enumeration asEnumeration(final Iterator iter) {
152: return new Enumeration() {
153: public boolean hasMoreElements() {
154: return iter.hasNext();
155: }
156:
157: public Object nextElement() {
158: return iter.next();
159: }
160: };
161: }
162:
163: /**
164: * Adapt the specified Enumeration to the Iterator interface.
165: * @param e the Enumeration to adapt.
166: * @return an Iterator.
167: */
168: public static Iterator asIterator(final Enumeration e) {
169: return new Iterator() {
170: public boolean hasNext() {
171: return e.hasMoreElements();
172: }
173:
174: public Object next() {
175: return e.nextElement();
176: }
177:
178: public void remove() {
179: throw new UnsupportedOperationException();
180: }
181: };
182: }
183:
184: private static final class CompoundEnumeration implements
185: Enumeration {
186:
187: private final Enumeration e1, e2;
188:
189: public CompoundEnumeration(Enumeration e1, Enumeration e2) {
190: this .e1 = e1;
191: this .e2 = e2;
192: }
193:
194: public boolean hasMoreElements() {
195: return e1.hasMoreElements() || e2.hasMoreElements();
196: }
197:
198: public Object nextElement() throws NoSuchElementException {
199: if (e1.hasMoreElements()) {
200: return e1.nextElement();
201: } else {
202: return e2.nextElement();
203: }
204: }
205:
206: }
207:
208: }
|