jdk/src/share/classes/java/util/IdentityHashMap.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 9275 1df1f7dfab7f
child 12448 b95438b17098
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This class implements the <tt>Map</tt> interface with a hash table, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * reference-equality in place of object-equality when comparing keys (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * values).  In other words, in an <tt>IdentityHashMap</tt>, two keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <tt>(k1==k2)</tt>.  (In normal <tt>Map</tt> implementations (like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p><b>This class is <i>not</i> a general-purpose <tt>Map</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * implementation!  While this class implements the <tt>Map</tt> interface, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * intentionally violates <tt>Map's</tt> general contract, which mandates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * use of the <tt>equals</tt> method when comparing objects.  This class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * designed for use only in the rare cases wherein reference-equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * semantics are required.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>A typical use of this class is <i>topology-preserving object graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * transformations</i>, such as serialization or deep-copying.  To perform such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * a transformation, a program must maintain a "node table" that keeps track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of all the object references that have already been processed.  The node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * table must not equate distinct objects even if they happen to be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Another typical use of this class is to maintain <i>proxy objects</i>.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * example, a debugging facility might wish to maintain a proxy object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * each object in the program being debugged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>This class provides all of the optional map operations, and permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <tt>null</tt> values and the <tt>null</tt> key.  This class makes no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * guarantees as to the order of the map; in particular, it does not guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * that the order will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>This class provides constant-time performance for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * identity hash function ({@link System#identityHashCode(Object)})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * disperses elements properly among the buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>This class has one tuning parameter (which affects performance but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * semantics): <i>expected maximum size</i>.  This parameter is the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * number of key-value mappings that the map is expected to hold.  Internally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * this parameter is used to determine the number of buckets initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * comprising the hash table.  The precise relationship between the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * maximum size and the number of buckets is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>If the size of the map (the number of key-value mappings) sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * exceeds the expected maximum size, the number of buckets is increased
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Increasing the number of buckets ("rehashing") may be fairly expensive, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * it pays to create identity hash maps with a sufficiently large expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * maximum size.  On the other hand, iteration over collection views requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * time proportional to the number of buckets in the hash table, so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * pays not to set the expected maximum size too high if you are especially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * concerned with iteration performance or memory usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * If multiple threads access an identity hash map concurrently, and at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * least one of the threads modifies the map structurally, it <i>must</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * be synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   Map m = Collections.synchronizedMap(new IdentityHashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p>The iterators returned by the <tt>iterator</tt> method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * collections returned by all of this class's "collection view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * methods" are <i>fail-fast</i>: if the map is structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * at any time after the iterator is created, in any way except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * through the iterator's own <tt>remove</tt> method, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * will throw a {@link ConcurrentModificationException}.  Thus, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * face of concurrent modification, the iterator fails quickly and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * cleanly, rather than risking arbitrary, non-deterministic behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * exception for its correctness: <i>fail-fast iterators should be used only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p>Implementation note: This is a simple <i>linear-probe</i> hash table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * as described for example in texts by Sedgewick and Knuth.  The array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * alternates holding keys and values.  (This has better locality for large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * tables than does using separate arrays.)  For many JRE implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * and operation mixes, this class will yield better performance than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see     System#identityHashCode(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @author  Doug Lea and Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
public class IdentityHashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    implements Map<K,V>, java.io.Serializable, Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The initial capacity used by the no-args constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * MUST be a power of two.  The value 32 corresponds to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * (specified) expected maximum size of 21, given a load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * of 2/3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static final int DEFAULT_CAPACITY = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * The minimum capacity, used if a lower value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * by either of the constructors with arguments.  The value 4 corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * to an expected maximum size of 2, given a load factor of 2/3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final int MINIMUM_CAPACITY = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * MUST be a power of two <= 1<<29.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final int MAXIMUM_CAPACITY = 1 << 29;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The table, resized as necessary. Length MUST always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private transient Object[] table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The number of key-value mappings contained in this identity hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The number of modifications, to support fast-fail iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
65
51fc1d79463f 6625725: (coll) modCount should not be volatile
martin
parents: 58
diff changeset
   176
    private transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * The next size value at which to resize (capacity * load factor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private transient int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Value representing null keys inside tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static final Object NULL_KEY = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Use NULL_KEY for key if it is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static Object maskNull(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return (key == null ? NULL_KEY : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns internal representation of null key back to caller as null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static Object unmaskNull(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return (key == NULL_KEY ? null : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Constructs a new, empty identity hash map with a default expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * maximum size (21).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public IdentityHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        init(DEFAULT_CAPACITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Constructs a new, empty map with the specified expected maximum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Putting more than the expected number of key-value mappings into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * the map may cause the internal data structure to grow, which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * somewhat time-consuming.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param expectedMaxSize the expected maximum size of the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @throws IllegalArgumentException if <tt>expectedMaxSize</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public IdentityHashMap(int expectedMaxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (expectedMaxSize < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throw new IllegalArgumentException("expectedMaxSize is negative: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                               + expectedMaxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        init(capacity(expectedMaxSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Returns the appropriate capacity for the specified expected maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * size.  Returns the smallest power of two between MINIMUM_CAPACITY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * and MAXIMUM_CAPACITY, inclusive, that is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * (3 * expectedMaxSize)/2, if such a number exists.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * returns MAXIMUM_CAPACITY.  If (3 * expectedMaxSize)/2 is negative, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * is assumed that overflow has occurred, and MAXIMUM_CAPACITY is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private int capacity(int expectedMaxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Compute min capacity for expectedMaxSize given a load factor of 2/3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int minCapacity = (3 * expectedMaxSize)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Compute the appropriate capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (minCapacity > MAXIMUM_CAPACITY || minCapacity < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            result = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            result = MINIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            while (result < minCapacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                result <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Initializes object to be an empty map with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * capacity, which is assumed to be a power of two between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private void init(int initCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // assert (initCapacity & -initCapacity) == initCapacity; // power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // assert initCapacity >= MINIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // assert initCapacity <= MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        threshold = (initCapacity * 2)/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        table = new Object[2 * initCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Constructs a new identity hash map containing the keys-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param m the map whose mappings are to be placed into this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public IdentityHashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // Allow for a bit of growth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this((int) ((1 + m.size()) * 1.1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Returns the number of key-value mappings in this identity hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns <tt>true</tt> if this identity hash map contains no key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return <tt>true</tt> if this identity hash map contains no key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *         mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Returns index for Object x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static int hash(Object x, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int h = System.identityHashCode(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // Multiply by -127, and left-shift to use least bit as part of hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return ((h << 1) - (h << 8)) & (length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Circularly traverses table of size len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private static int nextKeyIndex(int i, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return (i + 2 < len ? i + 2 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * {@code k} to a value {@code v} such that {@code (key == k)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                return (V) tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Tests whether the specified object reference is a key in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return  <code>true</code> if the specified object reference is a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see     #containsValue(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Tests whether the specified object reference is a value in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *         specified object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see     #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        Object[] tab = table;
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 65
diff changeset
   380
        for (int i = 1; i < tab.length; i += 2)
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 65
diff changeset
   381
            if (tab[i] == value && tab[i - 1] != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Tests if the specified key-value mapping is in the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param   value possible value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return  <code>true</code> if and only if the specified key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *          mapping is in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private boolean containsMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return tab[i + 1] == value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Associates the specified value with the specified key in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * hash map.  If the map previously contained a mapping for the key, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * old value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param key the key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param value the value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see     #get(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see     #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        while ( (item = tab[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                V oldValue = (V) tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                tab[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        tab[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        tab[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (++size >= threshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            resize(len); // len == 2 * current capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Resize the table to hold given capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param newCapacity the new capacity, must be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private void resize(int newCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int newLength = newCapacity * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        Object[] oldTable = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int oldLength = oldTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (oldLength == 2*MAXIMUM_CAPACITY) { // can't expand any further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (threshold == MAXIMUM_CAPACITY-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                throw new IllegalStateException("Capacity exhausted.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            threshold = MAXIMUM_CAPACITY-1;  // Gigantic map!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (oldLength >= newLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        Object[] newTable = new Object[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        threshold = newLength / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        for (int j = 0; j < oldLength; j += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            Object key = oldTable[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                Object value = oldTable[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                oldTable[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                oldTable[j+1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                int i = hash(key, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                while (newTable[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    i = nextKeyIndex(i, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                newTable[i] = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                newTable[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        table = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        int n = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (n > threshold) // conservatively pre-expand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            resize(capacity(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        for (Entry<? extends K, ? extends V> e : m.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            put(e.getKey(), e.getValue());
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
     * Removes the mapping for this key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                V oldValue = (V) tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                closeDeletion(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Removes the specified key-value mapping from the map if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param   value possible value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @return  <code>true</code> if and only if the specified key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *          mapping was in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    private boolean removeMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                if (tab[i + 1] != value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                closeDeletion(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Rehash all possibly-colliding entries following a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * deletion. This preserves the linear-probe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * collision properties required by get, put, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param d the index of a newly empty deleted slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    private void closeDeletion(int d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // Adapted from Knuth Section 6.4 Algorithm R
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // Look for items to swap into newly vacated slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // starting at index immediately following deletion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // and continuing until a null slot is seen, indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        // the end of a run of possibly-colliding keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
             i = nextKeyIndex(i, len) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            // The following test triggers if the item at slot i (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // hashes to be at slot r) should take the spot vacated by d.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            // If so, we swap it in, and then continue with d now at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // newly vacated i.  This process will terminate when we hit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // the null slot at the end of this run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            // The test is messy because we are using a circular table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int r = hash(item, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if ((i < r && (r <= d || d <= i)) || (r <= d && d <= i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                tab[d] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                tab[d + 1] = tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                d = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        for (int i = 0; i < tab.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Compares the specified object with this map for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <tt>true</tt> if the given object is also a map and the two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * represent identical object-reference mappings.  More formally, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * map is equal to another map <tt>m</tt> if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <tt>this.entrySet().equals(m.entrySet())</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <p><b>Owing to the reference-equality-based semantics of this map it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * possible that the symmetry and transitivity requirements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <tt>Object.equals</tt> contract may be violated if this map is compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * to a normal map.  However, the <tt>Object.equals</tt> contract is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * guaranteed to hold among <tt>IdentityHashMap</tt> instances.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param  o object to be compared for equality with this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        } else if (o instanceof IdentityHashMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            IdentityHashMap m = (IdentityHashMap) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (m.size() != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            Object[] tab = m.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            for (int i = 0; i < tab.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                Object k = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if (k != null && !containsMapping(k, tab[i + 1]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        } else if (o instanceof Map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            Map m = (Map)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return entrySet().equals(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            return false;  // o is not a Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * defined to be the sum of the hash codes of each entry in the map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <tt>IdentityHashMap</tt> instances <tt>m1</tt> and <tt>m2</tt>, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * required by the general contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * <p><b>Owing to the reference-equality-based semantics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <tt>Map.Entry</tt> instances in the set returned by this map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <tt>entrySet</tt> method, it is possible that the contractual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * requirement of <tt>Object.hashCode</tt> mentioned in the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * paragraph will be violated if one of the two objects being compared is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * an <tt>IdentityHashMap</tt> instance and the other is a normal map.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        for (int i = 0; i < tab.length; i +=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                Object k = unmaskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                result += System.identityHashCode(k) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                          System.identityHashCode(tab[i + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Returns a shallow copy of this identity hash map: the keys and values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            IdentityHashMap<K,V> m = (IdentityHashMap<K,V>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            m.entrySet = null;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   703
            m.table = table.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9275
diff changeset
   706
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        int index = (size != 0 ? 0 : table.length); // current slot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        int expectedModCount = modCount; // to support fast-fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        int lastReturnedIndex = -1;      // to allow remove()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        boolean indexValid; // To avoid unnecessary next computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        Object[] traversalTable = table; // reference to main table or copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            Object[] tab = traversalTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            for (int i = index; i < tab.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    return indexValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            index = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        protected int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (!indexValid && !hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            indexValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            lastReturnedIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            return lastReturnedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if (lastReturnedIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            expectedModCount = ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            int deletedSlot = lastReturnedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            // back up index to revisit new contents after deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            index = deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            indexValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            // Removal code proceeds as in closeDeletion except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // it must catch the rare case where an element already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            // seen is swapped into a vacant slot that will be later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            // traversed by this iterator. We cannot allow future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            // next() calls to return it again.  The likelihood of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            // this occurring under 2/3 load factor is very slim, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // when it does happen, we must make a copy of the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // the table to use for the rest of the traversal. Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // this can only happen when we are near the end of the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            // even in these rare cases, this is not very expensive in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            // time or space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            Object[] tab = traversalTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            int d = deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            K key = (K) tab[d];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            tab[d] = null;        // vacate the slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            tab[d + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            // If traversing a copy, remove in real table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            // We can skip gap-closure on copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (tab != IdentityHashMap.this.table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                IdentityHashMap.this.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
58
55e9cd9ade0b 6612102: (coll) IdentityHashMap.iterator().remove() might decrement size twice
martin
parents: 51
diff changeset
   783
            size--;
55e9cd9ade0b 6612102: (coll) IdentityHashMap.iterator().remove() might decrement size twice
martin
parents: 51
diff changeset
   784
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                 i = nextKeyIndex(i, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                int r = hash(item, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                // See closeDeletion for explanation of this conditional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                if ((i < r && (r <= d || d <= i)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    (r <= d && d <= i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    // If we are about to swap an already-seen element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    // into a slot that may later be returned by next(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    // then clone the rest of table for use in future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    // next() calls. It is OK that our copy will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    // a gap in the "wrong" place, since it will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    // be used for searching anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    if (i < deletedSlot && d >= deletedSlot &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                        traversalTable == IdentityHashMap.this.table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                        int remaining = len - deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                        Object[] newTable = new Object[remaining];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        System.arraycopy(tab, deletedSlot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                         newTable, 0, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                        traversalTable = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                        index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    tab[d] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    tab[d + 1] = tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    d = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    private class KeyIterator extends IdentityHashMapIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            return (K) unmaskNull(traversalTable[nextIndex()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    private class ValueIterator extends IdentityHashMapIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return (V) traversalTable[nextIndex() + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    private class EntryIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        extends IdentityHashMapIterator<Map.Entry<K,V>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    {
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   835
        private Entry lastReturnedEntry = null;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   836
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        public Map.Entry<K,V> next() {
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   838
            lastReturnedEntry = new Entry(nextIndex());
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   839
            return lastReturnedEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   842
        public void remove() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   843
            lastReturnedIndex =
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   844
                ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   845
            super.remove();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   846
            lastReturnedEntry.index = lastReturnedIndex;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   847
            lastReturnedEntry = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   850
        private class Entry implements Map.Entry<K,V> {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   851
            private int index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   852
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   853
            private Entry(int index) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   854
                this.index = index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   855
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   856
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   857
            public K getKey() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   858
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   859
                return (K) unmaskNull(traversalTable[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   860
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   862
            public V getValue() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   863
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   864
                return (V) traversalTable[index+1];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   865
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   866
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   867
            public V setValue(V value) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   868
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   869
                V oldValue = (V) traversalTable[index+1];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   870
                traversalTable[index+1] = value;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   871
                // if shadowing, force into main table
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   872
                if (traversalTable != IdentityHashMap.this.table)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   873
                    put((K) traversalTable[index], value);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   874
                return oldValue;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   875
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   877
            public boolean equals(Object o) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   878
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   879
                    return super.equals(o);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   880
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   881
                if (!(o instanceof Map.Entry))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   882
                    return false;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   883
                Map.Entry e = (Map.Entry)o;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   884
                return (e.getKey() == unmaskNull(traversalTable[index]) &&
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   885
                       e.getValue() == traversalTable[index+1]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   886
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   887
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   888
            public int hashCode() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   889
                if (lastReturnedIndex < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   890
                    return super.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   892
                return (System.identityHashCode(unmaskNull(traversalTable[index])) ^
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   893
                       System.identityHashCode(traversalTable[index+1]));
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   894
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   895
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   896
            public String toString() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   897
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   898
                    return super.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   900
                return (unmaskNull(traversalTable[index]) + "="
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   901
                        + traversalTable[index+1]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   902
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   904
            private void checkIndexForEntryUse() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   905
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   906
                    throw new IllegalStateException("Entry was removed");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   907
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * This field is initialized to contain an instance of the entry set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * view the first time this view is requested.  The view is stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * so there's no reason to create more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Returns an identity-based set view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * The set is backed by the map, so changes to the map are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * the set, and vice-versa.  If the map is modified while an iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * over the set is in progress, the results of the iteration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * undefined.  The set supports element removal, which removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * corresponding mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * <tt>clear</tt> methods.  It does not support the <tt>add</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <tt>addAll</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * <p><b>While the object returned by this method implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <tt>Set</tt> interface, it does <i>not</i> obey <tt>Set's</tt> general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * contract.  Like its backing map, the set returned by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * defines element equality as reference-equality rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * object-equality.  This affects the behavior of its <tt>contains</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <tt>remove</tt>, <tt>containsAll</tt>, <tt>equals</tt>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * <tt>hashCode</tt> methods.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <p><b>The <tt>equals</tt> method of the returned set returns <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * only if the specified object is a set containing exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * object references as the returned set.  The symmetry and transitivity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * requirements of the <tt>Object.equals</tt> contract may be violated if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * the set returned by this method is compared to a normal set.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * the <tt>Object.equals</tt> contract is guaranteed to hold among sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * returned by this method.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * <p>The <tt>hashCode</tt> method of the returned set returns the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * the <i>identity hashcodes</i> of the elements in the set, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * the sum of their hashcodes.  This is mandated by the change in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * semantics of the <tt>equals</tt> method, in order to enforce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * general contract of the <tt>Object.hashCode</tt> method among sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @return an identity-based set view of the keys contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @see System#identityHashCode(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        if (ks != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            return keySet = new KeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            int oldSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            IdentityHashMap.this.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            return size != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         * Must revert from AbstractSet's impl to AbstractCollection's, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         * the former contains an optimization that results in incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         * behavior when c is a smaller "normal" (non-identity-based) Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            boolean modified = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   988
            for (Iterator<K> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            for (K key : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                result += System.identityHashCode(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * modified while an iteration over the collection is in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * <tt>retainAll</tt> and <tt>clear</tt> methods.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * support the <tt>add</tt> or <tt>addAll</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * <p><b>While the object returned by this method implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <tt>Collection</tt> interface, it does <i>not</i> obey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * <tt>Collection's</tt> general contract.  Like its backing map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * the collection returned by this method defines element equality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * reference-equality rather than object-equality.  This affects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * behavior of its <tt>contains</tt>, <tt>remove</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * <tt>containsAll</tt> methods.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (vs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            return vs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return values = new Values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    private class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        public boolean remove(Object o) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1046
            for (Iterator<V> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                if (i.next() == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * Each element in the returned set is a reference-equality-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <tt>Map.Entry</tt>.  The set is backed by the map, so changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * to the map are reflected in the set, and vice-versa.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * map is modified while an iteration over the set is in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * the results of the iteration are undefined.  The set supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * element removal, which removes the corresponding mapping from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * methods.  It does not support the <tt>add</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * <tt>addAll</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * <p>Like the backing map, the <tt>Map.Entry</tt> objects in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * returned by this method define key and value equality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * reference-equality rather than object-equality.  This affects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * behavior of the <tt>equals</tt> and <tt>hashCode</tt> methods of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <tt>Map.Entry</tt> objects.  A reference-equality based <tt>Map.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * e</tt> is equal to an object <tt>o</tt> if and only if <tt>o</tt> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <tt>Map.Entry</tt> and <tt>e.getKey()==o.getKey() &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * e.getValue()==o.getValue()</tt>.  To accommodate these equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * semantics, the <tt>hashCode</tt> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * <tt>System.identityHashCode(e.getKey()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * System.identityHashCode(e.getValue())</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * <p><b>Owing to the reference-equality-based semantics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * <tt>Map.Entry</tt> instances in the set returned by this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * it is possible that the symmetry and transitivity requirements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * the {@link Object#equals(Object)} contract may be violated if any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * the entries in the set is compared to a normal map entry, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * the set returned by this method is compared to a set of normal map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * entries (such as would be returned by a call to this method on a normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * map).  However, the <tt>Object.equals</tt> contract is guaranteed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * hold among identity-based map entries, and among sets of such entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * </b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return a set view of the identity-mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (es != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            return es;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return entrySet = new EntrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            return containsMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return removeMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         * Must revert from AbstractSet's impl to AbstractCollection's, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         * the former contains an optimization that results in incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
         * behavior when c is a smaller "normal" (non-identity-based) Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            boolean modified = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1134
            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            Object[] result = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            Iterator<Map.Entry<K,V>> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            for (int i = 0; i < size; i++)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1148
                result[i] = new AbstractMap.SimpleEntry<>(it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                a = (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                    .newInstance(a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            Iterator<Map.Entry<K,V>> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            for (int i = 0; i < size; i++)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1160
                a[i] = (T) new AbstractMap.SimpleEntry<>(it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    private static final long serialVersionUID = 8188218128353913216L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Save the state of the <tt>IdentityHashMap</tt> instance to a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * (i.e., serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @serialData The <i>size</i> of the HashMap (the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *          mappings) (<tt>int</tt>), followed by the key (Object) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *          value (Object) for each key-value mapping represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     *          IdentityHashMap.  The key-value mappings are emitted in no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *          particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        throws java.io.IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        // Write out and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        // Write out keys and values (alternating)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        for (int i = 0; i < tab.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                s.writeObject(unmaskNull(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                s.writeObject(tab[i + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Reconstitute the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        throws java.io.IOException, ClassNotFoundException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // Read in any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        // Read in size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        // Allow for 33% growth (i.e., capacity is >= 2* size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        init(capacity((size*4)/3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        // Read the keys and values, and put the mappings in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            K key = (K) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            V value = (V) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            putForCreate(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * The put method for readObject.  It does not resize the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * update modCount, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    private void putForCreate(K key, V value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        K k = (K)maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        while ( (item = tab[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        tab[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        tab[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
}