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