jdk/src/share/classes/java/util/EnumMap.java
author mduigou
Fri, 11 Apr 2014 14:07:25 -0700
changeset 23746 ce60f7b62312
parent 14342 8435a30053c1
permissions -rw-r--r--
8035284: Remove redundant null initialization Reviewed-by: lancea, martin, chegar, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12879
diff changeset
     2
 * Copyright (c) 2003, 2012, 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
import java.util.Map.Entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.misc.SharedSecrets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A specialized {@link Map} implementation for use with enum type keys.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of the keys in an enum map must come from a single enum type that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * specified, explicitly or implicitly, when the map is created.  Enum maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * are represented internally as arrays.  This representation is extremely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * compact and efficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>Enum maps are maintained in the <i>natural order</i> of their keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (the order in which the enum constants are declared).  This is reflected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * in the iterators returned by the collections views ({@link #keySet()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@link #entrySet()}, and {@link #values()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Iterators returned by the collection views are <i>weakly consistent</i>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * they will never throw {@link ConcurrentModificationException} and they may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * or may not show the effects of any modifications to the map that occur while
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the iteration is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Null keys are not permitted.  Attempts to insert a null key will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * throw {@link NullPointerException}.  Attempts to test for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * presence of a null key or to remove one will, however, function properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Null values are permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <P>Like most collection implementations <tt>EnumMap</tt> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * synchronized. If multiple threads access an enum map concurrently, and at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * least one of the threads modifies the map, it should be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * externally.  This is typically accomplished by synchronizing on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * object that naturally encapsulates the enum map.  If no such object exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the map should be "wrapped" using the {@link Collections#synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * unsynchronized access:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     Map&lt;EnumKey, V&gt; m
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         = Collections.synchronizedMap(new EnumMap&lt;EnumKey, V&gt;(...));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>Implementation note: All basic operations execute in constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * They are likely (though not guaranteed) to be faster than their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * {@link HashMap} counterparts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see EnumSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    implements java.io.Serializable, Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The <tt>Class</tt> object for the enum type of all the keys of this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private final Class<K> keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * All of the values comprising K.  (Cached for performance.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private transient K[] keyUniverse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Array representation of this map.  The ith element is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * to which universe[i] is currently mapped, or null if it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * mapped to anything, or NULL if it's mapped to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private transient Object[] vals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The number of mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private transient int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Distinguished non-null value for representing null values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
11690
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   109
    private static final Object NULL = new Object() {
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   110
        public int hashCode() {
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   111
            return 0;
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   112
        }
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   113
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   114
        public String toString() {
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   115
            return "java.util.EnumMap.NULL";
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   116
        }
74cf5384d069 7123229: (coll) EnumMap.containsValue(null) returns true
ngmr
parents: 9275
diff changeset
   117
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private Object maskNull(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return (value == null ? NULL : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   123
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private V unmaskNull(Object value) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   125
        return (V)(value == NULL ? null : value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   128
    private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Creates an empty enum map with the specified key type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param keyType the class object of the key type for this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws NullPointerException if <tt>keyType</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public EnumMap(Class<K> keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        keyUniverse = getKeyUniverse(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        vals = new Object[keyUniverse.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates an enum map with the same key type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * map, initially containing the same mappings (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param m the enum map from which to initialize this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @throws NullPointerException if <tt>m</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public EnumMap(EnumMap<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        keyType = m.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        keyUniverse = m.keyUniverse;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   152
        vals = m.vals.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        size = m.size;
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
     * Creates an enum map initialized from the specified map.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * specified map is an <tt>EnumMap</tt> instance, this constructor behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * identically to {@link #EnumMap(EnumMap)}.  Otherwise, the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * must contain at least one mapping (in order to determine the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * enum map's key type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param m the map from which to initialize this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws IllegalArgumentException if <tt>m</tt> is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *     <tt>EnumMap</tt> instance and contains no mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @throws NullPointerException if <tt>m</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public EnumMap(Map<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (m instanceof EnumMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            EnumMap<K, ? extends V> em = (EnumMap<K, ? extends V>) m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            keyType = em.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            keyUniverse = em.keyUniverse;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   173
            vals = em.vals.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            size = em.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (m.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new IllegalArgumentException("Specified map is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            keyType = m.keySet().iterator().next().getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            keyUniverse = getKeyUniverse(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            vals = new Object[keyUniverse.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param value the value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return <tt>true</tt> if this map maps one or more keys to this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        value = maskNull(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        for (Object val : vals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (value.equals(val))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param key the key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *            key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean containsKey(Object key) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   222
        return isValidKey(key) && vals[((Enum<?>)key).ordinal()] != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private boolean containsMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return isValidKey(key) &&
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   227
            maskNull(value).equals(vals[((Enum<?>)key).ordinal()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * {@code k} to a value {@code v} such that {@code (key == k)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return (isValidKey(key) ?
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   247
                unmaskNull(vals[((Enum<?>)key).ordinal()]) : null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * If the map previously contained a mapping for this key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param key the key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param value the value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return the previous value associated with specified key, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *     <tt>null</tt> if there was no mapping for key.  (A <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *     return can also indicate that the map previously associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *     <tt>null</tt> with the specified key.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        typeCheck(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   269
        int index = key.ordinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Object oldValue = vals[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        vals[index] = maskNull(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (oldValue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return unmaskNull(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Removes the mapping for this key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param key the key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return the previous value associated with specified key, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *     <tt>null</tt> if there was no entry for key.  (A <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *     return can also indicate that the map previously associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *     <tt>null</tt> with the specified key.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (!isValidKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return null;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   289
        int index = ((Enum<?>)key).ordinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Object oldValue = vals[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        vals[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (oldValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return unmaskNull(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private boolean removeMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!isValidKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   300
        int index = ((Enum<?>)key).ordinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (maskNull(value).equals(vals[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            vals[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns true if key is of the proper type to be a key in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * enum map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private boolean isValidKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // Cheaper than instanceof Enum followed by getDeclaringClass
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   318
        Class<?> keyClass = key.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return keyClass == keyType || keyClass.getSuperclass() == keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param m the mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws NullPointerException the specified map is null, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *     one or more keys in the specified map are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (m instanceof EnumMap) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   335
            EnumMap<?, ?> em = (EnumMap<?, ?>)m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (em.keyType != keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (em.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                throw new ClassCastException(em.keyType + " != " + keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            for (int i = 0; i < keyUniverse.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                Object emValue = em.vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (emValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    if (vals[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    vals[i] = emValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            super.putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Removes all mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        Arrays.fill(vals, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * This field is initialized to contain an instance of the entry set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * view the first time this view is requested.  The view is stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * so there's no reason to create more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 14342
diff changeset
   370
    private transient Set<Map.Entry<K,V>> entrySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * The returned set obeys the general contract outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * {@link Map#keySet()}.  The set's iterator will return the keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * in their natural order (the order in which the enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * are declared).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return a set view of the keys contained in this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (ks != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return keySet = new KeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            int oldSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            EnumMap.this.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return size != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            EnumMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * The returned collection obeys the general contract outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * {@link Map#values()}.  The collection's iterator will return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * values in the order their corresponding keys appear in map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * which is their natural order (the order in which the enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * are declared).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return a collection view of the values contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (vs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return vs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return values = new Values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            o = maskNull(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            for (int i = 0; i < vals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (o.equals(vals[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    vals[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            EnumMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * The returned set obeys the general contract outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * {@link Map#keySet()}.  The set's iterator will return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * mappings in the order their keys appear in map, which is their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * natural order (the order in which the enum constants are declared).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return a set view of the mappings contained in this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (es != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return es;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return entrySet = new EntrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   475
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   479
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return containsMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   485
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return removeMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            EnumMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return fillEntryArray(new Object[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                a = (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    .newInstance(a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            return (T[]) fillEntryArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        private Object[] fillEntryArray(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            for (int i = 0; i < vals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                if (vals[i] != null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   511
                    a[j++] = new AbstractMap.SimpleEntry<>(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        keyUniverse[i], unmaskNull(vals[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private abstract class EnumMapIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // Lower bound on index of next element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        // Index of last returned element, or -1 if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            while (index < vals.length && vals[index] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return index != vals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            checkLastReturnedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (vals[lastReturnedIndex] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                vals[lastReturnedIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        private void checkLastReturnedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if (lastReturnedIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    private class KeyIterator extends EnumMapIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (!hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            lastReturnedIndex = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return keyUniverse[lastReturnedIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private class ValueIterator extends EnumMapIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (!hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            lastReturnedIndex = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return unmaskNull(vals[lastReturnedIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   564
    private class EntryIterator extends EnumMapIterator<Map.Entry<K,V>> {
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 14342
diff changeset
   565
        private Entry lastReturnedEntry;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   566
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (!hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                throw new NoSuchElementException();
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   570
            lastReturnedEntry = new Entry(index++);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   571
            return lastReturnedEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   574
        public void remove() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   575
            lastReturnedIndex =
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   576
                ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   577
            super.remove();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   578
            lastReturnedEntry.index = lastReturnedIndex;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   579
            lastReturnedEntry = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   582
        private class Entry implements Map.Entry<K,V> {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   583
            private int index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   584
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   585
            private Entry(int index) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   586
                this.index = index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   587
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   588
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   589
            public K getKey() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   590
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   591
                return keyUniverse[index];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   592
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   594
            public V getValue() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   595
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   596
                return unmaskNull(vals[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   597
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   598
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   599
            public V setValue(V value) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   600
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   601
                V oldValue = unmaskNull(vals[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   602
                vals[index] = maskNull(value);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   603
                return oldValue;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   604
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   605
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   606
            public boolean equals(Object o) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   607
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   608
                    return o == this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   610
                if (!(o instanceof Map.Entry))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   611
                    return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   613
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   614
                V ourValue = unmaskNull(vals[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   615
                Object hisValue = e.getValue();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   616
                return (e.getKey() == keyUniverse[index] &&
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   617
                        (ourValue == hisValue ||
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   618
                         (ourValue != null && ourValue.equals(hisValue))));
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   619
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   620
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   621
            public int hashCode() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   622
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   623
                    return super.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   625
                return entryHashCode(index);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   626
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   627
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   628
            public String toString() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   629
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   630
                    return super.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   632
                return keyUniverse[index] + "="
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   633
                    + unmaskNull(vals[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   634
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   636
            private void checkIndexForEntryUse() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   637
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   638
                    throw new IllegalStateException("Entry was removed");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   639
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Compares the specified object with this map for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <tt>true</tt> if the given object is also a map and the two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * represent the same mappings, as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Map#equals(Object)} contract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param o the object to be compared for equality with this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public boolean equals(Object o) {
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   655
        if (this == o)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   656
            return true;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   657
        if (o instanceof EnumMap)
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   658
            return equals((EnumMap<?,?>)o);
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   659
        if (!(o instanceof Map))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   660
            return false;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   661
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   662
        Map<?,?> m = (Map<?,?>)o;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   663
        if (size != m.size())
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   664
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   666
        for (int i = 0; i < keyUniverse.length; i++) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   667
            if (null != vals[i]) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   668
                K key = keyUniverse[i];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   669
                V value = unmaskNull(vals[i]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   670
                if (null == value) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   671
                    if (!((null == m.get(key)) && m.containsKey(key)))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   672
                       return false;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   673
                } else {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   674
                   if (!value.equals(m.get(key)))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   675
                      return false;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   676
                }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   677
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   678
        }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   679
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   680
        return true;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   681
    }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   682
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   683
    private boolean equals(EnumMap<?,?> em) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (em.keyType != keyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return size == 0 && em.size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // Key types match, compare each value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        for (int i = 0; i < keyUniverse.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            Object ourValue =    vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            Object hisValue = em.vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (hisValue != ourValue &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                (hisValue == null || !hisValue.equals(ourValue)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   699
     * Returns the hash code value for this map.  The hash code of a map is
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   700
     * defined to be the sum of the hash codes of each entry in the map.
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   701
     */
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   702
    public int hashCode() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   703
        int h = 0;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   704
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   705
        for (int i = 0; i < keyUniverse.length; i++) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   706
            if (null != vals[i]) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   707
                h += entryHashCode(i);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   708
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   709
        }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   710
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   711
        return h;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   712
    }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   713
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   714
    private int entryHashCode(int index) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   715
        return (keyUniverse[index].hashCode() ^ vals[index].hashCode());
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   716
    }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   717
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   718
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Returns a shallow copy of this enum map.  (The values themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return a shallow copy of this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   724
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public EnumMap<K, V> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        EnumMap<K, V> result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            result = (EnumMap<K, V>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        } catch(CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   732
        result.vals = result.vals.clone();
12879
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents: 12448
diff changeset
   733
        result.entrySet = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Throws an exception if e is not of the correct type for this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private void typeCheck(K key) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   741
        Class<?> keyClass = key.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (keyClass != keyType && keyClass.getSuperclass() != keyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            throw new ClassCastException(keyClass + " != " + keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Returns all of the values comprising K.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * The result is uncloned, cached, and shared by all callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return SharedSecrets.getJavaLangAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                                        .getEnumConstantsShared(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private static final long serialVersionUID = 458661240069192865L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Save the state of the <tt>EnumMap</tt> instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @serialData The <i>size</i> of the enum map (the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *             mappings) is emitted (int), followed by the key (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *             and value (Object) for each key-value mapping represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *             by the enum map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        throws java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        // Write out the key type and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // Write out keys and values (alternating)
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   776
        int entriesToBeWritten = size;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   777
        for (int i = 0; entriesToBeWritten > 0; i++) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   778
            if (null != vals[i]) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   779
                s.writeObject(keyUniverse[i]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   780
                s.writeObject(unmaskNull(vals[i]));
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   781
                entriesToBeWritten--;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   782
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Reconstitute the <tt>EnumMap</tt> instance from a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11690
diff changeset
   790
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        throws java.io.IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        // Read in the key type and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        keyUniverse = getKeyUniverse(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        vals = new Object[keyUniverse.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // Read in size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        // Read the keys and values, and put the mappings in the HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            K key = (K) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            V value = (V) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
}