KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > CollectionUtils


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.util;
19
20 import java.util.Vector JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Dictionary JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
27

28 /**
29  * A set of helper methods related to collection manipulation.
30  *
31  * @since Ant 1.5
32  */

33 public class CollectionUtils {
34
35     /**
36      * Please use Vector.equals() or List.equals().
37      * @param v1 the first vector.
38      * @param v2 the second vector.
39      * @return true if the vectors are equal.
40      * @since Ant 1.5
41      * @deprecated since 1.6.x.
42      */

43     public static boolean equals(Vector JavaDoc v1, Vector JavaDoc v2) {
44         if (v1 == v2) {
45             return true;
46         }
47
48         if (v1 == null || v2 == null) {
49             return false;
50         }
51
52         return v1.equals(v2);
53     }
54
55     /**
56      * Dictionary does not have an equals.
57      * Please use Map.equals().
58      *
59      * <p>Follows the equals contract of Java 2's Map.</p>
60      * @param d1 the first directory.
61      * @param d2 the second directory.
62      * @return true if the directories are equal.
63      * @since Ant 1.5
64      * @deprecated since 1.6.x.
65      */

66     public static boolean equals(Dictionary JavaDoc d1, Dictionary JavaDoc d2) {
67         if (d1 == d2) {
68             return true;
69         }
70
71         if (d1 == null || d2 == null) {
72             return false;
73         }
74
75         if (d1.size() != d2.size()) {
76             return false;
77         }
78
79         Enumeration JavaDoc e1 = d1.keys();
80         while (e1.hasMoreElements()) {
81             Object JavaDoc key = e1.nextElement();
82             Object JavaDoc value1 = d1.get(key);
83             Object JavaDoc value2 = d2.get(key);
84             if (value2 == null || !value1.equals(value2)) {
85                 return false;
86             }
87         }
88
89         // don't need the opposite check as the Dictionaries have the
90
// same size, so we've also covered all keys of d2 already.
91

92         return true;
93     }
94
95     /**
96      * Dictionary does not know the putAll method. Please use Map.putAll().
97      * @param m1 the to directory.
98      * @param m2 the from directory.
99      * @since Ant 1.6
100      * @deprecated since 1.6.x.
101      */

102     public static void putAll(Dictionary JavaDoc m1, Dictionary JavaDoc m2) {
103         for (Enumeration JavaDoc it = m2.keys(); it.hasMoreElements();) {
104             Object JavaDoc 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 JavaDoc {
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 JavaDoc nextElement() throws NoSuchElementException JavaDoc {
130             throw new NoSuchElementException JavaDoc();
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 JavaDoc append(Enumeration JavaDoc e1, Enumeration JavaDoc 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 JavaDoc asEnumeration(final Iterator JavaDoc iter) {
152         return new Enumeration JavaDoc() {
153             public boolean hasMoreElements() {
154                 return iter.hasNext();
155             }
156             public Object JavaDoc nextElement() {
157                 return iter.next();
158             }
159         };
160     }
161
162     /**
163      * Adapt the specified Enumeration to the Iterator interface.
164      * @param e the Enumeration to adapt.
165      * @return an Iterator.
166      */

167     public static Iterator JavaDoc asIterator(final Enumeration JavaDoc e) {
168         return new Iterator JavaDoc() {
169             public boolean hasNext() {
170                 return e.hasMoreElements();
171             }
172             public Object JavaDoc next() {
173                 return e.nextElement();
174             }
175             public void remove() {
176                 throw new UnsupportedOperationException JavaDoc();
177             }
178         };
179     }
180
181     private static final class CompoundEnumeration implements Enumeration JavaDoc {
182
183         private final Enumeration JavaDoc e1, e2;
184
185         public CompoundEnumeration(Enumeration JavaDoc e1, Enumeration JavaDoc e2) {
186             this.e1 = e1;
187             this.e2 = e2;
188         }
189
190         public boolean hasMoreElements() {
191             return e1.hasMoreElements() || e2.hasMoreElements();
192         }
193
194         public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
195             if (e1.hasMoreElements()) {
196                 return e1.nextElement();
197             } else {
198                 return e2.nextElement();
199             }
200         }
201
202     }
203
204 }
205
Popular Tags