src/java.base/share/classes/java/util/JumboEnumSet.java
author jlaskey
Thu, 14 Nov 2019 12:50:08 -0400
branchJDK-8193209-branch
changeset 59088 da026c172c1e
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
add missing files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Private implementation class for EnumSet, for "jumbo" enum types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * (i.e., those with more than 64 elements).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class JumboEnumSet<E extends Enum<E>> extends EnumSet<E> {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
    37
    @java.io.Serial
8800
e658ae923d53 7028133: Specify serialVersionUID for RegularEnumSet and JumboEnumSet
mduigou
parents: 8787
diff changeset
    38
    private static final long serialVersionUID = 334349849919042784L;
e658ae923d53 7028133: Specify serialVersionUID for RegularEnumSet and JumboEnumSet
mduigou
parents: 8787
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Bit vector representation of this set.  The ith bit of the jth
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * element of this array represents the  presence of universe[64*j +i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * in this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private long elements[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // Redundant - maintained for performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
    50
    JumboEnumSet(Class<E>elementType, Enum<?>[] universe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super(elementType, universe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        elements = new long[(universe.length + 63) >>> 6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    void addRange(E from, E to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int fromIndex = from.ordinal() >>> 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int toIndex = to.ordinal() >>> 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (fromIndex == toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            elements[fromIndex] = (-1L >>>  (from.ordinal() - to.ordinal() - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                            << from.ordinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            elements[fromIndex] = (-1L << from.ordinal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            for (int i = fromIndex + 1; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                elements[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            elements[toIndex] = -1L >>> (63 - to.ordinal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        size = to.ordinal() - from.ordinal() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    void addAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i = 0; i < elements.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            elements[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        elements[elements.length - 1] >>>= -universe.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        size = universe.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    void complement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (int i = 0; i < elements.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            elements[i] = ~elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        elements[elements.length - 1] &= (-1L >>> -universe.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        size = universe.length - size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns an iterator over the elements contained in this set.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * iterator traverses the elements in their <i>natural order</i> (which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * the order in which the enum constants are declared). The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Iterator is a "weakly consistent" iterator that will never throw {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * ConcurrentModificationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return an iterator over the elements contained in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public Iterator<E> iterator() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    95
        return new EnumSetIterator<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * A bit vector representing the elements in the current "word"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * of the set not yet returned by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        long unseen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * The index corresponding to unseen in the elements array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int unseenIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * The bit representing the last element returned by this iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * but not removed, or zero if no such element exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        long lastReturned = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * The index corresponding to lastReturned in the elements array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int lastReturnedIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        EnumSetIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            unseen = elements[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   125
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            while (unseen == 0 && unseenIndex < elements.length - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                unseen = elements[++unseenIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return unseen != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   132
        @Override
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   133
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            lastReturned = unseen & -unseen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            lastReturnedIndex = unseenIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            unseen -= lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return (E) universe[(lastReturnedIndex << 6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                + Long.numberOfTrailingZeros(lastReturned)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   144
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (lastReturned == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new IllegalStateException();
8787
4b055daf8dd0 7014637: Improve behavior of EnumSet Iterator.remove()
mduigou
parents: 7803
diff changeset
   148
            final long oldElements = elements[lastReturnedIndex];
4b055daf8dd0 7014637: Improve behavior of EnumSet Iterator.remove()
mduigou
parents: 7803
diff changeset
   149
            elements[lastReturnedIndex] &= ~lastReturned;
4b055daf8dd0 7014637: Improve behavior of EnumSet Iterator.remove()
mduigou
parents: 7803
diff changeset
   150
            if (oldElements != elements[lastReturnedIndex]) {
4b055daf8dd0 7014637: Improve behavior of EnumSet Iterator.remove()
mduigou
parents: 7803
diff changeset
   151
                size--;
4b055daf8dd0 7014637: Improve behavior of EnumSet Iterator.remove()
mduigou
parents: 7803
diff changeset
   152
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            lastReturned = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the number of elements in this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the number of elements in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   167
     * Returns {@code true} if this set contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   169
     * @return {@code true} if this set contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   176
     * Returns {@code true} if this set contains the specified element.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param e element to be checked for containment in this collection
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   179
     * @return {@code true} if this set contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public boolean contains(Object e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   184
        Class<?> eClass = e.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (eClass != elementType && eClass.getSuperclass() != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   188
        int eOrdinal = ((Enum<?>)e).ordinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Adds the specified element to this set if it is not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param e element to be added to this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   198
     * @return {@code true} if the set changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   200
     * @throws NullPointerException if {@code e} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        typeCheck(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int eOrdinal = e.ordinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int eWordNum = eOrdinal >>> 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        long oldElements = elements[eWordNum];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        elements[eWordNum] |= (1L << eOrdinal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        boolean result = (elements[eWordNum] != oldElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Removes the specified element from this set if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param e element to be removed from this set, if present
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   220
     * @return {@code true} if the set contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public boolean remove(Object e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   225
        Class<?> eClass = e.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (eClass != elementType && eClass.getSuperclass() != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   228
        int eOrdinal = ((Enum<?>)e).ordinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int eWordNum = eOrdinal >>> 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        long oldElements = elements[eWordNum];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        elements[eWordNum] &= ~(1L << eOrdinal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        boolean result = (elements[eWordNum] != oldElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   242
     * Returns {@code true} if this set contains all of the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * in the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param c collection to be checked for containment in this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   246
     * @return {@code true} if this set contains all of the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *        in the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (!(c instanceof JumboEnumSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return super.containsAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   254
        JumboEnumSet<?> es = (JumboEnumSet<?>)c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (es.elementType != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return es.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (int i = 0; i < elements.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if ((es.elements[i] & ~elements[i]) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Adds all of the elements in the specified collection to this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param c collection whose elements are to be added to this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   268
     * @return {@code true} if this set changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws NullPointerException if the specified collection or any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *     its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (!(c instanceof JumboEnumSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return super.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   276
        JumboEnumSet<?> es = (JumboEnumSet<?>)c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (es.elementType != elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (es.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new ClassCastException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    es.elementType + " != " + elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        for (int i = 0; i < elements.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            elements[i] |= es.elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return recalculateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Removes from this set all of its elements that are contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param c elements to be removed from this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   295
     * @return {@code true} if this set changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (!(c instanceof JumboEnumSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return super.removeAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   302
        JumboEnumSet<?> es = (JumboEnumSet<?>)c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (es.elementType != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        for (int i = 0; i < elements.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            elements[i] &= ~es.elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return recalculateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Retains only the elements in this set that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param c elements to be retained in this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   316
     * @return {@code true} if this set changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (!(c instanceof JumboEnumSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return super.retainAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        JumboEnumSet<?> es = (JumboEnumSet<?>)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (es.elementType != elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            boolean changed = (size != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        for (int i = 0; i < elements.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            elements[i] &= es.elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return recalculateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Removes all of the elements from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Arrays.fill(elements, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Compares the specified object with this set for equality.  Returns
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   345
     * {@code true} if the given object is also a set, the two sets have
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * the same size, and every member of the given set is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   349
     * @param o object to be compared for equality with this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   350
     * @return {@code true} if the specified object is equal to this set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (!(o instanceof JumboEnumSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   356
        JumboEnumSet<?> es = (JumboEnumSet<?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (es.elementType != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return size == 0 && es.size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return Arrays.equals(es.elements, elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Recalculates the size of the set.  Returns true if it's changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private boolean recalculateSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int oldSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        for (long elt : elements)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            size += Long.bitCount(elt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return size != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public EnumSet<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        JumboEnumSet<E> result = (JumboEnumSet<E>) super.clone();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   377
        result.elements = result.elements.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}