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