jdk/src/java.base/share/classes/java/util/IdentityHashMap.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
child 32108 aa5490a167ee
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
     2
 * Copyright (c) 2000, 2014, 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;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
    27
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
    28
import java.lang.reflect.Array;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
    29
import java.util.function.BiConsumer;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
    30
import java.util.function.BiFunction;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
    31
import java.util.function.Consumer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements the <tt>Map</tt> interface with a hash table, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * reference-equality in place of object-equality when comparing keys (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * values).  In other words, in an <tt>IdentityHashMap</tt>, two keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <tt>(k1==k2)</tt>.  (In normal <tt>Map</tt> implementations (like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p><b>This class is <i>not</i> a general-purpose <tt>Map</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * implementation!  While this class implements the <tt>Map</tt> interface, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * intentionally violates <tt>Map's</tt> general contract, which mandates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * use of the <tt>equals</tt> method when comparing objects.  This class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * designed for use only in the rare cases wherein reference-equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * semantics are required.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>A typical use of this class is <i>topology-preserving object graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * transformations</i>, such as serialization or deep-copying.  To perform such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * a transformation, a program must maintain a "node table" that keeps track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of all the object references that have already been processed.  The node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * table must not equate distinct objects even if they happen to be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Another typical use of this class is to maintain <i>proxy objects</i>.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * example, a debugging facility might wish to maintain a proxy object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * each object in the program being debugged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>This class provides all of the optional map operations, and permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <tt>null</tt> values and the <tt>null</tt> key.  This class makes no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * guarantees as to the order of the map; in particular, it does not guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * that the order will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>This class provides constant-time performance for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * identity hash function ({@link System#identityHashCode(Object)})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * disperses elements properly among the buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>This class has one tuning parameter (which affects performance but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * semantics): <i>expected maximum size</i>.  This parameter is the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * number of key-value mappings that the map is expected to hold.  Internally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * this parameter is used to determine the number of buckets initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * comprising the hash table.  The precise relationship between the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * maximum size and the number of buckets is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>If the size of the map (the number of key-value mappings) sufficiently
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
    76
 * exceeds the expected maximum size, the number of buckets is increased.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Increasing the number of buckets ("rehashing") may be fairly expensive, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * it pays to create identity hash maps with a sufficiently large expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * maximum size.  On the other hand, iteration over collection views requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * time proportional to the number of buckets in the hash table, so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * pays not to set the expected maximum size too high if you are especially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * concerned with iteration performance or memory usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * If multiple threads access an identity hash map concurrently, and at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * least one of the threads modifies the map structurally, it <i>must</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * be synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   Map m = Collections.synchronizedMap(new IdentityHashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>The iterators returned by the <tt>iterator</tt> method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * collections returned by all of this class's "collection view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * methods" are <i>fail-fast</i>: if the map is structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * at any time after the iterator is created, in any way except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * through the iterator's own <tt>remove</tt> method, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * will throw a {@link ConcurrentModificationException}.  Thus, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * face of concurrent modification, the iterator fails quickly and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * cleanly, rather than risking arbitrary, non-deterministic behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * exception for its correctness: <i>fail-fast iterators should be used only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>Implementation note: This is a simple <i>linear-probe</i> hash table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * as described for example in texts by Sedgewick and Knuth.  The array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * alternates holding keys and values.  (This has better locality for large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * tables than does using separate arrays.)  For many JRE implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * and operation mixes, this class will yield better performance than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see     System#identityHashCode(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @author  Doug Lea and Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
public class IdentityHashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    implements Map<K,V>, java.io.Serializable, Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The initial capacity used by the no-args constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * MUST be a power of two.  The value 32 corresponds to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * (specified) expected maximum size of 21, given a load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * of 2/3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static final int DEFAULT_CAPACITY = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The minimum capacity, used if a lower value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * by either of the constructors with arguments.  The value 4 corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * to an expected maximum size of 2, given a load factor of 2/3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int MINIMUM_CAPACITY = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * MUST be a power of two <= 1<<29.
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   162
     *
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   163
     * In fact, the map can hold no more than MAXIMUM_CAPACITY-1 items
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   164
     * because it has to have at least one slot with the key == null
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   165
     * in order to avoid infinite loops in get(), put(), remove()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static final int MAXIMUM_CAPACITY = 1 << 29;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The table, resized as necessary. Length MUST always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   172
    transient Object[] table; // non-private to simplify nested class access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The number of key-value mappings contained in this identity hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   179
    int size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * The number of modifications, to support fast-fail iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   184
    transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Value representing null keys inside tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   189
    static final Object NULL_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Use NULL_KEY for key if it is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static Object maskNull(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return (key == null ? NULL_KEY : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Returns internal representation of null key back to caller as null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   201
    static final Object unmaskNull(Object key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return (key == NULL_KEY ? null : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Constructs a new, empty identity hash map with a default expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * maximum size (21).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public IdentityHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        init(DEFAULT_CAPACITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Constructs a new, empty map with the specified expected maximum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Putting more than the expected number of key-value mappings into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * the map may cause the internal data structure to grow, which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * somewhat time-consuming.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param expectedMaxSize the expected maximum size of the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws IllegalArgumentException if <tt>expectedMaxSize</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public IdentityHashMap(int expectedMaxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (expectedMaxSize < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IllegalArgumentException("expectedMaxSize is negative: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                               + expectedMaxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        init(capacity(expectedMaxSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   230
     * Returns the appropriate capacity for the given expected maximum size.
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   231
     * Returns the smallest power of two between MINIMUM_CAPACITY and
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   232
     * MAXIMUM_CAPACITY, inclusive, that is greater than (3 *
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   233
     * expectedMaxSize)/2, if such a number exists.  Otherwise returns
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   234
     * MAXIMUM_CAPACITY.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   236
    private static int capacity(int expectedMaxSize) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   237
        // assert expectedMaxSize >= 0;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   238
        return
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   239
            (expectedMaxSize > MAXIMUM_CAPACITY / 3) ? MAXIMUM_CAPACITY :
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   240
            (expectedMaxSize <= 2 * MINIMUM_CAPACITY / 3) ? MINIMUM_CAPACITY :
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   241
            Integer.highestOneBit(expectedMaxSize + (expectedMaxSize << 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Initializes object to be an empty map with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * capacity, which is assumed to be a power of two between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private void init(int initCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // assert (initCapacity & -initCapacity) == initCapacity; // power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // assert initCapacity >= MINIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // assert initCapacity <= MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        table = new Object[2 * initCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Constructs a new identity hash map containing the keys-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param m the map whose mappings are to be placed into this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public IdentityHashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Allow for a bit of growth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        this((int) ((1 + m.size()) * 1.1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the number of key-value mappings in this identity hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns <tt>true</tt> if this identity hash map contains no key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return <tt>true</tt> if this identity hash map contains no key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns index for Object x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private static int hash(Object x, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int h = System.identityHashCode(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // Multiply by -127, and left-shift to use least bit as part of hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return ((h << 1) - (h << 8)) & (length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Circularly traverses table of size len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static int nextKeyIndex(int i, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return (i + 2 < len ? i + 2 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * {@code k} to a value {@code v} such that {@code (key == k)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   323
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                return (V) tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Tests whether the specified object reference is a key in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return  <code>true</code> if the specified object reference is a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @see     #containsValue(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Tests whether the specified object reference is a value in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         specified object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see     #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Object[] tab = table;
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 65
diff changeset
   374
        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
   375
            if (tab[i] == value && tab[i - 1] != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Tests if the specified key-value mapping is in the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param   value possible value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @return  <code>true</code> if and only if the specified key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *          mapping is in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private boolean containsMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                return tab[i + 1] == value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Associates the specified value with the specified key in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * hash map.  If the map previously contained a mapping for the key, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * old value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param key the key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param value the value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see     #get(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see     #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public V put(K key, V value) {
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   420
        final Object k = maskNull(key);
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   421
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   422
        retryAfterResize: for (;;) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   423
            final Object[] tab = table;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   424
            final int len = tab.length;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   425
            int i = hash(k, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   427
            for (Object item; (item = tab[i]) != null;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   428
                 i = nextKeyIndex(i, len)) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   429
                if (item == k) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   430
                    @SuppressWarnings("unchecked")
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   431
                        V oldValue = (V) tab[i + 1];
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   432
                    tab[i + 1] = value;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   433
                    return oldValue;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   434
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   436
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   437
            final int s = size + 1;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   438
            // Use optimized form of 3 * s.
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   439
            // Next capacity is len, 2 * current capacity.
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   440
            if (s + (s << 1) > len && resize(len))
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   441
                continue retryAfterResize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   443
            modCount++;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   444
            tab[i] = k;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   445
            tab[i + 1] = value;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   446
            size = s;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   447
            return null;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   448
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   452
     * Resizes the table if necessary to hold given capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param newCapacity the new capacity, must be a power of two.
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   455
     * @return whether a resize did in fact take place
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   457
    private boolean resize(int newCapacity) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int newLength = newCapacity * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Object[] oldTable = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int oldLength = oldTable.length;
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   463
        if (oldLength == 2 * MAXIMUM_CAPACITY) { // can't expand any further
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   464
            if (size == MAXIMUM_CAPACITY - 1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                throw new IllegalStateException("Capacity exhausted.");
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   466
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (oldLength >= newLength)
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   469
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        Object[] newTable = new Object[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        for (int j = 0; j < oldLength; j += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            Object key = oldTable[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                Object value = oldTable[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                oldTable[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                oldTable[j+1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                int i = hash(key, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                while (newTable[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    i = nextKeyIndex(i, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                newTable[i] = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                newTable[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        table = newTable;
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   487
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int n = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return;
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   502
        if (n > size)
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   503
            resize(capacity(n)); // conservatively pre-expand
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        for (Entry<? extends K, ? extends V> e : m.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Removes the mapping for this key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                size--;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   529
                @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   530
                    V oldValue = (V) tab[i + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                closeDeletion(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Removes the specified key-value mapping from the map if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param   value possible value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @return  <code>true</code> if and only if the specified key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *          mapping was in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    private boolean removeMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if (tab[i + 1] != value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                closeDeletion(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Rehash all possibly-colliding entries following a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * deletion. This preserves the linear-probe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * collision properties required by get, put, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param d the index of a newly empty deleted slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private void closeDeletion(int d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        // Adapted from Knuth Section 6.4 Algorithm R
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // Look for items to swap into newly vacated slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        // starting at index immediately following deletion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // and continuing until a null slot is seen, indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // the end of a run of possibly-colliding keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
             i = nextKeyIndex(i, len) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            // The following test triggers if the item at slot i (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // hashes to be at slot r) should take the spot vacated by d.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // If so, we swap it in, and then continue with d now at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            // newly vacated i.  This process will terminate when we hit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            // the null slot at the end of this run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            // The test is messy because we are using a circular table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            int r = hash(item, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if ((i < r && (r <= d || d <= i)) || (r <= d && d <= i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                tab[d] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                tab[d + 1] = tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                d = i;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        for (int i = 0; i < tab.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Compares the specified object with this map for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <tt>true</tt> if the given object is also a map and the two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * represent identical object-reference mappings.  More formally, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * map is equal to another map <tt>m</tt> if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <tt>this.entrySet().equals(m.entrySet())</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <p><b>Owing to the reference-equality-based semantics of this map it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * possible that the symmetry and transitivity requirements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <tt>Object.equals</tt> contract may be violated if this map is compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * to a normal map.  However, the <tt>Object.equals</tt> contract is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * guaranteed to hold among <tt>IdentityHashMap</tt> instances.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param  o object to be compared for equality with this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } else if (o instanceof IdentityHashMap) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   643
            IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (m.size() != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            Object[] tab = m.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            for (int i = 0; i < tab.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                Object k = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                if (k != null && !containsMapping(k, tab[i + 1]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        } else if (o instanceof Map) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   655
            Map<?,?> m = (Map<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            return entrySet().equals(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return false;  // o is not a Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * defined to be the sum of the hash codes of each entry in the map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * <tt>IdentityHashMap</tt> instances <tt>m1</tt> and <tt>m2</tt>, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * required by the general contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <p><b>Owing to the reference-equality-based semantics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <tt>Map.Entry</tt> instances in the set returned by this map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <tt>entrySet</tt> method, it is possible that the contractual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * requirement of <tt>Object.hashCode</tt> mentioned in the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * paragraph will be violated if one of the two objects being compared is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * an <tt>IdentityHashMap</tt> instance and the other is a normal map.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        for (int i = 0; i < tab.length; i +=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                Object k = unmaskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                result += System.identityHashCode(k) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                          System.identityHashCode(tab[i + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * Returns a shallow copy of this identity hash map: the keys and values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        try {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   703
            IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            m.entrySet = null;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   705
            m.table = table.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9275
diff changeset
   708
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        int index = (size != 0 ? 0 : table.length); // current slot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        int expectedModCount = modCount; // to support fast-fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        int lastReturnedIndex = -1;      // to allow remove()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        boolean indexValid; // To avoid unnecessary next computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        Object[] traversalTable = table; // reference to main table or copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            Object[] tab = traversalTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            for (int i = index; i < tab.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    return indexValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            index = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        protected int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            if (!indexValid && !hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            indexValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            lastReturnedIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            return lastReturnedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            if (lastReturnedIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            expectedModCount = ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            int deletedSlot = lastReturnedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            // back up index to revisit new contents after deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            index = deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            indexValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            // Removal code proceeds as in closeDeletion except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            // it must catch the rare case where an element already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            // seen is swapped into a vacant slot that will be later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            // traversed by this iterator. We cannot allow future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // next() calls to return it again.  The likelihood of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // this occurring under 2/3 load factor is very slim, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // when it does happen, we must make a copy of the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            // the table to use for the rest of the traversal. Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            // this can only happen when we are near the end of the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // even in these rare cases, this is not very expensive in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            // time or space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            Object[] tab = traversalTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            int d = deletedSlot;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   773
            Object key = tab[d];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            tab[d] = null;        // vacate the slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            tab[d + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            // If traversing a copy, remove in real table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // We can skip gap-closure on copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (tab != IdentityHashMap.this.table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                IdentityHashMap.this.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
58
55e9cd9ade0b 6612102: (coll) IdentityHashMap.iterator().remove() might decrement size twice
martin
parents: 51
diff changeset
   785
            size--;
55e9cd9ade0b 6612102: (coll) IdentityHashMap.iterator().remove() might decrement size twice
martin
parents: 51
diff changeset
   786
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                 i = nextKeyIndex(i, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                int r = hash(item, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                // See closeDeletion for explanation of this conditional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                if ((i < r && (r <= d || d <= i)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    (r <= d && d <= i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    // If we are about to swap an already-seen element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    // into a slot that may later be returned by next(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    // then clone the rest of table for use in future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    // next() calls. It is OK that our copy will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    // a gap in the "wrong" place, since it will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    // be used for searching anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    if (i < deletedSlot && d >= deletedSlot &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                        traversalTable == IdentityHashMap.this.table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        int remaining = len - deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        Object[] newTable = new Object[remaining];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                        System.arraycopy(tab, deletedSlot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                         newTable, 0, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                        traversalTable = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    tab[d] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    tab[d + 1] = tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    d = i;
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    private class KeyIterator extends IdentityHashMapIterator<K> {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   823
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            return (K) unmaskNull(traversalTable[nextIndex()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    private class ValueIterator extends IdentityHashMapIterator<V> {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   830
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return (V) traversalTable[nextIndex() + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    private class EntryIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        extends IdentityHashMapIterator<Map.Entry<K,V>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    {
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   839
        private Entry lastReturnedEntry;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   840
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        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
   842
            lastReturnedEntry = new Entry(nextIndex());
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   843
            return lastReturnedEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   846
        public void remove() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   847
            lastReturnedIndex =
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   848
                ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   849
            super.remove();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   850
            lastReturnedEntry.index = lastReturnedIndex;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   851
            lastReturnedEntry = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   854
        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
   855
            private int index;
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
            private Entry(int index) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   858
                this.index = index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   859
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   860
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   861
            @SuppressWarnings("unchecked")
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   862
            public K getKey() {
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 (K) unmaskNull(traversalTable[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   865
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   867
            @SuppressWarnings("unchecked")
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   868
            public V getValue() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   869
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   870
                return (V) traversalTable[index+1];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   871
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   872
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   873
            @SuppressWarnings("unchecked")
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   874
            public V setValue(V value) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   875
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   876
                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
   877
                traversalTable[index+1] = value;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   878
                // 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
   879
                if (traversalTable != IdentityHashMap.this.table)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   880
                    put((K) traversalTable[index], value);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   881
                return oldValue;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   882
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   884
            public boolean equals(Object o) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   885
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   886
                    return super.equals(o);
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
                if (!(o instanceof Map.Entry))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   889
                    return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   890
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   891
                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
   892
                       e.getValue() == traversalTable[index+1]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   893
            }
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
            public int hashCode() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   896
                if (lastReturnedIndex < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   897
                    return super.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   899
                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
   900
                       System.identityHashCode(traversalTable[index+1]));
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   901
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   902
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   903
            public String toString() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   904
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   905
                    return super.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   907
                return (unmaskNull(traversalTable[index]) + "="
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   908
                        + traversalTable[index+1]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   909
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   911
            private void checkIndexForEntryUse() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   912
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   913
                    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
   914
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * This field is initialized to contain an instance of the entry set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * view the first time this view is requested.  The view is stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * so there's no reason to create more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   925
    private transient Set<Map.Entry<K,V>> entrySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Returns an identity-based set view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * The set is backed by the map, so changes to the map are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * the set, and vice-versa.  If the map is modified while an iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * over the set is in progress, the results of the iteration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * undefined.  The set supports element removal, which removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * corresponding mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * <tt>clear</tt> methods.  It does not support the <tt>add</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <tt>addAll</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * <p><b>While the object returned by this method implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <tt>Set</tt> interface, it does <i>not</i> obey <tt>Set's</tt> general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * contract.  Like its backing map, the set returned by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * defines element equality as reference-equality rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * object-equality.  This affects the behavior of its <tt>contains</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * <tt>remove</tt>, <tt>containsAll</tt>, <tt>equals</tt>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * <tt>hashCode</tt> methods.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <p><b>The <tt>equals</tt> method of the returned set returns <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * only if the specified object is a set containing exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * object references as the returned set.  The symmetry and transitivity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * requirements of the <tt>Object.equals</tt> contract may be violated if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * the set returned by this method is compared to a normal set.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * the <tt>Object.equals</tt> contract is guaranteed to hold among sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * returned by this method.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * <p>The <tt>hashCode</tt> method of the returned set returns the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * the <i>identity hashcodes</i> of the elements in the set, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * the sum of their hashcodes.  This is mandated by the change in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * semantics of the <tt>equals</tt> method, in order to enforce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * general contract of the <tt>Object.hashCode</tt> method among sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @return an identity-based set view of the keys contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @see System#identityHashCode(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (ks != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            return keySet = new KeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            int oldSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            IdentityHashMap.this.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            return size != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * Must revert from AbstractSet's impl to AbstractCollection's, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * the former contains an optimization that results in incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * behavior when c is a smaller "normal" (non-identity-based) Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        public boolean removeAll(Collection<?> c) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19208
diff changeset
   994
            Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            boolean modified = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   996
            for (Iterator<K> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            for (K key : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                result += System.identityHashCode(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1013
        public Object[] toArray() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1014
            return toArray(new Object[0]);
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1015
        }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1016
        @SuppressWarnings("unchecked")
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1017
        public <T> T[] toArray(T[] a) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1018
            int expectedModCount = modCount;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1019
            int size = size();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1020
            if (a.length < size)
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1021
                a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1022
            Object[] tab = table;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1023
            int ti = 0;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1024
            for (int si = 0; si < tab.length; si += 2) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1025
                Object key;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1026
                if ((key = tab[si]) != null) { // key present ?
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1027
                    // more elements than expected -> concurrent modification from other thread
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1028
                    if (ti >= size) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1029
                        throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1030
                    }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1031
                    a[ti++] = (T) unmaskNull(key); // unmask key
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1032
                }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1033
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1034
            // fewer elements than expected or concurrent modification from other thread detected
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1035
            if (ti < size || expectedModCount != modCount) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1036
                throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1037
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1038
            // final null marker as per spec
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1039
            if (ti < a.length) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1040
                a[ti] = null;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1041
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1042
            return a;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1043
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1044
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1045
        public Spliterator<K> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1046
            return new KeySpliterator<>(IdentityHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1047
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * modified while an iteration over the collection is in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <tt>retainAll</tt> and <tt>clear</tt> methods.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * support the <tt>add</tt> or <tt>addAll</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <p><b>While the object returned by this method implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * <tt>Collection</tt> interface, it does <i>not</i> obey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * <tt>Collection's</tt> general contract.  Like its backing map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * the collection returned by this method defines element equality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * reference-equality rather than object-equality.  This affects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * behavior of its <tt>contains</tt>, <tt>remove</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <tt>containsAll</tt> methods.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (vs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            return vs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            return values = new Values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    private class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        public boolean remove(Object o) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1089
            for (Iterator<V> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                if (i.next() == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1100
        public Object[] toArray() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1101
            return toArray(new Object[0]);
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1102
        }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1103
        @SuppressWarnings("unchecked")
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1104
        public <T> T[] toArray(T[] a) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1105
            int expectedModCount = modCount;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1106
            int size = size();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1107
            if (a.length < size)
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1108
                a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1109
            Object[] tab = table;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1110
            int ti = 0;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1111
            for (int si = 0; si < tab.length; si += 2) {
16042
0bf6469a1cfb 8008785: IdentityHashMap.values().toArray(V[]) broken by JDK-8008167
mduigou
parents: 15997
diff changeset
  1112
                if (tab[si] != null) { // key present ?
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1113
                    // more elements than expected -> concurrent modification from other thread
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1114
                    if (ti >= size) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1115
                        throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1116
                    }
16042
0bf6469a1cfb 8008785: IdentityHashMap.values().toArray(V[]) broken by JDK-8008167
mduigou
parents: 15997
diff changeset
  1117
                    a[ti++] = (T) tab[si+1]; // copy value
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1118
                }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1119
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1120
            // fewer elements than expected or concurrent modification from other thread detected
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1121
            if (ti < size || expectedModCount != modCount) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1122
                throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1123
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1124
            // final null marker as per spec
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1125
            if (ti < a.length) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1126
                a[ti] = null;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1127
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1128
            return a;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1129
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1130
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1131
        public Spliterator<V> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1132
            return new ValueSpliterator<>(IdentityHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1133
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * Each element in the returned set is a reference-equality-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <tt>Map.Entry</tt>.  The set is backed by the map, so changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * to the map are reflected in the set, and vice-versa.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * map is modified while an iteration over the set is in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * the results of the iteration are undefined.  The set supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * element removal, which removes the corresponding mapping from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * methods.  It does not support the <tt>add</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <tt>addAll</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * <p>Like the backing map, the <tt>Map.Entry</tt> objects in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * returned by this method define key and value equality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * reference-equality rather than object-equality.  This affects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * behavior of the <tt>equals</tt> and <tt>hashCode</tt> methods of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <tt>Map.Entry</tt> objects.  A reference-equality based <tt>Map.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * 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
  1155
     * <tt>Map.Entry</tt> and <tt>e.getKey()==o.getKey() &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * e.getValue()==o.getValue()</tt>.  To accommodate these equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * semantics, the <tt>hashCode</tt> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * <tt>System.identityHashCode(e.getKey()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * System.identityHashCode(e.getValue())</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * <p><b>Owing to the reference-equality-based semantics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * <tt>Map.Entry</tt> instances in the set returned by this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * it is possible that the symmetry and transitivity requirements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * the {@link Object#equals(Object)} contract may be violated if any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * the entries in the set is compared to a normal map entry, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * the set returned by this method is compared to a set of normal map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * entries (such as would be returned by a call to this method on a normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * map).  However, the <tt>Object.equals</tt> contract is guaranteed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * hold among identity-based map entries, and among sets of such entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * </b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @return a set view of the identity-mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (es != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return es;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            return entrySet = new EntrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1189
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            return containsMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1195
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            return removeMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         * Must revert from AbstractSet's impl to AbstractCollection's, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
         * the former contains an optimization that results in incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         * behavior when c is a smaller "normal" (non-identity-based) Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        public boolean removeAll(Collection<?> c) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19208
diff changeset
  1210
            Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            boolean modified = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1212
            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        public Object[] toArray() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1222
            return toArray(new Object[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        public <T> T[] toArray(T[] a) {
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1227
            int expectedModCount = modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (a.length < size)
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1230
                a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1231
            Object[] tab = table;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1232
            int ti = 0;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1233
            for (int si = 0; si < tab.length; si += 2) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1234
                Object key;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1235
                if ((key = tab[si]) != null) { // key present ?
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1236
                    // more elements than expected -> concurrent modification from other thread
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1237
                    if (ti >= size) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1238
                        throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1239
                    }
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 19855
diff changeset
  1240
                    a[ti++] = (T) new AbstractMap.SimpleEntry<>(unmaskNull(key), tab[si + 1]);
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1241
                }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1242
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1243
            // fewer elements than expected or concurrent modification from other thread detected
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1244
            if (ti < size || expectedModCount != modCount) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1245
                throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1246
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1247
            // final null marker as per spec
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1248
            if (ti < a.length) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1249
                a[ti] = null;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1250
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1253
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1254
        public Spliterator<Map.Entry<K,V>> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1255
            return new EntrySpliterator<>(IdentityHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1256
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    private static final long serialVersionUID = 8188218128353913216L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /**
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1263
     * Saves the state of the <tt>IdentityHashMap</tt> instance to a stream
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1264
     * (i.e., serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @serialData The <i>size</i> of the HashMap (the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *          mappings) (<tt>int</tt>), followed by the key (Object) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *          value (Object) for each key-value mapping represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *          IdentityHashMap.  The key-value mappings are emitted in no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *          particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        throws java.io.IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // Write out and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        // Write out keys and values (alternating)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        for (int i = 0; i < tab.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                s.writeObject(unmaskNull(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                s.writeObject(tab[i + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    /**
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1292
     * Reconstitutes the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1293
     * deserializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        throws java.io.IOException, ClassNotFoundException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        // Read in any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // Read in size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        int size = s.readInt();
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1302
        if (size < 0)
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1303
            throw new java.io.StreamCorruptedException
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1304
                ("Illegal mappings count: " + size);
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1305
        init(capacity(size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        // Read the keys and values, and put the mappings in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        for (int i=0; i<size; i++) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1309
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1310
                K key = (K) s.readObject();
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1311
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1312
                V value = (V) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            putForCreate(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * The put method for readObject.  It does not resize the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * update modCount, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    private void putForCreate(K key, V value)
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1322
        throws java.io.StreamCorruptedException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1324
        Object k = maskNull(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        while ( (item = tab[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        tab[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        tab[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1338
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18280
diff changeset
  1339
    @SuppressWarnings("unchecked")
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1340
    @Override
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1341
    public void forEach(BiConsumer<? super K, ? super V> action) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1342
        Objects.requireNonNull(action);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1343
        int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1344
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1345
        Object[] t = table;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1346
        for (int index = 0; index < t.length; index += 2) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1347
            Object k = t[index];
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1348
            if (k != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1349
                action.accept((K) unmaskNull(k), (V) t[index + 1]);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1350
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1351
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1352
            if (modCount != expectedModCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1353
                throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1354
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1355
        }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1356
    }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1357
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18280
diff changeset
  1358
    @SuppressWarnings("unchecked")
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1359
    @Override
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1360
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1361
        Objects.requireNonNull(function);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1362
        int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1363
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1364
        Object[] t = table;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1365
        for (int index = 0; index < t.length; index += 2) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1366
            Object k = t[index];
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1367
            if (k != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1368
                t[index + 1] = function.apply((K) unmaskNull(k), (V) t[index + 1]);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1369
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1370
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1371
            if (modCount != expectedModCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1372
                throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1373
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1374
        }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1375
    }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1376
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1377
    /**
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1378
     * Similar form as array-based Spliterators, but skips blank elements,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1379
     * and guestimates size as decreasing by half per split.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1380
     */
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1381
    static class IdentityHashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1382
        final IdentityHashMap<K,V> map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1383
        int index;             // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1384
        int fence;             // -1 until first use; then one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1385
        int est;               // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1386
        int expectedModCount;  // initialized when fence set
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1387
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1388
        IdentityHashMapSpliterator(IdentityHashMap<K,V> map, int origin,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1389
                                   int fence, int est, int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1390
            this.map = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1391
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1392
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1393
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1394
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1395
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1396
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1397
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1398
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1399
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1400
                est = map.size;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1401
                expectedModCount = map.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1402
                hi = fence = map.table.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1403
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1404
            return hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1405
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1406
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1407
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1408
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1409
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1410
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1411
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1412
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1413
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1414
        extends IdentityHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1415
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1416
        KeySpliterator(IdentityHashMap<K,V> map, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1417
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1418
            super(map, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1419
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1420
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1421
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1422
            int hi = getFence(), lo = index, mid = ((lo + hi) >>> 1) & ~1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1423
            return (lo >= mid) ? null :
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1424
                new KeySpliterator<>(map, lo, index = mid, est >>>= 1,
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1425
                                     expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1426
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1427
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1428
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1429
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1430
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1431
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1432
            int i, hi, mc; Object key;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1433
            IdentityHashMap<K,V> m; Object[] a;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1434
            if ((m = map) != null && (a = m.table) != null &&
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1435
                (i = index) >= 0 && (index = hi = getFence()) <= a.length) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1436
                for (; i < hi; i += 2) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1437
                    if ((key = a[i]) != null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1438
                        action.accept((K)unmaskNull(key));
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1439
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1440
                if (m.modCount == expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1441
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1442
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1443
            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1444
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1445
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1446
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1447
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1448
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1449
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1450
            Object[] a = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1451
            int hi = getFence();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1452
            while (index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1453
                Object key = a[index];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1454
                index += 2;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1455
                if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1456
                    action.accept((K)unmaskNull(key));
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1457
                    if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1458
                        throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1459
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1460
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1461
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1462
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1463
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1464
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1465
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1466
            return (fence < 0 || est == map.size ? SIZED : 0) | Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1467
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1468
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1469
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1470
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1471
        extends IdentityHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1472
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1473
        ValueSpliterator(IdentityHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1474
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1475
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1476
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1477
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1478
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1479
            int hi = getFence(), lo = index, mid = ((lo + hi) >>> 1) & ~1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1480
            return (lo >= mid) ? null :
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1481
                new ValueSpliterator<>(map, lo, index = mid, est >>>= 1,
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1482
                                       expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1483
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1484
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1485
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1486
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1487
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1488
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1489
            IdentityHashMap<K,V> m; Object[] a;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1490
            if ((m = map) != null && (a = m.table) != null &&
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1491
                (i = index) >= 0 && (index = hi = getFence()) <= a.length) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1492
                for (; i < hi; i += 2) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1493
                    if (a[i] != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1494
                        @SuppressWarnings("unchecked") V v = (V)a[i+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1495
                        action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1496
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1497
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1498
                if (m.modCount == expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1499
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1500
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1501
            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1502
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1503
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1504
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1505
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1506
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1507
            Object[] a = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1508
            int hi = getFence();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1509
            while (index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1510
                Object key = a[index];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1511
                @SuppressWarnings("unchecked") V v = (V)a[index+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1512
                index += 2;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1513
                if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1514
                    action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1515
                    if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1516
                        throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1517
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1518
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1519
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1520
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1521
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1522
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1523
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1524
            return (fence < 0 || est == map.size ? SIZED : 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1525
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1526
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1527
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1528
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1529
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1530
        extends IdentityHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1531
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1532
        EntrySpliterator(IdentityHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1533
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1534
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1535
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1536
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1537
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1538
            int hi = getFence(), lo = index, mid = ((lo + hi) >>> 1) & ~1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1539
            return (lo >= mid) ? null :
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1540
                new EntrySpliterator<>(map, lo, index = mid, est >>>= 1,
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1541
                                       expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1542
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1543
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1544
        public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1545
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1546
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1547
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1548
            IdentityHashMap<K,V> m; Object[] a;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1549
            if ((m = map) != null && (a = m.table) != null &&
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1550
                (i = index) >= 0 && (index = hi = getFence()) <= a.length) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1551
                for (; i < hi; i += 2) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1552
                    Object key = a[i];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1553
                    if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1554
                        @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1555
                            (K)unmaskNull(key);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1556
                        @SuppressWarnings("unchecked") V v = (V)a[i+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1557
                        action.accept
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1558
                            (new AbstractMap.SimpleImmutableEntry<>(k, v));
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1559
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1560
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1561
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1562
                if (m.modCount == expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1563
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1564
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1565
            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1566
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1567
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1568
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1569
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1570
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1571
            Object[] a = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1572
            int hi = getFence();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1573
            while (index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1574
                Object key = a[index];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1575
                @SuppressWarnings("unchecked") V v = (V)a[index+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1576
                index += 2;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1577
                if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1578
                    @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1579
                        (K)unmaskNull(key);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1580
                    action.accept
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1581
                        (new AbstractMap.SimpleImmutableEntry<>(k, v));
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1582
                    if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1583
                        throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1584
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1585
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1586
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1587
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1588
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1589
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1590
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1591
            return (fence < 0 || est == map.size ? SIZED : 0) | Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1592
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1593
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1594
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
}