KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > RegularEnumSet


1 /*
2  * @(#)RegularEnumSet.java 1.6 04/05/28
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util;
9
10 /**
11  * Private implementation class for EnumSet, for "regular sized" enum types
12  * (i.e., those with 64 or fewer enum constants).
13  *
14  * @author Josh Bloch
15  * @since 1.5
16  * @serial exclude
17  */

18 class RegularEnumSet<E extends Enum JavaDoc<E>> extends EnumSet JavaDoc<E> {
19     /**
20      * Bit vector representation of this set. The 2^k bit indicates the
21      * presence of universe[k] in this set.
22      */

23     private long elements = 0L;
24
25     RegularEnumSet(Class JavaDoc<E>elementType, Enum JavaDoc[] universe) {
26         super(elementType, universe);
27     }
28
29     void addRange(E from, E to) {
30         elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal();
31     }
32
33     void addAll() {
34         if (universe.length != 0)
35             elements = -1L >>> -universe.length;
36     }
37
38     void complement() {
39         if (universe.length != 0) {
40             elements = ~elements;
41             elements &= -1L >>> -universe.length; // Mask unused bits
42
}
43     }
44
45     /**
46      * Returns an iterator over the elements contained in this set. The
47      * iterator traverses the elements in their <i>natural order</i> (which is
48      * the order in which the enum constants are declared). The returned
49      * Iterator is a "snapshot" iterator that will never throw {@link
50      * ConcurrentModificationException}; the elements are traversed as they
51      * existed when this call was invoked.
52      *
53      * @return an iterator over the elements contained in this set
54      */

55     public Iterator JavaDoc<E> iterator() {
56         return new EnumSetIterator<E>();
57     }
58
59     private class EnumSetIterator<E extends Enum JavaDoc<E>> implements Iterator JavaDoc<E> {
60         /**
61          * A bit vector representing the elements in the set not yet
62          * returned by this iterator.
63          */

64         long unseen;
65
66         /**
67          * The bit representing the last element returned by this iterator
68          * but not removed, or zero if no such element exists.
69          */

70         long lastReturned = 0;
71
72         EnumSetIterator() {
73             unseen = elements;
74         }
75
76         public boolean hasNext() {
77             return unseen != 0;
78         }
79
80         public E next() {
81             if (unseen == 0)
82                 throw new NoSuchElementException JavaDoc();
83             lastReturned = unseen & -unseen;
84             unseen -= lastReturned;
85             return (E) universe[Long.numberOfTrailingZeros(lastReturned)];
86         }
87
88         public void remove() {
89             if (lastReturned == 0)
90                 throw new IllegalStateException JavaDoc();
91             elements -= lastReturned;
92             lastReturned = 0;
93         }
94     }
95
96     /**
97      * Returns the number of elements in this set.
98      *
99      * @return the number of elements in this set
100      */

101     public int size() {
102         return Long.bitCount(elements);
103     }
104
105     /**
106      * Returns <tt>true</tt> if this set contains no elements.
107      *
108      * @return <tt>true</tt> if this set contains no elements
109      */

110     public boolean isEmpty() {
111         return elements == 0;
112     }
113
114     /**
115      * Returns <tt>true</tt> if this set contains the specified element.
116      *
117      * @param e element to be checked for containment in this collection
118      * @return <tt>true</tt> if this set contains the specified element
119      */

120     public boolean contains(Object JavaDoc e) {
121         if (e == null)
122             return false;
123         Class JavaDoc eClass = e.getClass();
124         if (eClass != elementType && eClass.getSuperclass() != elementType)
125             return false;
126
127         return (elements & (1L << ((Enum JavaDoc)e).ordinal())) != 0;
128     }
129
130     // Modification Operations
131

132     /**
133      * Adds the specified element to this set if it is not already present.
134      *
135      * @param e element to be added to this set
136      * @return <tt>true</tt> if the set changed as a result of the call
137      *
138      * @throws NullPointerException if <tt>e</tt> is null
139      */

140     public boolean add(E e) {
141         typeCheck(e);
142
143         long oldElements = elements;
144         elements |= (1L << ((Enum JavaDoc)e).ordinal());
145         return elements != oldElements;
146     }
147
148     /**
149      * Removes the specified element from this set if it is present.
150      *
151      * @param e element to be removed from this set, if present
152      * @return <tt>true</tt> if the set contained the specified element
153      */

154     public boolean remove(Object JavaDoc e) {
155         if (e == null)
156             return false;
157         Class JavaDoc eClass = e.getClass();
158         if (eClass != elementType && eClass.getSuperclass() != elementType)
159             return false;
160
161         long oldElements = elements;
162         elements &= ~(1L << ((Enum JavaDoc)e).ordinal());
163         return elements != oldElements;
164     }
165
166     // Bulk Operations
167

168     /**
169      * Returns <tt>true</tt> if this set contains all of the elements
170      * in the specified collection.
171      *
172      * @param c collection to be checked for containment in this set
173      * @return <tt>true</tt> if this set contains all of the elements
174      * in the specified collection
175      * @throws NullPointerException if the specified collection is null
176      */

177     public boolean containsAll(Collection JavaDoc<?> c) {
178         if (!(c instanceof RegularEnumSet JavaDoc))
179             return super.containsAll(c);
180
181         RegularEnumSet JavaDoc es = (RegularEnumSet JavaDoc)c;
182         if (es.elementType != elementType)
183             return es.isEmpty();
184
185         return (es.elements & ~elements) == 0;
186     }
187
188     /**
189      * Adds all of the elements in the specified collection to this set.
190      *
191      * @param c collection whose elements are to be added to this set
192      * @return <tt>true</tt> if this set changed as a result of the call
193      * @throws NullPointerException if the specified collection or any
194      * of its elements are null
195      */

196     public boolean addAll(Collection JavaDoc<? extends E> c) {
197         if (!(c instanceof RegularEnumSet JavaDoc))
198             return super.addAll(c);
199
200         RegularEnumSet JavaDoc es = (RegularEnumSet JavaDoc)c;
201         if (es.elementType != elementType) {
202             if (es.isEmpty())
203                 return false;
204             else
205                 throw new ClassCastException JavaDoc(
206                     es.elementType + " != " + elementType);
207         }
208
209         long oldElements = elements;
210         elements |= es.elements;
211         return elements != oldElements;
212     }
213
214     /**
215      * Removes from this set all of its elements that are contained in
216      * the specified collection.
217      *
218      * @param c elements to be removed from this set
219      * @return <tt>true</tt> if this set changed as a result of the call
220      * @throws NullPointerException if the specified collection is null
221      */

222     public boolean removeAll(Collection JavaDoc<?> c) {
223         if (!(c instanceof RegularEnumSet JavaDoc))
224             return super.removeAll(c);
225
226         RegularEnumSet JavaDoc es = (RegularEnumSet JavaDoc)c;
227         if (es.elementType != elementType)
228             return false;
229
230         long oldElements = elements;
231         elements &= ~es.elements;
232         return elements != oldElements;
233     }
234
235     /**
236      * Retains only the elements in this set that are contained in the
237      * specified collection.
238      *
239      * @param c elements to be retained in this set
240      * @return <tt>true</tt> if this set changed as a result of the call
241      * @throws NullPointerException if the specified collection is null
242      */

243     public boolean retainAll(Collection JavaDoc<?> c) {
244         if (!(c instanceof RegularEnumSet JavaDoc))
245             return super.retainAll(c);
246
247         RegularEnumSet JavaDoc es = (RegularEnumSet JavaDoc)c;
248         if (es.elementType != elementType) {
249             elements = 0;
250             return true;
251         }
252
253         long oldElements = elements;
254         elements &= es.elements;
255         return elements != oldElements;
256     }
257
258     /**
259      * Removes all of the elements from this set.
260      */

261     public void clear() {
262         elements = 0;
263     }
264
265     /**
266      * Compares the specified object with this set for equality. Returns
267      * <tt>true</tt> if the given object is also a set, the two sets have
268      * the same size, and every member of the given set is contained in
269      * this set.
270      *
271      * @param e object to be compared for equality with this set
272      * @return <tt>true</tt> if the specified object is equal to this set
273      */

274     public boolean equals(Object JavaDoc o) {
275         if (!(o instanceof RegularEnumSet JavaDoc))
276             return super.equals(o);
277
278         RegularEnumSet JavaDoc es = (RegularEnumSet JavaDoc)o;
279         if (es.elementType != elementType)
280             return elements == 0 && es.elements == 0;
281         return es.elements == elements;
282     }
283 }
284
Popular Tags