jdk/src/share/classes/java/util/EnumMap.java
author sherman
Wed, 21 Oct 2009 11:40:40 -0700
changeset 4161 679d00486dc6
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6878475: Better syntax for the named capture group in regex Summary: Updated the syntax of the newly added named capture group Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final Object NULL = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private Object maskNull(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return (value == null ? NULL : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private V unmaskNull(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return (V) (value == NULL ? null : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static Enum[] ZERO_LENGTH_ENUM_ARRAY = new Enum[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates an empty enum map with the specified key type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param keyType the class object of the key type for this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws NullPointerException if <tt>keyType</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public EnumMap(Class<K> keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        keyUniverse = getKeyUniverse(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        vals = new Object[keyUniverse.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Creates an enum map with the same key type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * map, initially containing the same mappings (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param m the enum map from which to initialize this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws NullPointerException if <tt>m</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public EnumMap(EnumMap<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        keyType = m.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        keyUniverse = m.keyUniverse;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   143
        vals = m.vals.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        size = m.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Creates an enum map initialized from the specified map.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * specified map is an <tt>EnumMap</tt> instance, this constructor behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * identically to {@link #EnumMap(EnumMap)}.  Otherwise, the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * must contain at least one mapping (in order to determine the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * enum map's key type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param m the map from which to initialize this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws IllegalArgumentException if <tt>m</tt> is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *     <tt>EnumMap</tt> instance and contains no mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws NullPointerException if <tt>m</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public EnumMap(Map<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (m instanceof EnumMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            EnumMap<K, ? extends V> em = (EnumMap<K, ? extends V>) m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            keyType = em.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            keyUniverse = em.keyUniverse;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   164
            vals = em.vals.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            size = em.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (m.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                throw new IllegalArgumentException("Specified map is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            keyType = m.keySet().iterator().next().getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            keyUniverse = getKeyUniverse(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            vals = new Object[keyUniverse.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
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 <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param value the value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @return <tt>true</tt> if this map maps one or more keys to this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        value = maskNull(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        for (Object val : vals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (value.equals(val))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Returns <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param key the key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *            key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return isValidKey(key) && vals[((Enum)key).ordinal()] != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private boolean containsMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return isValidKey(key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            maskNull(value).equals(vals[((Enum)key).ordinal()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * {@code k} to a value {@code v} such that {@code (key == k)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return (isValidKey(key) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                unmaskNull(vals[((Enum)key).ordinal()]) : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * If the map previously contained a mapping for this key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param key the key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param value the value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return the previous value associated with specified key, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *     <tt>null</tt> if there was no mapping for key.  (A <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *     return can also indicate that the map previously associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *     <tt>null</tt> with the specified key.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        typeCheck(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   260
        int index = key.ordinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        Object oldValue = vals[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        vals[index] = maskNull(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (oldValue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return unmaskNull(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Removes the mapping for this key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param key the key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the previous value associated with specified key, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *     <tt>null</tt> if there was no entry for key.  (A <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *     return can also indicate that the map previously associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *     <tt>null</tt> with the specified key.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (!isValidKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int index = ((Enum)key).ordinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        Object oldValue = vals[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        vals[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (oldValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return unmaskNull(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private boolean removeMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (!isValidKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int index = ((Enum)key).ordinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (maskNull(value).equals(vals[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            vals[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Returns true if key is of the proper type to be a key in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * enum map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private boolean isValidKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (key == null)
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
        // Cheaper than instanceof Enum followed by getDeclaringClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Class keyClass = key.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return keyClass == keyType || keyClass.getSuperclass() == keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param m the mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @throws NullPointerException the specified map is null, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *     one or more keys in the specified map are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (m instanceof EnumMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            EnumMap<? extends K, ? extends V> em =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                (EnumMap<? extends K, ? extends V>)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (em.keyType != keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (em.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                throw new ClassCastException(em.keyType + " != " + keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            for (int i = 0; i < keyUniverse.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                Object emValue = em.vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (emValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    if (vals[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    vals[i] = emValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            super.putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Removes all mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        Arrays.fill(vals, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * This field is initialized to contain an instance of the entry set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * view the first time this view is requested.  The view is stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * so there's no reason to create more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * The returned set obeys the general contract outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * {@link Map#keySet()}.  The set's iterator will return the keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * in their natural order (the order in which the enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * are declared).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return a set view of the keys contained in this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (ks != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return keySet = new KeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            int oldSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            EnumMap.this.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return size != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            EnumMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * The returned collection obeys the general contract outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * {@link Map#values()}.  The collection's iterator will return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * values in the order their corresponding keys appear in map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * which is their natural order (the order in which the enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * are declared).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return a collection view of the values contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (vs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return vs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return values = new Values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            o = maskNull(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            for (int i = 0; i < vals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (o.equals(vals[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    vals[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            EnumMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * The returned set obeys the general contract outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * {@link Map#keySet()}.  The set's iterator will return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * mappings in the order their keys appear in map, which is their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * natural order (the order in which the enum constants are declared).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @return a set view of the mappings contained in this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (es != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return es;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return entrySet = new EntrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return containsMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return removeMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            EnumMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return fillEntryArray(new Object[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                a = (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    .newInstance(a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return (T[]) fillEntryArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        private Object[] fillEntryArray(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            for (int i = 0; i < vals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                if (vals[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    a[j++] = new AbstractMap.SimpleEntry<K,V>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        keyUniverse[i], unmaskNull(vals[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    private abstract class EnumMapIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // Lower bound on index of next element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // Index of last returned element, or -1 if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            while (index < vals.length && vals[index] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return index != vals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            checkLastReturnedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (vals[lastReturnedIndex] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                vals[lastReturnedIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        private void checkLastReturnedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (lastReturnedIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    private class KeyIterator extends EnumMapIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (!hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            lastReturnedIndex = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return keyUniverse[lastReturnedIndex];
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 ValueIterator extends EnumMapIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public V 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 unmaskNull(vals[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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Since we don't use Entry objects, we use the Iterator itself as entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    private class EntryIterator extends EnumMapIterator<Map.Entry<K,V>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        implements Map.Entry<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            if (!hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            lastReturnedIndex = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            checkLastReturnedIndexForEntryUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return keyUniverse[lastReturnedIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            checkLastReturnedIndexForEntryUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return unmaskNull(vals[lastReturnedIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            checkLastReturnedIndexForEntryUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            V oldValue = unmaskNull(vals[lastReturnedIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            vals[lastReturnedIndex] = maskNull(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (lastReturnedIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                return o == this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            Map.Entry e = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            V ourValue = unmaskNull(vals[lastReturnedIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            Object hisValue = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            return e.getKey() == keyUniverse[lastReturnedIndex] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                (ourValue == hisValue ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 (ourValue != null && ourValue.equals(hisValue)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (lastReturnedIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            Object value = vals[lastReturnedIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return keyUniverse[lastReturnedIndex].hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                ^ (value == NULL ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (lastReturnedIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return keyUniverse[lastReturnedIndex] + "="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                + unmaskNull(vals[lastReturnedIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        private void checkLastReturnedIndexForEntryUse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (lastReturnedIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                throw new IllegalStateException("Entry was removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Compares the specified object with this map for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <tt>true</tt> if the given object is also a map and the two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * represent the same mappings, as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Map#equals(Object)} contract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param o the object to be compared for equality with this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (!(o instanceof EnumMap))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            return super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        EnumMap em = (EnumMap)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (em.keyType != keyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return size == 0 && em.size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // Key types match, compare each value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        for (int i = 0; i < keyUniverse.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            Object ourValue =    vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            Object hisValue = em.vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (hisValue != ourValue &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                (hisValue == null || !hisValue.equals(ourValue)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Returns a shallow copy of this enum map.  (The values themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @return a shallow copy of this enum map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public EnumMap<K, V> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        EnumMap<K, V> result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            result = (EnumMap<K, V>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        } catch(CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   665
        result.vals = result.vals.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Throws an exception if e is not of the correct type for this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private void typeCheck(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        Class keyClass = key.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (keyClass != keyType && keyClass.getSuperclass() != keyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            throw new ClassCastException(keyClass + " != " + keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Returns all of the values comprising K.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * The result is uncloned, cached, and shared by all callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return SharedSecrets.getJavaLangAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                        .getEnumConstantsShared(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    private static final long serialVersionUID = 458661240069192865L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Save the state of the <tt>EnumMap</tt> instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @serialData The <i>size</i> of the enum map (the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *             mappings) is emitted (int), followed by the key (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *             and value (Object) for each key-value mapping represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *             by the enum map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        throws java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Write out the key type and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // Write out keys and values (alternating)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        for (Map.Entry<K,V> e :  entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            s.writeObject(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            s.writeObject(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Reconstitute the <tt>EnumMap</tt> instance from a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        throws java.io.IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // Read in the key type and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        keyUniverse = getKeyUniverse(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        vals = new Object[keyUniverse.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // Read in size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // Read the keys and values, and put the mappings in the HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            K key = (K) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            V value = (V) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
}