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