1 32 package net.sf.retrotranslator.runtime.java.util; 33 34 import java.util.*; 35 36 39 public class EnumSet_<E extends Enum <E>> extends HashSet<E> { 40 41 private static final long serialVersionUID = 7684628957901243852L; 42 43 private Class <E> elementType; 44 45 private EnumSet_(Class <E> elementType) { 46 if (!elementType.isEnum()) { 47 throw new ClassCastException (); 48 } 49 this.elementType = elementType; 50 } 51 52 public static <E extends Enum <E>> EnumSet_<E> allOf(Class <E> elementType) { 53 EnumSet_<E> result = new EnumSet_<E>(elementType); 54 for (E e : elementType.getEnumConstants()) { 55 result.add(e); 56 } 57 return result; 58 } 59 60 public static <E extends Enum <E>> EnumSet_<E> complementOf(EnumSet_<E> enumSet) { 61 Class <E> elementType = enumSet.elementType; 62 EnumSet_<E> result = new EnumSet_<E>(elementType); 63 for (E e : elementType.getEnumConstants()) { 64 if (!enumSet.contains(e)) { 65 result.add(e); 66 } 67 } 68 return result; 69 } 70 71 public static <E extends Enum <E>> EnumSet_<E> copyOf(Collection<E> collection) { 72 if (collection instanceof EnumSet_) { 73 return copyOf((EnumSet_<E>) collection); 74 } 75 Iterator<E> iterator = collection.iterator(); 76 if (!iterator.hasNext()) { 77 throw new IllegalArgumentException (); 78 } 79 EnumSet_<E> result = EnumSet_.of(iterator.next()); 80 while (iterator.hasNext()) { 81 result.add(iterator.next()); 82 } 83 return result; 84 } 85 86 public static <E extends Enum <E>> EnumSet_<E> copyOf(EnumSet_<E> enumSet) { 87 return enumSet.clone(); 88 } 89 90 public static <E extends Enum <E>> EnumSet_<E> noneOf(Class <E> elementType) { 91 return new EnumSet_<E>(elementType); 92 } 93 94 public static <E extends Enum <E>> EnumSet_<E> of(E e) { 95 EnumSet_<E> result = new EnumSet_<E>(e.getDeclaringClass()); 96 result.add(e); 97 return result; 98 } 99 100 public static <E extends Enum <E>> EnumSet_<E> of(E e1, E e2) { 101 EnumSet_<E> result = of(e1); 102 result.add(e2); 103 return result; 104 } 105 106 public static <E extends Enum <E>> EnumSet_<E> of(E e1, E e2, E e3) { 107 EnumSet_<E> result = of(e1); 108 result.add(e2); 109 result.add(e3); 110 return result; 111 } 112 113 public static <E extends Enum <E>> EnumSet_<E> of(E e1, E e2, E e3, E e4) { 114 EnumSet_<E> result = of(e1); 115 result.add(e2); 116 result.add(e3); 117 result.add(e4); 118 return result; 119 } 120 121 public static <E extends Enum <E>> EnumSet_<E> of(E e1, E e2, E e3, E e4, E e5) { 122 EnumSet_<E> result = of(e1); 123 result.add(e2); 124 result.add(e3); 125 result.add(e4); 126 result.add(e5); 127 return result; 128 } 129 130 public static <E extends Enum <E>> EnumSet_<E> of(E first, E... rest) { 131 EnumSet_<E> result = of(first); 132 for (E e : rest) { 133 result.add(e); 134 } 135 return result; 136 } 137 138 public static <E extends Enum <E>> EnumSet_<E> range(E from, E to) { 139 int fromIndex = from.ordinal(); 140 int toIndex = to.ordinal(); 141 if (fromIndex > toIndex) throw new IllegalArgumentException (); 142 Class <E> elementType = from.getDeclaringClass(); 143 E[] enumConstants = elementType.getEnumConstants(); 144 EnumSet_<E> result = new EnumSet_<E>(elementType); 145 for (int i = fromIndex; i <= toIndex; i++) { 146 E enumConstant = enumConstants[i]; 147 result.add(enumConstant); 148 } 149 return result; 150 } 151 152 public boolean add(E o) { 153 if (o == null) { 154 throw new NullPointerException (); 155 } 156 return super.add(elementType.cast(o)); 157 } 158 159 public Iterator<E> iterator() { 160 TreeSet<E> treeSet = new TreeSet<E>(ENUM_COMPARATOR); 161 Iterator<E> iterator = super.iterator(); 162 while (iterator.hasNext()) { 163 treeSet.add(iterator.next()); 164 } 165 return treeSet.iterator(); 166 } 167 168 private static final Comparator<Enum > ENUM_COMPARATOR = new Comparator<Enum >() { 169 public int compare(Enum o1, Enum o2) { 170 return o1.ordinal() - o2.ordinal(); 171 } 172 }; 173 174 public EnumSet_<E> clone() { 175 return (EnumSet_<E>) super.clone(); 176 } 177 178 } 179 | Popular Tags |