KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > EnumSet_


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.util;
33
34 import java.util.*;
35
36 /**
37  * @author Taras Puchko
38  */

39 public class EnumSet_<E extends Enum JavaDoc<E>> extends HashSet<E> {
40
41     private static final long serialVersionUID = 7684628957901243852L;
42
43     private Class JavaDoc<E> elementType;
44
45     private EnumSet_(Class JavaDoc<E> elementType) {
46         if (!elementType.isEnum()) {
47             throw new ClassCastException JavaDoc();
48         }
49         this.elementType = elementType;
50     }
51
52     public static <E extends Enum JavaDoc<E>> EnumSet_<E> allOf(Class JavaDoc<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 JavaDoc<E>> EnumSet_<E> complementOf(EnumSet_<E> enumSet) {
61         Class JavaDoc<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 JavaDoc<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 JavaDoc();
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 JavaDoc<E>> EnumSet_<E> copyOf(EnumSet_<E> enumSet) {
87         return enumSet.clone();
88     }
89
90     public static <E extends Enum JavaDoc<E>> EnumSet_<E> noneOf(Class JavaDoc<E> elementType) {
91         return new EnumSet_<E>(elementType);
92     }
93
94     public static <E extends Enum JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc();
142         Class JavaDoc<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 JavaDoc();
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 JavaDoc> ENUM_COMPARATOR = new Comparator<Enum JavaDoc>() {
169         public int compare(Enum JavaDoc o1, Enum JavaDoc 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