jdk/src/share/classes/java/util/WeakHashMap.java
author henryjen
Mon, 08 Jul 2013 15:46:26 -0400
changeset 18825 06636235cd12
parent 18280 6c3c0ff49eb5
child 19208 1e3d351eba80
permissions -rw-r--r--
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces Reviewed-by: psandoz, mduigou Contributed-by: brian goetz <brian.goetz@oracle.com>
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: 14342
diff changeset
     2
 * Copyright (c) 1998, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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: 14342
diff changeset
    27
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.ReferenceQueue;
18166
a24e00a7c5ae 8010325: Remove hash32() method and hash32 int field from java.lang.String
bchristi
parents: 17949
diff changeset
    30
import java.util.concurrent.ThreadLocalRandom;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
    31
import java.util.function.BiConsumer;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
    32
import java.util.function.BiFunction;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
    33
import java.util.function.Consumer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
56
48451b4616e8 5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents: 2
diff changeset
    37
 * Hash table based implementation of the <tt>Map</tt> interface, with
48451b4616e8 5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents: 2
diff changeset
    38
 * <em>weak keys</em>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * An entry in a <tt>WeakHashMap</tt> will automatically be removed when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * its key is no longer in ordinary use.  More precisely, the presence of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * mapping for a given key will not prevent the key from being discarded by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * garbage collector, that is, made finalizable, finalized, and then reclaimed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When a key has been discarded its entry is effectively removed from the map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * so this class behaves somewhat differently from other <tt>Map</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> Both null values and the null key are supported. This class has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * performance characteristics similar to those of the <tt>HashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * class, and has the same efficiency parameters of <em>initial capacity</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and <em>load factor</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p> Like most collection classes, this class is not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * A synchronized <tt>WeakHashMap</tt> may be constructed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p> This class is intended primarily for use with key objects whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tt>equals</tt> methods test for object identity using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <tt>==</tt> operator.  Once such a key is discarded it can never be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * recreated, so it is impossible to do a lookup of that key in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <tt>WeakHashMap</tt> at some later time and be surprised that its entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * has been removed.  This class will work perfectly well with key objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * whose <tt>equals</tt> methods are not based upon object identity, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * as <tt>String</tt> instances.  With such recreatable key objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * however, the automatic removal of <tt>WeakHashMap</tt> entries whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * keys have been discarded may prove to be confusing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p> The behavior of the <tt>WeakHashMap</tt> class depends in part upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the actions of the garbage collector, so several familiar (though not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * required) <tt>Map</tt> invariants do not hold for this class.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the garbage collector may discard keys at any time, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <tt>WeakHashMap</tt> may behave as though an unknown thread is silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * removing entries.  In particular, even if you synchronize on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <tt>WeakHashMap</tt> instance and invoke none of its mutator methods, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * is possible for the <tt>size</tt> method to return smaller values over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * time, for the <tt>isEmpty</tt> method to return <tt>false</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * then <tt>true</tt>, for the <tt>containsKey</tt> method to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <tt>true</tt> and later <tt>false</tt> for a given key, for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <tt>get</tt> method to return a value for a given key but later return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <tt>null</tt>, for the <tt>put</tt> method to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <tt>null</tt> and the <tt>remove</tt> method to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <tt>false</tt> for a key that previously appeared to be in the map, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * for successive examinations of the key set, the value collection, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * the entry set to yield successively smaller numbers of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p> Each key object in a <tt>WeakHashMap</tt> is stored indirectly as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * the referent of a weak reference.  Therefore a key will automatically be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * removed only after the weak references to it, both inside and outside of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * map, have been cleared by the garbage collector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p> <strong>Implementation note:</strong> The value objects in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <tt>WeakHashMap</tt> are held by ordinary strong references.  Thus care
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * should be taken to ensure that value objects do not strongly refer to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * own keys, either directly or indirectly, since that will prevent the keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * from being discarded.  Note that a value object may refer indirectly to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * key via the <tt>WeakHashMap</tt> itself; that is, a value object may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * strongly refer to some other key object whose associated value object, in
12864
c404d4a709de 7166055: Javadoc for WeakHashMap contains misleading advice
littlee
parents: 12859
diff changeset
    98
 * turn, strongly refers to the key of the first value object.  If the values
c404d4a709de 7166055: Javadoc for WeakHashMap contains misleading advice
littlee
parents: 12859
diff changeset
    99
 * in the map do not rely on the map holding strong references to them, one way
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * to deal with this is to wrap values themselves within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <tt>WeakReferences</tt> before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * inserting, as in: <tt>m.put(key, new WeakReference(value))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * and then unwrapping upon each <tt>get</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>The iterators returned by the <tt>iterator</tt> method of the collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * returned by all of this class's "collection view methods" are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <i>fail-fast</i>: if the map is structurally modified at any time after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * iterator is created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <tt>remove</tt> method, the iterator will throw a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * arbitrary, non-deterministic behavior at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * exception for its correctness:  <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @author      Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @author      Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @since       1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see         java.util.HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @see         java.lang.ref.WeakReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
public class WeakHashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    implements Map<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The default initial capacity -- MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final int DEFAULT_INITIAL_CAPACITY = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * MUST be a power of two <= 1<<30.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static final int MAXIMUM_CAPACITY = 1 << 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The load factor used when none specified in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final float DEFAULT_LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The table, resized as necessary. Length MUST Always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    Entry<K,V>[] table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * The number of key-value mappings contained in this weak hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The next size value at which to resize (capacity * load factor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * The load factor for the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private final float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Reference queue for cleared WeakEntries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   180
    private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * The number of times this WeakHashMap has been structurally modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Structural modifications are those that change the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * mappings in the map or otherwise modify its internal structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * (e.g., rehash).  This field is used to make iterators on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Collection-views of the map fail-fast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @see ConcurrentModificationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
65
51fc1d79463f 6625725: (coll) modCount should not be volatile
martin
parents: 56
diff changeset
   191
    int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   193
    private static class Holder {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   194
        static final boolean USE_HASHSEED;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   195
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   196
        static {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   197
            String hashSeedProp = java.security.AccessController.doPrivileged(
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   198
                    new sun.security.action.GetPropertyAction(
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   199
                        "jdk.map.useRandomSeed"));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   200
            boolean localBool = (null != hashSeedProp)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   201
                    ? Boolean.parseBoolean(hashSeedProp) : false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   202
            USE_HASHSEED = localBool;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   203
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   204
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   205
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   206
    /**
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   207
     * A randomizing value associated with this instance that is applied to
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   208
     * hash code of keys to make hash collisions harder to find.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   209
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   210
     * Non-final so it can be set lazily, but be sure not to set more than once.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   211
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   212
    transient int hashSeed;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   213
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   214
    /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   215
     * Initialize the hashing mask value.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   216
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   217
    final void initHashSeed() {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   218
        if (sun.misc.VM.isBooted() && Holder.USE_HASHSEED) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   219
            // Do not set hashSeed more than once!
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   220
            // assert hashSeed == 0;
18166
a24e00a7c5ae 8010325: Remove hash32() method and hash32 int field from java.lang.String
bchristi
parents: 17949
diff changeset
   221
            int seed = ThreadLocalRandom.current().nextInt();
a24e00a7c5ae 8010325: Remove hash32() method and hash32 int field from java.lang.String
bchristi
parents: 17949
diff changeset
   222
            hashSeed = (seed != 0) ? seed : 1;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   223
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   224
    }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   225
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private Entry<K,V>[] newTable(int n) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   228
        return (Entry<K,V>[]) new Entry<?,?>[n];
2
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
     * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * capacity and the given load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param  initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param  loadFactor      The load factor of the <tt>WeakHashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @throws IllegalArgumentException if the initial capacity is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         or if the load factor is nonpositive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public WeakHashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new IllegalArgumentException("Illegal Initial Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (initialCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            initialCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new IllegalArgumentException("Illegal Load factor: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                               loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int capacity = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        while (capacity < initialCapacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            capacity <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        table = newTable(capacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        this.loadFactor = loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        threshold = (int)(capacity * loadFactor);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   256
        initHashSeed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * capacity and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param  initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public WeakHashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Constructs a new, empty <tt>WeakHashMap</tt> with the default initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * capacity (16) and load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public WeakHashMap() {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   275
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Constructs a new <tt>WeakHashMap</tt> with the same mappings as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * specified map.  The <tt>WeakHashMap</tt> is created with the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * load factor (0.75) and an initial capacity sufficient to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * mappings in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param   m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws  NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public WeakHashMap(Map<? extends K, ? extends V> m) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   289
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   290
                DEFAULT_INITIAL_CAPACITY),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             DEFAULT_LOAD_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // internal utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Value representing null keys inside tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static final Object NULL_KEY = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Use NULL_KEY for key if it is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static Object maskNull(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return (key == null) ? NULL_KEY : key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns internal representation of null key back to caller as null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    static Object unmaskNull(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return (key == NULL_KEY) ? null : key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Checks for equality of non-null reference x and possibly-null y.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * default uses Object.equals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private static boolean eq(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return x == y || x.equals(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   325
     * Retrieve object hash code and applies a supplemental hash function to the
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   326
     * result hash, which defends against poor quality hash functions.  This is
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   327
     * critical because HashMap uses power-of-two length hash tables, that
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   328
     * otherwise encounter collisions for hashCodes that do not differ
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   329
     * in lower bits.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   330
     */
13018
92c86cea72a8 7173919: Minor optimization of hashing methods
mduigou
parents: 12864
diff changeset
   331
    final int hash(Object k) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   332
        int h = hashSeed ^ k.hashCode();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   333
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   334
        // This function ensures that hashCodes that differ only by
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   335
        // constant multiples at each bit position have a bounded
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   336
        // number of collisions (approximately 8 at default load factor).
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   337
        h ^= (h >>> 20) ^ (h >>> 12);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   338
        return h ^ (h >>> 7) ^ (h >>> 4);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   339
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   340
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   341
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns index for hash code h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private static int indexFor(int h, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return h & (length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Expunges stale entries from the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private void expungeStaleEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (Object x; (x = queue.poll()) != null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            synchronized (queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    Entry<K,V> e = (Entry<K,V>) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                int i = indexFor(e.hash, table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                Entry<K,V> prev = table[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                Entry<K,V> p = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    Entry<K,V> next = p.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if (p == e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        if (prev == e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            table[i] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        // Must not null out e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        // stale entries may be in use by a HashIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        e.value = null; // Help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    prev = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    p = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Returns the table after first expunging stale entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private Entry<K,V>[] getTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        expungeStaleEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * This result is a snapshot, and may not reflect unprocessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * entries that will be removed before next attempted access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * because they are no longer referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        expungeStaleEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns <tt>true</tt> if this map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * This result is a snapshot, and may not reflect unprocessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * entries that will be removed before next attempted access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * because they are no longer referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        Object k = maskNull(key);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   430
        int h = hash(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        int index = indexFor(h, tab.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        Entry<K,V> e = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (e.hash == h && eq(k, e.get()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                return e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Returns <tt>true</tt> if this map contains a mapping for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param  key   The key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return <tt>true</tt> if there is a mapping for <tt>key</tt>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         <tt>false</tt> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return getEntry(key) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the entry associated with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns null if the map contains no mapping for this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    Entry<K,V> getEntry(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        Object k = maskNull(key);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   460
        int h = hash(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int index = indexFor(h, tab.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        Entry<K,V> e = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        while (e != null && !(e.hash == h && eq(k, e.get())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * If the map previously contained a mapping for this key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param key key with which the specified value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @param value value to be associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        Object k = maskNull(key);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   483
        int h = hash(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int i = indexFor(h, tab.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (h == e.hash && eq(k, e.get())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                V oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (value != oldValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                return oldValue;
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
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        Entry<K,V> e = tab[i];
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   498
        tab[i] = new Entry<>(k, value, queue, h, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (++size >= threshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            resize(tab.length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Rehashes the contents of this map into a new array with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * larger capacity.  This method is called automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * number of keys in this map reaches its threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * If current capacity is MAXIMUM_CAPACITY, this method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * resize the map, but sets threshold to Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * This has the effect of preventing future calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param newCapacity the new capacity, MUST be a power of two;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *        must be greater than current capacity unless current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *        capacity is MAXIMUM_CAPACITY (in which case value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *        is irrelevant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    void resize(int newCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        Entry<K,V>[] oldTable = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int oldCapacity = oldTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (oldCapacity == MAXIMUM_CAPACITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            threshold = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        Entry<K,V>[] newTable = newTable(newCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        transfer(oldTable, newTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        table = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * If ignoring null elements and processing ref queue caused massive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * shrinkage, then restore old table.  This should be rare, but avoids
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * unbounded expansion of garbage-filled tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (size >= threshold / 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            threshold = (int)(newCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            expungeStaleEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            transfer(newTable, oldTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            table = oldTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /** Transfers all entries from src to dest tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        for (int j = 0; j < src.length; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            Entry<K,V> e = src[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            src[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                Entry<K,V> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                Object key = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    e.next = null;  // Help GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    e.value = null; //  "   "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    int i = indexFor(e.hash, dest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    e.next = dest[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    dest[i] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * These mappings will replace any mappings that this map had for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param m mappings to be stored in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @throws  NullPointerException if the specified map is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        int numKeysToBeAdded = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (numKeysToBeAdded == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * Expand the map if the map if the number of mappings to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * is greater than or equal to threshold.  This is conservative; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * obvious condition is (m.size() + size) >= threshold, but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * condition could result in a map with twice the appropriate capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * if the keys to be added overlap with the keys already in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * By using the conservative calculation, we subject ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * to at most one extra resize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (numKeysToBeAdded > threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (targetCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                targetCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            int newCapacity = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            while (newCapacity < targetCapacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                newCapacity <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (newCapacity > table.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                resize(newCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Removes the mapping for a key from this weak hash map if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * More formally, if this map contains a mapping from key <tt>k</tt> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * value <tt>v</tt> such that <code>(key==null ?  k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * key.equals(k))</code>, that mapping is removed.  (The map can contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <p>Returns the value to which this map previously associated the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * or <tt>null</tt> if the map contained no mapping for the key.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * return value of <tt>null</tt> does not <i>necessarily</i> indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * that the map contained no mapping for the key; it's also possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * that the map explicitly mapped the key to <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <p>The map will not contain a mapping for the specified key once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        Object k = maskNull(key);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   625
        int h = hash(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int i = indexFor(h, tab.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        Entry<K,V> prev = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        Entry<K,V> e = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            Entry<K,V> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            if (h == e.hash && eq(k, e.get())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                if (prev == e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    tab[i] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                return e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            prev = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /** Special version of remove needed by Entry set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    boolean removeMapping(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        Object k = maskNull(entry.getKey());
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   656
        int h = hash(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        int i = indexFor(h, tab.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        Entry<K,V> prev = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        Entry<K,V> e = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            Entry<K,V> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (h == e.hash && e.equals(entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                if (prev == e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    tab[i] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            prev = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        // clear out ref queue. We don't need to expunge entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        // since table is getting cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        while (queue.poll() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        Arrays.fill(table, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        // Allocation of array may have caused GC, which may have caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // additional entries to go stale.  Removing these entries from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        // reference queue will make them eligible for reclamation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        while (queue.poll() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (value==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return containsNullValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        for (int i = tab.length; i-- > 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            for (Entry<K,V> e = tab[i]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                if (value.equals(e.value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * Special-case code for containsValue with null argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    private boolean containsNullValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        Entry<K,V>[] tab = getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        for (int i = tab.length; i-- > 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            for (Entry<K,V> e = tab[i]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                if (e.value==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * The entries in this hash table extend WeakReference, using its main ref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * field as the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        Entry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * Creates new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        Entry(Object key, V value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
              ReferenceQueue<Object> queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
              int hash, Entry<K,V> next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            super(key, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            this.hash  = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            this.next  = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return (K) WeakHashMap.unmaskNull(get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        public V setValue(V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            V oldValue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            K k1 = getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            Object k2 = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                V v1 = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                Object v2 = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            K k = getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            V v = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            return ((k==null ? 0 : k.hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    (v==null ? 0 : v.hashCode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            return getKey() + "=" + getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    private abstract class HashIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        private Entry<K,V> entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        private Entry<K,V> lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        private int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         * Strong reference needed to avoid disappearance of key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * between hasNext and next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        private Object nextKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * Strong reference needed to avoid disappearance of key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         * between nextEntry() and any use of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        private Object currentKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        HashIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            index = isEmpty() ? 0 : table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            Entry<K,V>[] t = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            while (nextKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                Entry<K,V> e = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                int i = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                while (e == null && i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    e = t[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                entry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    currentKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                nextKey = e.get(); // hold on to key in strong ref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (nextKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        /** The common parts of next() across different types of iterators */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        protected Entry<K,V> nextEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (nextKey == null && !hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            lastReturned = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            currentKey = nextKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            nextKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            WeakHashMap.this.remove(currentKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            currentKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    private class ValueIterator extends HashIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return nextEntry().value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    private class KeyIterator extends HashIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return nextEntry().getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    private class EntryIterator extends HashIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return nextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * the iterator's own <tt>remove</tt> operation), the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * which removes the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return (ks != null ? ks : (keySet = new KeySet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            return WeakHashMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            if (containsKey(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                WeakHashMap.this.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            WeakHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   931
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   932
        public Spliterator<K> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   933
            return new KeySpliterator<>(WeakHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   934
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * modified while an iteration over the collection is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * (except through the iterator's own <tt>remove</tt> operation),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return (vs != null) ? vs : (values = new Values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    private class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            return WeakHashMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            WeakHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   971
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   972
        public Spliterator<V> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   973
            return new ValueSpliterator<>(WeakHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   974
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * the iterator's own <tt>remove</tt> operation, or through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * <tt>setValue</tt> operation on a map entry returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * <tt>clear</tt> operations.  It does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        return es != null ? es : (entrySet = new EntrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            Entry<K,V> candidate = getEntry(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            return candidate != null && candidate.equals(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            return removeMapping(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            return WeakHashMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            WeakHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        private List<Map.Entry<K,V>> deepCopy() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1022
            List<Map.Entry<K,V>> list = new ArrayList<>(size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            for (Map.Entry<K,V> e : this)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1024
                list.add(new AbstractMap.SimpleEntry<>(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            return deepCopy().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return deepCopy().toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1035
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1036
        public Spliterator<Map.Entry<K,V>> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1037
            return new EntrySpliterator<>(WeakHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1038
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1040
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1041
    @Override
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1042
    public void forEach(BiConsumer<? super K, ? super V> action) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1043
        Objects.requireNonNull(action);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1044
        int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1045
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1046
        Entry<K, V>[] tab = getTable();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1047
        for (Entry<K, V> entry : tab) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1048
            while (entry != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1049
                Object key = entry.get();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1050
                if (key != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1051
                    action.accept((K)WeakHashMap.unmaskNull(key), entry.value);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1052
                }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1053
                entry = entry.next;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1054
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1055
                if (expectedModCount != modCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1056
                    throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1057
                }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1058
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1059
        }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1060
    }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1061
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1062
    @Override
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1063
    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: 18166
diff changeset
  1064
        Objects.requireNonNull(function);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1065
        int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1066
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1067
        Entry<K, V>[] tab = getTable();;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1068
        for (Entry<K, V> entry : tab) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1069
            while (entry != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1070
                Object key = entry.get();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1071
                if (key != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1072
                    entry.value = function.apply((K)WeakHashMap.unmaskNull(key), entry.value);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1073
                }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1074
                entry = entry.next;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1075
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1076
                if (expectedModCount != modCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1077
                    throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1078
                }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1079
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1080
        }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1081
    }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1082
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1083
    /**
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1084
     * Similar form as other hash Spliterators, but skips dead
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1085
     * elements.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1086
     */
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1087
    static class WeakHashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1088
        final WeakHashMap<K,V> map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1089
        WeakHashMap.Entry<K,V> current; // current node
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1090
        int index;             // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1091
        int fence;             // -1 until first use; then one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1092
        int est;               // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1093
        int expectedModCount;  // for comodification checks
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1094
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1095
        WeakHashMapSpliterator(WeakHashMap<K,V> m, int origin,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1096
                               int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1097
                               int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1098
            this.map = m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1099
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1100
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1101
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1102
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1103
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1104
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1105
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1106
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1107
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1108
                WeakHashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1109
                est = m.size();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1110
                expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1111
                hi = fence = m.table.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1112
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1113
            return hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1114
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1115
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1116
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1117
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1118
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1119
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1120
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1121
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1122
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1123
        extends WeakHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1124
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1125
        KeySpliterator(WeakHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1126
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1127
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1128
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1129
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1130
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1131
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1132
            return (lo >= mid) ? null :
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1133
                new KeySpliterator<K,V>(map, lo, index = mid, est >>>= 1,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1134
                                        expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1135
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1136
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1137
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1138
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1139
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1140
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1141
            WeakHashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1142
            WeakHashMap.Entry<K,V>[] tab = m.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1143
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1144
                mc = expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1145
                hi = fence = tab.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1146
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1147
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1148
                mc = expectedModCount;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1149
            if (tab.length >= hi && (i = index) >= 0 &&
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1150
                (i < (index = hi) || current != null)) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1151
                WeakHashMap.Entry<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1152
                current = null; // exhaust
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1153
                do {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1154
                    if (p == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1155
                        p = tab[i++];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1156
                    else {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1157
                        Object x = p.get();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1158
                        p = p.next;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1159
                        if (x != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1160
                            @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1161
                                (K) WeakHashMap.unmaskNull(x);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1162
                            action.accept(k);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1163
                        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1164
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1165
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1166
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1167
            if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1168
                throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1169
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1170
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1171
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1172
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1173
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1174
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1175
            WeakHashMap.Entry<K,V>[] tab = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1176
            if (tab.length >= (hi = getFence()) && index >= 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1177
                while (current != null || index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1178
                    if (current == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1179
                        current = tab[index++];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1180
                    else {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1181
                        Object x = current.get();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1182
                        current = current.next;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1183
                        if (x != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1184
                            @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1185
                                (K) WeakHashMap.unmaskNull(x);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1186
                            action.accept(k);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1187
                            if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1188
                                throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1189
                            return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1190
                        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1191
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1192
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1193
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1194
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1195
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1196
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1197
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1198
            return Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1199
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1200
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1201
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1202
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1203
        extends WeakHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1204
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1205
        ValueSpliterator(WeakHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1206
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1207
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1208
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1209
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1210
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1211
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1212
            return (lo >= mid) ? null :
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1213
                new ValueSpliterator<K,V>(map, lo, index = mid, est >>>= 1,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1214
                                          expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1215
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1216
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1217
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1218
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1219
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1220
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1221
            WeakHashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1222
            WeakHashMap.Entry<K,V>[] tab = m.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1223
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1224
                mc = expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1225
                hi = fence = tab.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1226
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1227
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1228
                mc = expectedModCount;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1229
            if (tab.length >= hi && (i = index) >= 0 &&
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1230
                (i < (index = hi) || current != null)) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1231
                WeakHashMap.Entry<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1232
                current = null; // exhaust
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1233
                do {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1234
                    if (p == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1235
                        p = tab[i++];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1236
                    else {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1237
                        Object x = p.get();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1238
                        V v = p.value;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1239
                        p = p.next;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1240
                        if (x != null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1241
                            action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1242
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1243
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1244
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1245
            if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1246
                throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1247
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1248
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1249
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1250
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1251
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1252
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1253
            WeakHashMap.Entry<K,V>[] tab = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1254
            if (tab.length >= (hi = getFence()) && index >= 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1255
                while (current != null || index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1256
                    if (current == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1257
                        current = tab[index++];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1258
                    else {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1259
                        Object x = current.get();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1260
                        V v = current.value;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1261
                        current = current.next;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1262
                        if (x != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1263
                            action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1264
                            if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1265
                                throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1266
                            return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1267
                        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1268
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1269
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1270
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1271
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1272
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1273
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1274
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1275
            return 0;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1276
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1277
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1278
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1279
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1280
        extends WeakHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1281
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1282
        EntrySpliterator(WeakHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1283
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1284
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1285
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1286
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1287
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1288
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1289
            return (lo >= mid) ? null :
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1290
                new EntrySpliterator<K,V>(map, lo, index = mid, est >>>= 1,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1291
                                          expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1292
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1293
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1294
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1295
        public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1296
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1297
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1298
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1299
            WeakHashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1300
            WeakHashMap.Entry<K,V>[] tab = m.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1301
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1302
                mc = expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1303
                hi = fence = tab.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1304
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1305
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1306
                mc = expectedModCount;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1307
            if (tab.length >= hi && (i = index) >= 0 &&
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1308
                (i < (index = hi) || current != null)) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1309
                WeakHashMap.Entry<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1310
                current = null; // exhaust
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1311
                do {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1312
                    if (p == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1313
                        p = tab[i++];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1314
                    else {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1315
                        Object x = p.get();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1316
                        V v = p.value;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1317
                        p = p.next;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1318
                        if (x != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1319
                            @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1320
                                (K) WeakHashMap.unmaskNull(x);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1321
                            action.accept
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1322
                                (new AbstractMap.SimpleImmutableEntry<K,V>(k, v));
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1323
                        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1324
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1325
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1326
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1327
            if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1328
                throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1329
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1330
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1331
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1332
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1333
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1334
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1335
            WeakHashMap.Entry<K,V>[] tab = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1336
            if (tab.length >= (hi = getFence()) && index >= 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1337
                while (current != null || index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1338
                    if (current == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1339
                        current = tab[index++];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1340
                    else {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1341
                        Object x = current.get();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1342
                        V v = current.value;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1343
                        current = current.next;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1344
                        if (x != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1345
                            @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1346
                                (K) WeakHashMap.unmaskNull(x);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1347
                            action.accept
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1348
                                (new AbstractMap.SimpleImmutableEntry<K,V>(k, v));
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1349
                            if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1350
                                throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1351
                            return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1352
                        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1353
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1354
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1355
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1356
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1357
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1358
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1359
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1360
            return Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1361
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1362
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
  1363
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
}