rt/emul/compact/src/main/java/java/util/RegularEnumSet.java
changeset 1293 b7da86101183
parent 1292 9cf04876e4a5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/RegularEnumSet.java	Sun Sep 22 21:51:56 2013 +0200
     1.3 @@ -0,0 +1,303 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util;
    1.30 +
    1.31 +/**
    1.32 + * Private implementation class for EnumSet, for "regular sized" enum types
    1.33 + * (i.e., those with 64 or fewer enum constants).
    1.34 + *
    1.35 + * @author Josh Bloch
    1.36 + * @since 1.5
    1.37 + * @serial exclude
    1.38 + */
    1.39 +class RegularEnumSet<E extends Enum<E>> extends EnumSet<E> {
    1.40 +    private static final long serialVersionUID = 3411599620347842686L;
    1.41 +    /**
    1.42 +     * Bit vector representation of this set.  The 2^k bit indicates the
    1.43 +     * presence of universe[k] in this set.
    1.44 +     */
    1.45 +    private long elements = 0L;
    1.46 +
    1.47 +    RegularEnumSet(Class<E>elementType, Enum[] universe) {
    1.48 +        super(elementType, universe);
    1.49 +    }
    1.50 +
    1.51 +    void addRange(E from, E to) {
    1.52 +        elements = (-1L >>>  (from.ordinal() - to.ordinal() - 1)) << from.ordinal();
    1.53 +    }
    1.54 +
    1.55 +    void addAll() {
    1.56 +        if (universe.length != 0)
    1.57 +            elements = -1L >>> -universe.length;
    1.58 +    }
    1.59 +
    1.60 +    void complement() {
    1.61 +        if (universe.length != 0) {
    1.62 +            elements = ~elements;
    1.63 +            elements &= -1L >>> -universe.length;  // Mask unused bits
    1.64 +        }
    1.65 +    }
    1.66 +
    1.67 +    /**
    1.68 +     * Returns an iterator over the elements contained in this set.  The
    1.69 +     * iterator traverses the elements in their <i>natural order</i> (which is
    1.70 +     * the order in which the enum constants are declared). The returned
    1.71 +     * Iterator is a "snapshot" iterator that will never throw {@link
    1.72 +     * ConcurrentModificationException}; the elements are traversed as they
    1.73 +     * existed when this call was invoked.
    1.74 +     *
    1.75 +     * @return an iterator over the elements contained in this set
    1.76 +     */
    1.77 +    public Iterator<E> iterator() {
    1.78 +        return new EnumSetIterator<>();
    1.79 +    }
    1.80 +
    1.81 +    private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> {
    1.82 +        /**
    1.83 +         * A bit vector representing the elements in the set not yet
    1.84 +         * returned by this iterator.
    1.85 +         */
    1.86 +        long unseen;
    1.87 +
    1.88 +        /**
    1.89 +         * The bit representing the last element returned by this iterator
    1.90 +         * but not removed, or zero if no such element exists.
    1.91 +         */
    1.92 +        long lastReturned = 0;
    1.93 +
    1.94 +        EnumSetIterator() {
    1.95 +            unseen = elements;
    1.96 +        }
    1.97 +
    1.98 +        public boolean hasNext() {
    1.99 +            return unseen != 0;
   1.100 +        }
   1.101 +
   1.102 +        public E next() {
   1.103 +            if (unseen == 0)
   1.104 +                throw new NoSuchElementException();
   1.105 +            lastReturned = unseen & -unseen;
   1.106 +            unseen -= lastReturned;
   1.107 +            return (E) universe[Long.numberOfTrailingZeros(lastReturned)];
   1.108 +        }
   1.109 +
   1.110 +        public void remove() {
   1.111 +            if (lastReturned == 0)
   1.112 +                throw new IllegalStateException();
   1.113 +            elements &= ~lastReturned;
   1.114 +            lastReturned = 0;
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Returns the number of elements in this set.
   1.120 +     *
   1.121 +     * @return the number of elements in this set
   1.122 +     */
   1.123 +    public int size() {
   1.124 +        return Long.bitCount(elements);
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Returns <tt>true</tt> if this set contains no elements.
   1.129 +     *
   1.130 +     * @return <tt>true</tt> if this set contains no elements
   1.131 +     */
   1.132 +    public boolean isEmpty() {
   1.133 +        return elements == 0;
   1.134 +    }
   1.135 +
   1.136 +    /**
   1.137 +     * Returns <tt>true</tt> if this set contains the specified element.
   1.138 +     *
   1.139 +     * @param e element to be checked for containment in this collection
   1.140 +     * @return <tt>true</tt> if this set contains the specified element
   1.141 +     */
   1.142 +    public boolean contains(Object e) {
   1.143 +        if (e == null)
   1.144 +            return false;
   1.145 +        Class eClass = e.getClass();
   1.146 +        if (eClass != elementType && eClass.getSuperclass() != elementType)
   1.147 +            return false;
   1.148 +
   1.149 +        return (elements & (1L << ((Enum)e).ordinal())) != 0;
   1.150 +    }
   1.151 +
   1.152 +    // Modification Operations
   1.153 +
   1.154 +    /**
   1.155 +     * Adds the specified element to this set if it is not already present.
   1.156 +     *
   1.157 +     * @param e element to be added to this set
   1.158 +     * @return <tt>true</tt> if the set changed as a result of the call
   1.159 +     *
   1.160 +     * @throws NullPointerException if <tt>e</tt> is null
   1.161 +     */
   1.162 +    public boolean add(E e) {
   1.163 +        typeCheck(e);
   1.164 +
   1.165 +        long oldElements = elements;
   1.166 +        elements |= (1L << ((Enum)e).ordinal());
   1.167 +        return elements != oldElements;
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Removes the specified element from this set if it is present.
   1.172 +     *
   1.173 +     * @param e element to be removed from this set, if present
   1.174 +     * @return <tt>true</tt> if the set contained the specified element
   1.175 +     */
   1.176 +    public boolean remove(Object e) {
   1.177 +        if (e == null)
   1.178 +            return false;
   1.179 +        Class eClass = e.getClass();
   1.180 +        if (eClass != elementType && eClass.getSuperclass() != elementType)
   1.181 +            return false;
   1.182 +
   1.183 +        long oldElements = elements;
   1.184 +        elements &= ~(1L << ((Enum)e).ordinal());
   1.185 +        return elements != oldElements;
   1.186 +    }
   1.187 +
   1.188 +    // Bulk Operations
   1.189 +
   1.190 +    /**
   1.191 +     * Returns <tt>true</tt> if this set contains all of the elements
   1.192 +     * in the specified collection.
   1.193 +     *
   1.194 +     * @param c collection to be checked for containment in this set
   1.195 +     * @return <tt>true</tt> if this set contains all of the elements
   1.196 +     *        in the specified collection
   1.197 +     * @throws NullPointerException if the specified collection is null
   1.198 +     */
   1.199 +    public boolean containsAll(Collection<?> c) {
   1.200 +        if (!(c instanceof RegularEnumSet))
   1.201 +            return super.containsAll(c);
   1.202 +
   1.203 +        RegularEnumSet es = (RegularEnumSet)c;
   1.204 +        if (es.elementType != elementType)
   1.205 +            return es.isEmpty();
   1.206 +
   1.207 +        return (es.elements & ~elements) == 0;
   1.208 +    }
   1.209 +
   1.210 +    /**
   1.211 +     * Adds all of the elements in the specified collection to this set.
   1.212 +     *
   1.213 +     * @param c collection whose elements are to be added to this set
   1.214 +     * @return <tt>true</tt> if this set changed as a result of the call
   1.215 +     * @throws NullPointerException if the specified collection or any
   1.216 +     *     of its elements are null
   1.217 +     */
   1.218 +    public boolean addAll(Collection<? extends E> c) {
   1.219 +        if (!(c instanceof RegularEnumSet))
   1.220 +            return super.addAll(c);
   1.221 +
   1.222 +        RegularEnumSet es = (RegularEnumSet)c;
   1.223 +        if (es.elementType != elementType) {
   1.224 +            if (es.isEmpty())
   1.225 +                return false;
   1.226 +            else
   1.227 +                throw new ClassCastException(
   1.228 +                    es.elementType + " != " + elementType);
   1.229 +        }
   1.230 +
   1.231 +        long oldElements = elements;
   1.232 +        elements |= es.elements;
   1.233 +        return elements != oldElements;
   1.234 +    }
   1.235 +
   1.236 +    /**
   1.237 +     * Removes from this set all of its elements that are contained in
   1.238 +     * the specified collection.
   1.239 +     *
   1.240 +     * @param c elements to be removed from this set
   1.241 +     * @return <tt>true</tt> if this set changed as a result of the call
   1.242 +     * @throws NullPointerException if the specified collection is null
   1.243 +     */
   1.244 +    public boolean removeAll(Collection<?> c) {
   1.245 +        if (!(c instanceof RegularEnumSet))
   1.246 +            return super.removeAll(c);
   1.247 +
   1.248 +        RegularEnumSet es = (RegularEnumSet)c;
   1.249 +        if (es.elementType != elementType)
   1.250 +            return false;
   1.251 +
   1.252 +        long oldElements = elements;
   1.253 +        elements &= ~es.elements;
   1.254 +        return elements != oldElements;
   1.255 +    }
   1.256 +
   1.257 +    /**
   1.258 +     * Retains only the elements in this set that are contained in the
   1.259 +     * specified collection.
   1.260 +     *
   1.261 +     * @param c elements to be retained in this set
   1.262 +     * @return <tt>true</tt> if this set changed as a result of the call
   1.263 +     * @throws NullPointerException if the specified collection is null
   1.264 +     */
   1.265 +    public boolean retainAll(Collection<?> c) {
   1.266 +        if (!(c instanceof RegularEnumSet))
   1.267 +            return super.retainAll(c);
   1.268 +
   1.269 +        RegularEnumSet<?> es = (RegularEnumSet<?>)c;
   1.270 +        if (es.elementType != elementType) {
   1.271 +            boolean changed = (elements != 0);
   1.272 +            elements = 0;
   1.273 +            return changed;
   1.274 +        }
   1.275 +
   1.276 +        long oldElements = elements;
   1.277 +        elements &= es.elements;
   1.278 +        return elements != oldElements;
   1.279 +    }
   1.280 +
   1.281 +    /**
   1.282 +     * Removes all of the elements from this set.
   1.283 +     */
   1.284 +    public void clear() {
   1.285 +        elements = 0;
   1.286 +    }
   1.287 +
   1.288 +    /**
   1.289 +     * Compares the specified object with this set for equality.  Returns
   1.290 +     * <tt>true</tt> if the given object is also a set, the two sets have
   1.291 +     * the same size, and every member of the given set is contained in
   1.292 +     * this set.
   1.293 +     *
   1.294 +     * @param e object to be compared for equality with this set
   1.295 +     * @return <tt>true</tt> if the specified object is equal to this set
   1.296 +     */
   1.297 +    public boolean equals(Object o) {
   1.298 +        if (!(o instanceof RegularEnumSet))
   1.299 +            return super.equals(o);
   1.300 +
   1.301 +        RegularEnumSet es = (RegularEnumSet)o;
   1.302 +        if (es.elementType != elementType)
   1.303 +            return elements == 0 && es.elements == 0;
   1.304 +        return es.elements == elements;
   1.305 +    }
   1.306 +}