1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.util;
|
---|
18 |
|
---|
19 | import java.util.Dictionary;
|
---|
20 | import java.util.Enumeration;
|
---|
21 | import java.util.NoSuchElementException;
|
---|
22 | import java.util.Vector;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A set of helper methods related to collection manipulation.
|
---|
26 | *
|
---|
27 | *
|
---|
28 | * @since Ant 1.5
|
---|
29 | */
|
---|
30 | public class CollectionUtils {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Please use Vector.equals() or List.equals()
|
---|
34 | *
|
---|
35 | * @since Ant 1.5
|
---|
36 | * @deprecated
|
---|
37 | */
|
---|
38 | public static boolean equals(Vector v1, Vector v2) {
|
---|
39 | if (v1 == v2) {
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (v1 == null || v2 == null) {
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 |
|
---|
47 | return v1.equals(v2);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Dictionary does not have an equals.
|
---|
52 | * Please use Map.equals()
|
---|
53 | *
|
---|
54 | * <p>Follows the equals contract of Java 2's Map.</p>
|
---|
55 | *
|
---|
56 | * @since Ant 1.5
|
---|
57 | * @deprecated
|
---|
58 | */
|
---|
59 | public static boolean equals(Dictionary d1, Dictionary d2) {
|
---|
60 | if (d1 == d2) {
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (d1 == null || d2 == null) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (d1.size() != d2.size()) {
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 |
|
---|
72 | Enumeration e1 = d1.keys();
|
---|
73 | while (e1.hasMoreElements()) {
|
---|
74 | Object key = e1.nextElement();
|
---|
75 | Object value1 = d1.get(key);
|
---|
76 | Object value2 = d2.get(key);
|
---|
77 | if (value2 == null || !value1.equals(value2)) {
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // don't need the opposite check as the Dictionaries have the
|
---|
83 | // same size, so we've also covered all keys of d2 already.
|
---|
84 |
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Dictionary does not know the putAll method. Please use Map.putAll().
|
---|
90 | *
|
---|
91 | * @since Ant 1.6
|
---|
92 | * @deprecated
|
---|
93 | */
|
---|
94 | public static void putAll(Dictionary m1, Dictionary m2) {
|
---|
95 | for (Enumeration it = m2.keys(); it.hasMoreElements();) {
|
---|
96 | Object key = it.nextElement();
|
---|
97 | m1.put(key, m2.get(key));
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * @since Ant 1.6
|
---|
103 | */
|
---|
104 | public static final class EmptyEnumeration implements Enumeration {
|
---|
105 | public EmptyEnumeration() {
|
---|
106 | }
|
---|
107 |
|
---|
108 | public boolean hasMoreElements() {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public Object nextElement() throws NoSuchElementException {
|
---|
113 | throw new NoSuchElementException();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Append one enumeration to another.
|
---|
119 | * Elements are evaluated lazily.
|
---|
120 | * @param e1 the first enumeration
|
---|
121 | * @param e2 the subsequent enumeration
|
---|
122 | * @return an enumeration representing e1 followed by e2
|
---|
123 | * @since Ant 1.6.3
|
---|
124 | */
|
---|
125 | public static Enumeration append(Enumeration e1, Enumeration e2) {
|
---|
126 | return new CompoundEnumeration(e1, e2);
|
---|
127 | }
|
---|
128 |
|
---|
129 | private static final class CompoundEnumeration implements Enumeration {
|
---|
130 |
|
---|
131 | private final Enumeration e1, e2;
|
---|
132 |
|
---|
133 | public CompoundEnumeration(Enumeration e1, Enumeration e2) {
|
---|
134 | this.e1 = e1;
|
---|
135 | this.e2 = e2;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public boolean hasMoreElements() {
|
---|
139 | return e1.hasMoreElements() || e2.hasMoreElements();
|
---|
140 | }
|
---|
141 |
|
---|
142 | public Object nextElement() throws NoSuchElementException {
|
---|
143 | if (e1.hasMoreElements()) {
|
---|
144 | return e1.nextElement();
|
---|
145 | } else {
|
---|
146 | return e2.nextElement();
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|