1 7 8 package java.util; 9 10 62 public abstract class EnumSet<E extends Enum <E>> extends AbstractSet <E> 63 implements Cloneable , java.io.Serializable 64 { 65 68 final Class <E> elementType; 69 70 73 final Enum [] universe; 74 75 private static Enum [] ZERO_LENGTH_ENUM_ARRAY = new Enum [0]; 76 77 EnumSet(Class <E>elementType, Enum [] universe) { 78 this.elementType = elementType; 79 this.universe = universe; 80 } 81 82 89 public static <E extends Enum <E>> EnumSet <E> noneOf(Class <E> elementType) { 90 Enum [] universe = elementType.getEnumConstants(); 91 if (universe == null) 92 throw new ClassCastException (elementType + " not an enum"); 93 94 if (universe.length <= 64) 95 return new RegularEnumSet <E>(elementType, universe); 96 else 97 return new JumboEnumSet <E>(elementType, universe); 98 } 99 100 108 public static <E extends Enum <E>> EnumSet <E> allOf(Class <E> elementType) { 109 EnumSet <E> result = noneOf(elementType); 110 result.addAll(); 111 return result; 112 } 113 114 118 abstract void addAll(); 119 120 127 public static <E extends Enum <E>> EnumSet <E> copyOf(EnumSet <E> s) { 128 return s.clone(); 129 } 130 131 143 public static <E extends Enum <E>> EnumSet <E> copyOf(Collection <E> c) { 144 if (c instanceof EnumSet ) { 145 return ((EnumSet <E>)c).clone(); 146 } else { 147 if (c.isEmpty()) 148 throw new IllegalArgumentException ("Collection is empty"); 149 Iterator <E> i = c.iterator(); 150 E first = i.next(); 151 EnumSet <E> result = EnumSet.of(first); 152 while (i.hasNext()) 153 result.add(i.next()); 154 return result; 155 } 156 } 157 158 166 public static <E extends Enum <E>> EnumSet <E> complementOf(EnumSet <E> s) { 167 EnumSet <E> result = copyOf(s); 168 result.complement(); 169 return result; 170 } 171 172 185 public static <E extends Enum <E>> EnumSet <E> of(E e) { 186 EnumSet <E> result = noneOf(e.getDeclaringClass()); 187 result.add(e); 188 return result; 189 } 190 191 205 public static <E extends Enum <E>> EnumSet <E> of(E e1, E e2) { 206 EnumSet <E> result = noneOf(e1.getDeclaringClass()); 207 result.add(e1); 208 result.add(e2); 209 return result; 210 } 211 212 227 public static <E extends Enum <E>> EnumSet <E> of(E e1, E e2, E e3) { 228 EnumSet <E> result = noneOf(e1.getDeclaringClass()); 229 result.add(e1); 230 result.add(e2); 231 result.add(e3); 232 return result; 233 } 234 235 251 public static <E extends Enum <E>> EnumSet <E> of(E e1, E e2, E e3, E e4) { 252 EnumSet <E> result = noneOf(e1.getDeclaringClass()); 253 result.add(e1); 254 result.add(e2); 255 result.add(e3); 256 result.add(e4); 257 return result; 258 } 259 260 277 public static <E extends Enum <E>> EnumSet <E> of(E e1, E e2, E e3, E e4, 278 E e5) 279 { 280 EnumSet <E> result = noneOf(e1.getDeclaringClass()); 281 result.add(e1); 282 result.add(e2); 283 result.add(e3); 284 result.add(e4); 285 result.add(e5); 286 return result; 287 } 288 289 302 public static <E extends Enum <E>> EnumSet <E> of(E first, E... rest) { 303 EnumSet <E> result = noneOf(first.getDeclaringClass()); 304 result.add(first); 305 for (E e : rest) 306 result.add(e); 307 return result; 308 } 309 310 324 public static <E extends Enum <E>> EnumSet <E> range(E from, E to) { 325 if (from.compareTo(to) > 0) 326 throw new IllegalArgumentException (from + " > " + to); 327 EnumSet <E> result = noneOf(from.getDeclaringClass()); 328 result.addRange(from, to); 329 return result; 330 } 331 332 336 abstract void addRange(E from, E to); 337 338 343 public EnumSet <E> clone() { 344 try { 345 return (EnumSet <E>) super.clone(); 346 } catch(CloneNotSupportedException e) { 347 throw new AssertionError (e); 348 } 349 } 350 351 354 abstract void complement(); 355 356 359 final void typeCheck(E e) { 360 Class eClass = e.getClass(); 361 if (eClass != elementType && eClass.getSuperclass() != elementType) 362 throw new ClassCastException (eClass + " != " + elementType); 363 } 364 365 374 private static class SerializationProxy <E extends Enum <E>> 375 implements java.io.Serializable 376 { 377 382 private final Class <E> elementType; 383 384 389 private final Enum [] elements; 390 391 SerializationProxy(EnumSet <E> set) { 392 elementType = set.elementType; 393 elements = (Enum []) set.toArray(ZERO_LENGTH_ENUM_ARRAY); 394 } 395 396 private Object readResolve() { 397 EnumSet <E> result = EnumSet.noneOf(elementType); 398 for (Enum e : elements) 399 result.add((E)e); 400 return result; 401 } 402 403 private static final long serialVersionUID = 362491234563181265L; 404 } 405 406 Object writeReplace() { 407 return new SerializationProxy<E>(this); 408 } 409 } 410 | Popular Tags |