1 18 package org.apache.tools.ant.util; 19 20 import java.util.Vector ; 21 import java.util.Iterator ; 22 import java.util.Dictionary ; 23 import java.util.Enumeration ; 24 import java.util.NoSuchElementException ; 25 26 28 33 public class CollectionUtils { 34 35 43 public static boolean equals(Vector v1, Vector 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 66 public static boolean equals(Dictionary d1, Dictionary 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 e1 = d1.keys(); 80 while (e1.hasMoreElements()) { 81 Object key = e1.nextElement(); 82 Object value1 = d1.get(key); 83 Object value2 = d2.get(key); 84 if (value2 == null || !value1.equals(value2)) { 85 return false; 86 } 87 } 88 89 92 return true; 93 } 94 95 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 113 public static final class EmptyEnumeration implements Enumeration { 114 115 public EmptyEnumeration() { 116 } 117 118 121 public boolean hasMoreElements() { 122 return false; 123 } 124 125 129 public Object nextElement() throws NoSuchElementException { 130 throw new NoSuchElementException (); 131 } 132 } 133 134 142 public static Enumeration append(Enumeration e1, Enumeration e2) { 143 return new CompoundEnumeration(e1, e2); 144 } 145 146 151 public static Enumeration asEnumeration(final Iterator iter) { 152 return new Enumeration () { 153 public boolean hasMoreElements() { 154 return iter.hasNext(); 155 } 156 public Object nextElement() { 157 return iter.next(); 158 } 159 }; 160 } 161 162 167 public static Iterator asIterator(final Enumeration e) { 168 return new Iterator () { 169 public boolean hasNext() { 170 return e.hasMoreElements(); 171 } 172 public Object next() { 173 return e.nextElement(); 174 } 175 public void remove() { 176 throw new UnsupportedOperationException (); 177 } 178 }; 179 } 180 181 private static final class CompoundEnumeration implements Enumeration { 182 183 private final Enumeration e1, e2; 184 185 public CompoundEnumeration(Enumeration e1, Enumeration 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 nextElement() throws NoSuchElementException { 195 if (e1.hasMoreElements()) { 196 return e1.nextElement(); 197 } else { 198 return e2.nextElement(); 199 } 200 } 201 202 } 203 204 } 205 | Popular Tags |