Source Code Cross Referenced for JumboEnumSet.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util;
027
028        /**
029         * Private implementation class for EnumSet, for "jumbo" enum types
030         * (i.e., those with more than 64 elements).
031         *
032         * @author Josh Bloch
033         * @since 1.5
034         * @serial exclude
035         */
036        class JumboEnumSet<E extends Enum<E>> extends EnumSet<E> {
037            /**
038             * Bit vector representation of this set.  The ith bit of the jth
039             * element of this array represents the  presence of universe[64*j +i]
040             * in this set.
041             */
042            private long elements[];
043
044            // Redundant - maintained for performance
045            private int size = 0;
046
047            JumboEnumSet(Class<E> elementType, Enum[] universe) {
048                super (elementType, universe);
049                elements = new long[(universe.length + 63) >>> 6];
050            }
051
052            void addRange(E from, E to) {
053                int fromIndex = from.ordinal() >>> 6;
054                int toIndex = to.ordinal() >>> 6;
055
056                if (fromIndex == toIndex) {
057                    elements[fromIndex] = (-1L >>> (from.ordinal()
058                            - to.ordinal() - 1)) << from.ordinal();
059                } else {
060                    elements[fromIndex] = (-1L << from.ordinal());
061                    for (int i = fromIndex + 1; i < toIndex; i++)
062                        elements[i] = -1;
063                    elements[toIndex] = -1L >>> (63 - to.ordinal());
064                }
065                size = to.ordinal() - from.ordinal() + 1;
066            }
067
068            void addAll() {
069                for (int i = 0; i < elements.length; i++)
070                    elements[i] = -1;
071                elements[elements.length - 1] >>>= -universe.length;
072                size = universe.length;
073            }
074
075            void complement() {
076                for (int i = 0; i < elements.length; i++)
077                    elements[i] = ~elements[i];
078                elements[elements.length - 1] &= (-1L >>> -universe.length);
079                size = universe.length - size;
080            }
081
082            /**
083             * Returns an iterator over the elements contained in this set.  The
084             * iterator traverses the elements in their <i>natural order</i> (which is
085             * the order in which the enum constants are declared). The returned
086             * Iterator is a "weakly consistent" iterator that will never throw {@link
087             * ConcurrentModificationException}.
088             *
089             * @return an iterator over the elements contained in this set
090             */
091            public Iterator<E> iterator() {
092                return new EnumSetIterator<E>();
093            }
094
095            private class EnumSetIterator<E extends Enum<E>> implements 
096                    Iterator<E> {
097                /**
098                 * A bit vector representing the elements in the current "word"
099                 * of the set not yet returned by this iterator.
100                 */
101                long unseen;
102
103                /**
104                 * The index corresponding to unseen in the elements array.
105                 */
106                int unseenIndex = 0;
107
108                /**
109                 * The bit representing the last element returned by this iterator
110                 * but not removed, or zero if no such element exists.
111                 */
112                long lastReturned = 0;
113
114                /**
115                 * The index corresponding to lastReturned in the elements array.
116                 */
117                int lastReturnedIndex = 0;
118
119                EnumSetIterator() {
120                    unseen = elements[0];
121                }
122
123                public boolean hasNext() {
124                    while (unseen == 0 && unseenIndex < elements.length - 1)
125                        unseen = elements[++unseenIndex];
126                    return unseen != 0;
127                }
128
129                public E next() {
130                    if (!hasNext())
131                        throw new NoSuchElementException();
132                    lastReturned = unseen & -unseen;
133                    lastReturnedIndex = unseenIndex;
134                    unseen -= lastReturned;
135                    return (E) universe[(lastReturnedIndex << 6)
136                            + Long.numberOfTrailingZeros(lastReturned)];
137                }
138
139                public void remove() {
140                    if (lastReturned == 0)
141                        throw new IllegalStateException();
142                    elements[lastReturnedIndex] -= lastReturned;
143                    size--;
144                    lastReturned = 0;
145                }
146            }
147
148            /**
149             * Returns the number of elements in this set.
150             *
151             * @return the number of elements in this set
152             */
153            public int size() {
154                return size;
155            }
156
157            /**
158             * Returns <tt>true</tt> if this set contains no elements.
159             *
160             * @return <tt>true</tt> if this set contains no elements
161             */
162            public boolean isEmpty() {
163                return size == 0;
164            }
165
166            /**
167             * Returns <tt>true</tt> if this set contains the specified element.
168             *
169             * @param e element to be checked for containment in this collection
170             * @return <tt>true</tt> if this set contains the specified element
171             */
172            public boolean contains(Object e) {
173                if (e == null)
174                    return false;
175                Class eClass = e.getClass();
176                if (eClass != elementType
177                        && eClass.getSuperclass() != elementType)
178                    return false;
179
180                int eOrdinal = ((Enum) e).ordinal();
181                return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;
182            }
183
184            // Modification Operations
185
186            /**
187             * Adds the specified element to this set if it is not already present.
188             *
189             * @param e element to be added to this set
190             * @return <tt>true</tt> if the set changed as a result of the call
191             *
192             * @throws NullPointerException if <tt>e</tt> is null
193             */
194            public boolean add(E e) {
195                typeCheck(e);
196
197                int eOrdinal = e.ordinal();
198                int eWordNum = eOrdinal >>> 6;
199
200                long oldElements = elements[eWordNum];
201                elements[eWordNum] |= (1L << eOrdinal);
202                boolean result = (elements[eWordNum] != oldElements);
203                if (result)
204                    size++;
205                return result;
206            }
207
208            /**
209             * Removes the specified element from this set if it is present.
210             *
211             * @param e element to be removed from this set, if present
212             * @return <tt>true</tt> if the set contained the specified element
213             */
214            public boolean remove(Object e) {
215                if (e == null)
216                    return false;
217                Class eClass = e.getClass();
218                if (eClass != elementType
219                        && eClass.getSuperclass() != elementType)
220                    return false;
221                int eOrdinal = ((Enum) e).ordinal();
222                int eWordNum = eOrdinal >>> 6;
223
224                long oldElements = elements[eWordNum];
225                elements[eWordNum] &= ~(1L << eOrdinal);
226                boolean result = (elements[eWordNum] != oldElements);
227                if (result)
228                    size--;
229                return result;
230            }
231
232            // Bulk Operations
233
234            /**
235             * Returns <tt>true</tt> if this set contains all of the elements
236             * in the specified collection.
237             *
238             * @param c collection to be checked for containment in this set
239             * @return <tt>true</tt> if this set contains all of the elements
240             *        in the specified collection
241             * @throws NullPointerException if the specified collection is null
242             */
243            public boolean containsAll(Collection<?> c) {
244                if (!(c instanceof  JumboEnumSet))
245                    return super .containsAll(c);
246
247                JumboEnumSet es = (JumboEnumSet) c;
248                if (es.elementType != elementType)
249                    return es.isEmpty();
250
251                for (int i = 0; i < elements.length; i++)
252                    if ((es.elements[i] & ~elements[i]) != 0)
253                        return false;
254                return true;
255            }
256
257            /**
258             * Adds all of the elements in the specified collection to this set.
259             *
260             * @param c collection whose elements are to be added to this set
261             * @return <tt>true</tt> if this set changed as a result of the call
262             * @throws NullPointerException if the specified collection or any of
263             *     its elements are null
264             */
265            public boolean addAll(Collection<? extends E> c) {
266                if (!(c instanceof  JumboEnumSet))
267                    return super .addAll(c);
268
269                JumboEnumSet es = (JumboEnumSet) c;
270                if (es.elementType != elementType) {
271                    if (es.isEmpty())
272                        return false;
273                    else
274                        throw new ClassCastException(es.elementType + " != "
275                                + elementType);
276                }
277
278                for (int i = 0; i < elements.length; i++)
279                    elements[i] |= es.elements[i];
280                return recalculateSize();
281            }
282
283            /**
284             * Removes from this set all of its elements that are contained in
285             * the specified collection.
286             *
287             * @param c elements to be removed from this set
288             * @return <tt>true</tt> if this set changed as a result of the call
289             * @throws NullPointerException if the specified collection is null
290             */
291            public boolean removeAll(Collection<?> c) {
292                if (!(c instanceof  JumboEnumSet))
293                    return super .removeAll(c);
294
295                JumboEnumSet es = (JumboEnumSet) c;
296                if (es.elementType != elementType)
297                    return false;
298
299                for (int i = 0; i < elements.length; i++)
300                    elements[i] &= ~es.elements[i];
301                return recalculateSize();
302            }
303
304            /**
305             * Retains only the elements in this set that are contained in the
306             * specified collection.
307             *
308             * @param c elements to be retained in this set
309             * @return <tt>true</tt> if this set changed as a result of the call
310             * @throws NullPointerException if the specified collection is null
311             */
312            public boolean retainAll(Collection<?> c) {
313                if (!(c instanceof  JumboEnumSet))
314                    return super .retainAll(c);
315
316                JumboEnumSet<?> es = (JumboEnumSet<?>) c;
317                if (es.elementType != elementType) {
318                    boolean changed = (size != 0);
319                    clear();
320                    return changed;
321                }
322
323                for (int i = 0; i < elements.length; i++)
324                    elements[i] &= es.elements[i];
325                return recalculateSize();
326            }
327
328            /**
329             * Removes all of the elements from this set.
330             */
331            public void clear() {
332                Arrays.fill(elements, 0);
333                size = 0;
334            }
335
336            /**
337             * Compares the specified object with this set for equality.  Returns
338             * <tt>true</tt> if the given object is also a set, the two sets have
339             * the same size, and every member of the given set is contained in
340             * this set.
341             *
342             * @param e object to be compared for equality with this set
343             * @return <tt>true</tt> if the specified object is equal to this set
344             */
345            public boolean equals(Object o) {
346                if (!(o instanceof  JumboEnumSet))
347                    return super .equals(o);
348
349                JumboEnumSet es = (JumboEnumSet) o;
350                if (es.elementType != elementType)
351                    return size == 0 && es.size == 0;
352
353                return Arrays.equals(es.elements, elements);
354            }
355
356            /**
357             * Recalculates the size of the set.  Returns true if it's changed.
358             */
359            private boolean recalculateSize() {
360                int oldSize = size;
361                size = 0;
362                for (long elt : elements)
363                    size += Long.bitCount(elt);
364
365                return size != oldSize;
366            }
367
368            public EnumSet<E> clone() {
369                JumboEnumSet<E> result = (JumboEnumSet<E>) super .clone();
370                result.elements = (long[]) result.elements.clone();
371                return result;
372            }
373        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.