001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import java.util.*;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class EnumSet_<E extends Enum<E>> extends HashSet<E> {
039:
040: private static final long serialVersionUID = 7684628957901243852L;
041:
042: private Class<E> elementType;
043:
044: private EnumSet_(Class<E> elementType) {
045: if (!Enum.class.isAssignableFrom(elementType)) {
046: throw new ClassCastException();
047: }
048: this .elementType = elementType;
049: }
050:
051: public static <E extends Enum<E>> EnumSet_<E> allOf(
052: Class<E> elementType) {
053: EnumSet_<E> result = new EnumSet_<E>(elementType);
054: for (E e : elementType.getEnumConstants()) {
055: result.add(e);
056: }
057: return result;
058: }
059:
060: public static <E extends Enum<E>> EnumSet_<E> complementOf(
061: EnumSet_<E> enumSet) {
062: Class<E> elementType = enumSet.elementType;
063: EnumSet_<E> result = new EnumSet_<E>(elementType);
064: for (E e : elementType.getEnumConstants()) {
065: if (!enumSet.contains(e)) {
066: result.add(e);
067: }
068: }
069: return result;
070: }
071:
072: public static <E extends Enum<E>> EnumSet_<E> copyOf(
073: Collection<E> collection) {
074: if (collection instanceof EnumSet_) {
075: return copyOf((EnumSet_<E>) collection);
076: }
077: Iterator<E> iterator = collection.iterator();
078: if (!iterator.hasNext()) {
079: throw new IllegalArgumentException();
080: }
081: EnumSet_<E> result = EnumSet_.of(iterator.next());
082: while (iterator.hasNext()) {
083: result.add(iterator.next());
084: }
085: return result;
086: }
087:
088: public static <E extends Enum<E>> EnumSet_<E> copyOf(
089: EnumSet_<E> enumSet) {
090: return enumSet.clone();
091: }
092:
093: public static <E extends Enum<E>> EnumSet_<E> noneOf(
094: Class<E> elementType) {
095: return new EnumSet_<E>(elementType);
096: }
097:
098: public static <E extends Enum<E>> EnumSet_<E> of(E e) {
099: EnumSet_<E> result = new EnumSet_<E>(e.getDeclaringClass());
100: result.add(e);
101: return result;
102: }
103:
104: public static <E extends Enum<E>> EnumSet_<E> of(E e1, E e2) {
105: EnumSet_<E> result = of(e1);
106: result.add(e2);
107: return result;
108: }
109:
110: public static <E extends Enum<E>> EnumSet_<E> of(E e1, E e2, E e3) {
111: EnumSet_<E> result = of(e1);
112: result.add(e2);
113: result.add(e3);
114: return result;
115: }
116:
117: public static <E extends Enum<E>> EnumSet_<E> of(E e1, E e2, E e3,
118: E e4) {
119: EnumSet_<E> result = of(e1);
120: result.add(e2);
121: result.add(e3);
122: result.add(e4);
123: return result;
124: }
125:
126: public static <E extends Enum<E>> EnumSet_<E> of(E e1, E e2, E e3,
127: E e4, E e5) {
128: EnumSet_<E> result = of(e1);
129: result.add(e2);
130: result.add(e3);
131: result.add(e4);
132: result.add(e5);
133: return result;
134: }
135:
136: public static <E extends Enum<E>> EnumSet_<E> of(E first, E... rest) {
137: EnumSet_<E> result = of(first);
138: for (E e : rest) {
139: result.add(e);
140: }
141: return result;
142: }
143:
144: public static <E extends Enum<E>> EnumSet_<E> range(E from, E to) {
145: int fromIndex = from.ordinal();
146: int toIndex = to.ordinal();
147: if (fromIndex > toIndex)
148: throw new IllegalArgumentException();
149: Class<E> elementType = from.getDeclaringClass();
150: E[] enumConstants = elementType.getEnumConstants();
151: EnumSet_<E> result = new EnumSet_<E>(elementType);
152: for (int i = fromIndex; i <= toIndex; i++) {
153: E enumConstant = enumConstants[i];
154: result.add(enumConstant);
155: }
156: return result;
157: }
158:
159: public boolean add(E o) {
160: if (o == null) {
161: throw new NullPointerException();
162: }
163: return super .add(elementType.cast(o));
164: }
165:
166: public Iterator<E> iterator() {
167: TreeSet<E> treeSet = new TreeSet<E>(ENUM_COMPARATOR);
168: Iterator<E> iterator = super .iterator();
169: while (iterator.hasNext()) {
170: treeSet.add(iterator.next());
171: }
172: return treeSet.iterator();
173: }
174:
175: private static final Comparator<Enum> ENUM_COMPARATOR = new Comparator<Enum>() {
176: public int compare(Enum o1, Enum o2) {
177: return o1.ordinal() - o2.ordinal();
178: }
179: };
180:
181: public EnumSet_<E> clone() {
182: return (EnumSet_<E>) super.clone();
183: }
184:
185: }
|