src/java.base/share/classes/java/util/Hashtable.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58242 94bb65cb37d3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52427
diff changeset
     2
 * Copyright (c) 1994, 2019, 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: 5466
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: 5466
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: 5466
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
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;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    27
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    29
import java.util.function.BiConsumer;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    30
import java.util.function.Function;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    31
import java.util.function.BiFunction;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49774
diff changeset
    32
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
56
48451b4616e8 5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents: 2
diff changeset
    35
 * This class implements a hash table, which maps keys to values. Any
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    36
 * non-{@code null} object can be used as a key or as a value. <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * To successfully store and retrieve objects from a hashtable, the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    39
 * objects used as keys must implement the {@code hashCode}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    40
 * method and the {@code equals} method. <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    42
 * An instance of {@code Hashtable} has two parameters that affect its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <i>initial capacity</i> is simply the capacity at the time the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * is created.  Note that the hash table is <i>open</i>: in the case of a "hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * collision", a single bucket stores multiple entries, which must be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * sequentially.  The <i>load factor</i> is a measure of how full the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * table is allowed to get before its capacity is automatically increased.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The initial capacity and load factor parameters are merely hints to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the implementation.  The exact details as to when and whether the rehash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * method is invoked are implementation-dependent.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Generally, the default load factor (.75) offers a good tradeoff between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * time and space costs.  Higher values decrease the space overhead but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * increase the time cost to look up an entry (which is reflected in most
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    57
 * {@code Hashtable} operations, including {@code get} and {@code put}).<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The initial capacity controls a tradeoff between wasted space and the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    60
 * need for {@code rehash} operations, which are time-consuming.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    61
 * No {@code rehash} operations will <i>ever</i> occur if the initial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * capacity is greater than the maximum number of entries the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    63
 * {@code Hashtable} will contain divided by its load factor.  However,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * setting the initial capacity too high can waste space.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    66
 * If many entries are to be made into a {@code Hashtable},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * creating it with a sufficiently large capacity may allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * entries to be inserted more efficiently than letting it perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * automatic rehashing as needed to grow the table. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * This example creates a hashtable of numbers. It uses the names of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * the numbers as keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <pre>   {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   Hashtable<String, Integer> numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *     = new Hashtable<String, Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   numbers.put("one", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   numbers.put("two", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   numbers.put("three", 3);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>To retrieve a number, use the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <pre>   {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   Integer n = numbers.get("two");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     System.out.println("two = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    87
 * <p>The iterators returned by the {@code iterator} method of the collections
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * returned by all of this class's "collection view methods" are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <em>fail-fast</em>: if the Hashtable is structurally modified at any time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * after the iterator is created, in any way except through the iterator's own
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    91
 * {@code remove} method, the iterator will throw a {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * arbitrary, non-deterministic behavior at an undetermined time in the future.
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
    95
 * The Enumerations returned by Hashtable's {@link #keys keys} and
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
    96
 * {@link #elements elements} methods are <em>not</em> fail-fast; if the
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
    97
 * Hashtable is structurally modified at any time after the enumeration is
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
    98
 * created then the results of enumerating are undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   103
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * implement the {@link Map} interface, making it a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47423
diff changeset
   110
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
64
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   111
 *
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   112
 * Java Collections Framework</a>.  Unlike the new collection
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   113
 * implementations, {@code Hashtable} is synchronized.  If a
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   114
 * thread-safe implementation is not needed, it is recommended to use
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   115
 * {@link HashMap} in place of {@code Hashtable}.  If a thread-safe
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   116
 * highly-concurrent implementation is desired, then it is recommended
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   117
 * to use {@link java.util.concurrent.ConcurrentHashMap} in place of
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 56
diff changeset
   118
 * {@code Hashtable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   120
 * @param <K> the type of keys maintained by this map
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   121
 * @param <V> the type of mapped values
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   122
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see     Object#equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see     Hashtable#rehash()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see     TreeMap
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24255
diff changeset
   133
 * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
public class Hashtable<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    extends Dictionary<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    implements Map<K,V>, Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * The hash table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   142
    private transient Entry<?,?>[] table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * The total number of entries in the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private transient int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * The table is rehashed when its size exceeds this threshold.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * value of this field is (int)(capacity * loadFactor).)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private int threshold;
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 load factor for the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * The number of times this Hashtable has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Structural modifications are those that change the number of entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * the Hashtable or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * the Hashtable fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52427
diff changeset
   174
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final long serialVersionUID = 1421746759512286392L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Constructs a new, empty hashtable with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * capacity and the specified load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param      initialCapacity   the initial capacity of the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param      loadFactor        the load factor of the hashtable.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   183
     * @throws     IllegalArgumentException  if the initial capacity is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *             than zero, or if the load factor is nonpositive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public Hashtable(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalArgumentException("Illegal Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (initialCapacity==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            initialCapacity = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.loadFactor = loadFactor;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   196
        table = new Entry<?,?>[initialCapacity];
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   197
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Constructs a new, empty hashtable with the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * and default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param     initialCapacity   the initial capacity of the hashtable.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   205
     * @throws    IllegalArgumentException if the initial capacity is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *              than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public Hashtable(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        this(initialCapacity, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Constructs a new, empty hashtable with a default initial capacity (11)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * and load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Hashtable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this(11, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Constructs a new hashtable with the same mappings as the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Map.  The hashtable is created with an initial capacity sufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * hold the mappings in the given Map and a default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param t the map whose mappings are to be placed in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws NullPointerException if the specified map is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public Hashtable(Map<? extends K, ? extends V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        this(Math.max(2*t.size(), 11), 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        putAll(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
38436
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   235
     * A constructor chained from {@link Properties} keeps Hashtable fields
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   236
     * uninitialized since they are not used.
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   237
     *
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   238
     * @param dummy a dummy parameter
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   239
     */
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   240
    Hashtable(Void dummy) {}
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   241
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   242
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns the number of keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return  the number of keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Tests if this hashtable maps no keys to values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   254
     * @return  {@code true} if this hashtable maps no keys to values;
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   255
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public synchronized boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return count == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns an enumeration of the keys in this hashtable.
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   263
     * Use the Enumeration methods on the returned object to fetch the keys
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   264
     * sequentially. If the hashtable is structurally modified while enumerating
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   265
     * over the keys then the results of enumerating are undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return  an enumeration of the keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see     Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @see     #elements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see     #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public synchronized Enumeration<K> keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return this.<K>getEnumeration(KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Returns an enumeration of the values in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Use the Enumeration methods on the returned object to fetch the elements
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   280
     * sequentially. If the hashtable is structurally modified while enumerating
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   281
     * over the values then the results of enumerating are undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return  an enumeration of the values in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @see     java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see     #keys()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @see     #values()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public synchronized Enumeration<V> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return this.<V>getEnumeration(VALUES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Tests if some key maps into the specified value in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * This operation is more expensive than the {@link #containsKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * containsKey} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <p>Note that this method is identical in functionality to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * {@link #containsValue containsValue}, (which is part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * {@link Map} interface in the collections framework).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param      value   a value to search for
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   303
     * @return     {@code true} if and only if some key maps to the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   304
     *             {@code value} argument in this hashtable as
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   305
     *             determined by the {@code equals} method;
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   306
     *             {@code false} otherwise.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   307
     * @throws     NullPointerException  if the value is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public synchronized boolean contains(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   314
        Entry<?,?> tab[] = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        for (int i = tab.length ; i-- > 0 ;) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   316
            for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (e.value.equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Returns true if this hashtable maps one or more keys to this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <p>Note that this method is identical in functionality to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * #contains contains} (which predates the {@link Map} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param value value whose presence in this hashtable is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   332
     * @return {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *         specified value
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   334
     * @throws NullPointerException  if the value is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return contains(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Tests if the specified object is a key in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param   key   possible key
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   345
     * @return  {@code true} if and only if the specified object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *          is a key in this hashtable, as determined by the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   347
     *          {@code equals} method; {@code false} otherwise.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   348
     * @throws  NullPointerException  if the key is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @see     #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public synchronized boolean containsKey(Object key) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   352
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   353
        int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int index = (hash & 0x7FFFFFFF) % tab.length;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   355
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * {@code k} to a value {@code v} such that {@code (key.equals(k))},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param key the key whose associated value is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the value to which the specified key is mapped, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *         {@code null} if this map contains no mapping for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see     #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   378
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public synchronized V get(Object key) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   380
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   381
        int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int index = (hash & 0x7FFFFFFF) % tab.length;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   383
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if ((e.hash == hash) && e.key.equals(key)) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   385
                return (V)e.value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   392
     * The maximum size of array to allocate.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   393
     * Some VMs reserve some header words in an array.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   394
     * Attempts to allocate larger arrays may result in
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   395
     * OutOfMemoryError: Requested array size exceeds VM limit
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   396
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   397
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   398
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   399
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Increases the capacity of and internally reorganizes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * hashtable, in order to accommodate and access its entries more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * efficiently.  This method is called automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * number of keys in the hashtable exceeds this hashtable's capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   406
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    protected void rehash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int oldCapacity = table.length;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   409
        Entry<?,?>[] oldMap = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   411
        // overflow-conscious code
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   412
        int newCapacity = (oldCapacity << 1) + 1;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   413
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   414
            if (oldCapacity == MAX_ARRAY_SIZE)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   415
                // Keep running with MAX_ARRAY_SIZE buckets
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   416
                return;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   417
            newCapacity = MAX_ARRAY_SIZE;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 715
diff changeset
   418
        }
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   419
        Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        modCount++;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   422
        threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        table = newMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        for (int i = oldCapacity ; i-- > 0 ;) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   426
            for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                Entry<K,V> e = old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                old = old.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   431
                e.next = (Entry<K,V>)newMap[index];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                newMap[index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   437
    private void addEntry(int hash, K key, V value, int index) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   438
        Entry<?,?> tab[] = table;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   439
        if (count >= threshold) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   440
            // Rehash the table if the threshold is exceeded
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   441
            rehash();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   442
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   443
            tab = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   444
            hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   445
            index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   446
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   447
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   448
        // Creates the new entry.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   449
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   450
        Entry<K,V> e = (Entry<K,V>) tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   451
        tab[index] = new Entry<>(hash, key, value, e);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   452
        count++;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   453
        modCount++;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   454
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   455
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   457
     * Maps the specified {@code key} to the specified
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   458
     * {@code value} in this hashtable. Neither the key nor the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   459
     * value can be {@code null}. <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   461
     * The value can be retrieved by calling the {@code get} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * with a key that is equal to the original key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param      key     the hashtable key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param      value   the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return     the previous value of the specified key in this hashtable,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   467
     *             or {@code null} if it did not have one
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   468
     * @throws     NullPointerException  if the key or value is
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   469
     *               {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see     #get(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public synchronized V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // Make sure the value is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // Makes sure the key is not already in the hashtable.
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   480
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   481
        int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        int index = (hash & 0x7FFFFFFF) % tab.length;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   483
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   484
        Entry<K,V> entry = (Entry<K,V>)tab[index];
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   485
        for(; entry != null ; entry = entry.next) {
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   486
            if ((entry.hash == hash) && entry.key.equals(key)) {
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   487
                V old = entry.value;
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   488
                entry.value = value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   493
        addEntry(hash, key, value, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Removes the key (and its corresponding value) from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * hashtable. This method does nothing if the key is not in the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param   key   the key that needs to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return  the value to which the key had been mapped in this hashtable,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   503
     *          or {@code null} if the key did not have a mapping
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   504
     * @throws  NullPointerException  if the key is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public synchronized V remove(Object key) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   507
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   508
        int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        int index = (hash & 0x7FFFFFFF) % tab.length;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   510
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   511
        Entry<K,V> e = (Entry<K,V>)tab[index];
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   512
        for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   519
                modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                V oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Copies all of the mappings from the specified map to this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * These mappings will replace any mappings that this hashtable had for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @param t mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public synchronized void putAll(Map<? extends K, ? extends V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        for (Map.Entry<? extends K, ? extends V> e : t.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            put(e.getKey(), e.getValue());
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
     * Clears this hashtable so that it contains no keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public synchronized void clear() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   547
        Entry<?,?> tab[] = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        for (int index = tab.length; --index >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            tab[index] = null;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   550
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Creates a shallow copy of this hashtable. All the structure of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * hashtable itself is copied, but the keys and values are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * This is a relatively expensive operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @return  a clone of the hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public synchronized Object clone() {
38436
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   562
        Hashtable<?,?> t = cloneHashtable();
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   563
        t.table = new Entry<?,?>[table.length];
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   564
        for (int i = table.length ; i-- > 0 ; ) {
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   565
            t.table[i] = (table[i] != null)
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   566
                ? (Entry<?,?>) table[i].clone() : null;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   567
        }
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   568
        t.keySet = null;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   569
        t.entrySet = null;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   570
        t.values = null;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   571
        t.modCount = 0;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   572
        return t;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   573
    }
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   574
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   575
    /** Calls super.clone() */
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   576
    final Hashtable<?,?> cloneHashtable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        try {
38436
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
   578
            return (Hashtable<?,?>)super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   581
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   586
     * Returns a string representation of this {@code Hashtable} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * in the form of a set of entries, enclosed in braces and separated
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   588
     * by the ASCII characters "<code> ,&nbsp;</code>" (comma and space). Each
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   589
     * entry is rendered as the key, an equals sign {@code =}, and the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   590
     * associated element, where the {@code toString} method is used to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * convert the key and element to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return  a string representation of this hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int max = size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (max == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return "{}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        Iterator<Map.Entry<K,V>> it = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        sb.append('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            Map.Entry<K,V> e = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            sb.append(key   == this ? "(this Map)" : key.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            sb.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            sb.append(value == this ? "(this Map)" : value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (i == max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                return sb.append('}').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    private <T> Enumeration<T> getEnumeration(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return Collections.emptyEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } else {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   623
            return new Enumerator<>(type, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private <T> Iterator<T> getIterator(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            return Collections.emptyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        } else {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   631
            return new Enumerator<>(type, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Each of these fields are initialized to contain an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * appropriate view the first time this view is requested.  The views are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * stateless, so there's no reason to create more than one of each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   642
    private transient volatile Set<K> keySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   643
    private transient volatile Set<Map.Entry<K,V>> entrySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   644
    private transient volatile Collection<V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   651
     * the iterator's own {@code remove} operation), the results of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * which removes the corresponding mapping from the map, via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   654
     * {@code Iterator.remove}, {@code Set.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   655
     * {@code removeAll}, {@code retainAll}, and {@code clear}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   656
     * operations.  It does not support the {@code add} or {@code addAll}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (keySet == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            keySet = Collections.synchronizedSet(new KeySet(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return getIterator(KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            return Hashtable.this.remove(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            Hashtable.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   690
     * the iterator's own {@code remove} operation, or through the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   691
     * {@code setValue} operation on a map entry returned by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   694
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   695
     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   696
     * {@code clear} operations.  It does not support the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   697
     * {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            entrySet = Collections.synchronizedSet(new EntrySet(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return getIterator(ENTRIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        public boolean add(Map.Entry<K,V> o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return super.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   719
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            Object key = entry.getKey();
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   721
            Entry<?,?>[] tab = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   722
            int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   725
            for (Entry<?,?> e = tab[index]; e != null; e = e.next)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if (e.hash==hash && e.equals(entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   734
            Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   735
            Object key = entry.getKey();
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   736
            Entry<?,?>[] tab = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   737
            int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   740
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   741
            Entry<K,V> e = (Entry<K,V>)tab[index];
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   742
            for(Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                if (e.hash==hash && e.equals(entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    if (prev != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   749
                    e.value = null; // clear for gc.
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   750
                    modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return false;
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 int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return count;
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 void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            Hashtable.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * modified while an iteration over the collection is in progress
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   772
     * (except through the iterator's own {@code remove} operation),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   775
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   776
     * {@code Collection.remove}, {@code removeAll},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   777
     * {@code retainAll} and {@code clear} operations.  It does not
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   778
     * support the {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            values = Collections.synchronizedCollection(new ValueCollection(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                                        this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private class ValueCollection extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            return getIterator(VALUES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            Hashtable.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Compares the specified Object with this Map for equality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * as per the definition in the Map interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param  o object to be compared for equality with this hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @return true if the specified Object is equal to this Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @see Map#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public synchronized boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        if (!(o instanceof Map))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   821
        Map<?,?> t = (Map<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (t.size() != size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        try {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
   826
            for (Map.Entry<K, V> e : entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                if (value == null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
   830
                    if (!(t.get(key) == null && t.containsKey(key)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    if (!value.equals(t.get(key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        } catch (ClassCastException unused)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        } catch (NullPointerException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * Returns the hash code value for this Map as per the definition in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Map interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @see Map#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public synchronized int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * This code detects the recursion caused by computing the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         * of a self-referential hash table and prevents the stack overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
         * that would otherwise result.  This allows certain 1.1-era
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
         * applets with self-referential hash tables to work.  This code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * abuses the loadFactor field to do double-duty as a hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         * in progress flag, so as not to worsen the space performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         * A negative load factor indicates that hash code computation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (count == 0 || loadFactor < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            return h;  // Returns zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        loadFactor = -loadFactor;  // Mark hashCode computation in progress
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   869
        Entry<?,?>[] tab = table;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   870
        for (Entry<?,?> entry : tab) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   871
            while (entry != null) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   872
                h += entry.hashCode();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   873
                entry = entry.next;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   874
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   875
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   876
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        loadFactor = -loadFactor;  // Mark hashCode computation complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   882
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   883
    public synchronized V getOrDefault(Object key, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   884
        V result = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   885
        return (null == result) ? defaultValue : result;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   886
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   887
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18280
diff changeset
   888
    @SuppressWarnings("unchecked")
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   889
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   890
    public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   891
        Objects.requireNonNull(action);     // explicit check required in case
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   892
                                            // table is empty.
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   893
        final int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   894
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   895
        Entry<?, ?>[] tab = table;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   896
        for (Entry<?, ?> entry : tab) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   897
            while (entry != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   898
                action.accept((K)entry.key, (V)entry.value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   899
                entry = entry.next;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   900
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   901
                if (expectedModCount != modCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   902
                    throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   903
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   904
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   905
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   906
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   907
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18280
diff changeset
   908
    @SuppressWarnings("unchecked")
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   909
    @Override
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   910
    public synchronized 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
   911
        Objects.requireNonNull(function);     // explicit check required in case
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   912
                                              // table is empty.
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   913
        final int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   914
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   915
        Entry<K, V>[] tab = (Entry<K, V>[])table;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   916
        for (Entry<K, V> entry : tab) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   917
            while (entry != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   918
                entry.value = Objects.requireNonNull(
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   919
                    function.apply(entry.key, entry.value));
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   920
                entry = entry.next;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   921
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   922
                if (expectedModCount != modCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   923
                    throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   924
                }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   925
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   926
        }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   927
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   928
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   929
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   930
    public synchronized V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   931
        Objects.requireNonNull(value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   932
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   933
        // Makes sure the key is not already in the hashtable.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   934
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   935
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   936
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   937
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   938
        Entry<K,V> entry = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   939
        for (; entry != null; entry = entry.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   940
            if ((entry.hash == hash) && entry.key.equals(key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   941
                V old = entry.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   942
                if (old == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   943
                    entry.value = value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   944
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   945
                return old;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   946
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   947
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   948
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   949
        addEntry(hash, key, value, index);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   950
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   951
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   952
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   953
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   954
    public synchronized boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   955
        Objects.requireNonNull(value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   956
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   957
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   958
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   959
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   960
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   961
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   962
        for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   963
            if ((e.hash == hash) && e.key.equals(key) && e.value.equals(value)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   964
                if (prev != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   965
                    prev.next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   966
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   967
                    tab[index] = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   968
                }
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   969
                e.value = null; // clear for gc
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
   970
                modCount++;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   971
                count--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   972
                return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   973
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   974
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   975
        return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   976
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   977
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   978
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   979
    public synchronized boolean replace(K key, V oldValue, V newValue) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19853
diff changeset
   980
        Objects.requireNonNull(oldValue);
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19853
diff changeset
   981
        Objects.requireNonNull(newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   982
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
   983
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   984
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   985
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   986
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   987
        for (; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   988
            if ((e.hash == hash) && e.key.equals(key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   989
                if (e.value.equals(oldValue)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   990
                    e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   991
                    return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   992
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   993
                    return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   994
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   995
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   996
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   997
        return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   998
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   999
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1000
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1001
    public synchronized V replace(K key, V value) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19853
diff changeset
  1002
        Objects.requireNonNull(value);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1003
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1004
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1005
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1006
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1007
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1008
        for (; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1009
            if ((e.hash == hash) && e.key.equals(key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1010
                V oldValue = e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1011
                e.value = value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1012
                return oldValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1013
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1014
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1015
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1016
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1017
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1018
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1019
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1020
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1021
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1022
     * {@link java.util.ConcurrentModificationException} if the mapping
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1023
     * function modified this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1024
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1025
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1026
     * mapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1027
     */
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1028
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1029
    public synchronized V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1030
        Objects.requireNonNull(mappingFunction);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1031
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1032
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1033
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1034
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1035
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1036
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1037
        for (; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1038
            if (e.hash == hash && e.key.equals(key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1039
                // Hashtable not accept null value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1040
                return e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1041
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1042
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1043
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1044
        int mc = modCount;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1045
        V newValue = mappingFunction.apply(key);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1046
        if (mc != modCount) { throw new ConcurrentModificationException(); }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1047
        if (newValue != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1048
            addEntry(hash, key, newValue, index);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1049
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1050
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1051
        return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1052
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1053
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1054
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1055
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1056
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1057
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1058
     * {@link java.util.ConcurrentModificationException} if the remapping
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1059
     * function modified this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1060
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1061
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1062
     * remapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1063
     */
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1064
    @Override
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1065
    public synchronized V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1066
        Objects.requireNonNull(remappingFunction);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1067
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1068
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1069
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1070
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1071
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1072
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1073
        for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1074
            if (e.hash == hash && e.key.equals(key)) {
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1075
                int mc = modCount;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1076
                V newValue = remappingFunction.apply(key, e.value);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1077
                if (mc != modCount) {
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1078
                    throw new ConcurrentModificationException();
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1079
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1080
                if (newValue == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1081
                    if (prev != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1082
                        prev.next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1083
                    } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1084
                        tab[index] = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1085
                    }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1086
                    modCount = mc + 1;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1087
                    count--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1088
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1089
                    e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1090
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1091
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1092
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1093
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1094
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1095
    }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1096
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1097
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1098
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1099
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1100
     * {@link java.util.ConcurrentModificationException} if the remapping
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1101
     * function modified this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1102
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1103
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1104
     * remapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1105
     */
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1106
    @Override
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1107
    public synchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1108
        Objects.requireNonNull(remappingFunction);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1109
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1110
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1111
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1112
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1113
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1114
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1115
        for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1116
            if (e.hash == hash && Objects.equals(e.key, key)) {
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1117
                int mc = modCount;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1118
                V newValue = remappingFunction.apply(key, e.value);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1119
                if (mc != modCount) {
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1120
                    throw new ConcurrentModificationException();
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1121
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1122
                if (newValue == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1123
                    if (prev != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1124
                        prev.next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1125
                    } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1126
                        tab[index] = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1127
                    }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1128
                    modCount = mc + 1;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1129
                    count--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1130
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1131
                    e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1132
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1133
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1134
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1135
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1136
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1137
        int mc = modCount;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1138
        V newValue = remappingFunction.apply(key, null);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1139
        if (mc != modCount) { throw new ConcurrentModificationException(); }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1140
        if (newValue != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1141
            addEntry(hash, key, newValue, index);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1142
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1143
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1144
        return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1145
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1146
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1147
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1148
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1149
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1150
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1151
     * {@link java.util.ConcurrentModificationException} if the remapping
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1152
     * function modified this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1153
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1154
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1155
     * remapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1156
     */
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1157
    @Override
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
  1158
    public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1159
        Objects.requireNonNull(remappingFunction);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1160
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1161
        Entry<?,?> tab[] = table;
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1162
        int hash = key.hashCode();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1163
        int index = (hash & 0x7FFFFFFF) % tab.length;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1164
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1165
        Entry<K,V> e = (Entry<K,V>)tab[index];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1166
        for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1167
            if (e.hash == hash && e.key.equals(key)) {
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1168
                int mc = modCount;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1169
                V newValue = remappingFunction.apply(e.value, value);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1170
                if (mc != modCount) {
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1171
                    throw new ConcurrentModificationException();
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1172
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1173
                if (newValue == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1174
                    if (prev != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1175
                        prev.next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1176
                    } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1177
                        tab[index] = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1178
                    }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 28559
diff changeset
  1179
                    modCount = mc + 1;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1180
                    count--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1181
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1182
                    e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1183
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1184
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1185
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1186
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1187
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1188
        if (value != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1189
            addEntry(hash, key, value, index);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1190
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1191
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1192
        return value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1193
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1194
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Save the state of the Hashtable to a stream (i.e., serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @serialData The <i>capacity</i> of the Hashtable (the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *             bucket array) is emitted (int), followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *             <i>size</i> of the Hashtable (the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     *             for each key-value mapping represented by the Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *             The key-value mappings are emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52427
diff changeset
  1205
    @java.io.Serial
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1206
    private void writeObject(java.io.ObjectOutputStream s)
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1207
            throws IOException {
38436
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1208
        writeHashtable(s);
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1209
    }
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1210
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1211
    /**
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1212
     * Perform serialization of the Hashtable to an ObjectOutputStream.
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1213
     * The Properties class overrides this method.
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1214
     */
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1215
    void writeHashtable(java.io.ObjectOutputStream s)
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1216
            throws IOException {
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1217
        Entry<Object, Object> entryStack = null;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1218
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1219
        synchronized (this) {
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1220
            // Write out the threshold and loadFactor
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1221
            s.defaultWriteObject();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1222
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1223
            // Write out the length and count of elements
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1224
            s.writeInt(table.length);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1225
            s.writeInt(count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1227
            // Stack copies of the entries in the table
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
  1228
            for (Entry<?, ?> entry : table) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1230
                while (entry != null) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1231
                    entryStack =
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1232
                        new Entry<>(0, entry.key, entry.value, entryStack);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1233
                    entry = entry.next;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1234
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1237
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1238
        // Write out the key/value objects from the stacked entries
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1239
        while (entryStack != null) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1240
            s.writeObject(entryStack.key);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1241
            s.writeObject(entryStack.value);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1242
            entryStack = entryStack.next;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents: 7816
diff changeset
  1243
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /**
38436
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1247
     * Called by Properties to write out a simulated threshold and loadfactor.
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1248
     */
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1249
    final void defaultWriteHashtable(java.io.ObjectOutputStream s, int length,
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1250
            float loadFactor) throws IOException {
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1251
        this.threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1252
        this.loadFactor = loadFactor;
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1253
        s.defaultWriteObject();
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1254
    }
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1255
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1256
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * Reconstitute the Hashtable from a stream (i.e., deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52427
diff changeset
  1259
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    private void readObject(java.io.ObjectInputStream s)
38436
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1261
            throws IOException, ClassNotFoundException {
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1262
        readHashtable(s);
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1263
    }
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1264
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1265
    /**
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1266
     * Perform deserialization of the Hashtable from an ObjectInputStream.
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1267
     * The Properties class overrides this method.
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1268
     */
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1269
    void readHashtable(java.io.ObjectInputStream s)
4676f6c9ebee 8029891: Deadlock detected in java/lang/ClassLoader/deadlock/GetResource.java
bchristi
parents: 32108
diff changeset
  1270
            throws IOException, ClassNotFoundException {
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1271
        // Read in the threshold and loadFactor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1274
        // Validate loadFactor (ignore threshold - it will be re-computed)
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1275
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1276
            throw new StreamCorruptedException("Illegal Load: " + loadFactor);
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1277
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        // Read the original length of the array and number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        int origlength = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        int elements = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1282
        // Validate # of elements
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1283
        if (elements < 0)
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1284
            throw new StreamCorruptedException("Illegal # of Elements: " + elements);
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1285
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1286
        // Clamp original length to be more than elements / loadFactor
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1287
        // (this is the invariant enforced with auto-growth)
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1288
        origlength = Math.max(origlength, (int)(elements / loadFactor) + 1);
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1289
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1290
        // Compute new length with a bit of room 5% + 3 to grow but
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1291
        // no larger than the clamped original length.  Make the length
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        // odd if it's large enough, this helps distribute the entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        // Guard against the length ending up zero, that's not valid.
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1294
        int length = (int)((elements + elements / 20) / loadFactor) + 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        if (length > elements && (length & 1) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            length--;
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1297
        length = Math.min(length, origlength);
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1298
49774
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1299
        if (length < 0) { // overflow
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1300
            length = origlength;
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1301
        }
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1302
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1303
        // Check Map.Entry[].class since it's the nearest public type to
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1304
        // what we're actually creating.
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1305
        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Map.Entry[].class, length);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1306
        table = new Entry<?,?>[length];
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1307
        threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        // Read the number of elements and then all the key/value objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        for (; elements > 0; elements--) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1312
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1313
                K key = (K)s.readObject();
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1314
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1315
                V value = (V)s.readObject();
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1316
            // sync is eliminated for performance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            reconstitutionPut(table, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * The put method used by readObject. This is provided because put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * is overridable and should not be called in readObject since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * subclass will not yet be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * <p>This differs from the regular put method in several ways. No
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * checking for rehashing is necessary since the number of elements
28559
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1328
     * initially in the table is known. The modCount is not incremented and
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1329
     * there's no synchronization because we are creating a new instance.
e4d6d177eef0 8068427: Hashtable deserialization reconstitutes table with wrong capacity
plevart
parents: 25859
diff changeset
  1330
     * Also, no return value is needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1332
    private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        throws StreamCorruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        // Makes sure the key is not already in the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        // This should not happen in deserialized version.
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1340
        int hash = key.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        int index = (hash & 0x7FFFFFFF) % tab.length;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1342
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        // Creates the new entry.
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1348
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1349
            Entry<K,V> e = (Entry<K,V>)tab[index];
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1350
        tab[index] = new Entry<>(hash, key, value, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1355
     * Hashtable bucket collision list entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    private static class Entry<K,V> implements Map.Entry<K,V> {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1358
        final int hash;
13018
92c86cea72a8 7173919: Minor optimization of hashing methods
mduigou
parents: 12859
diff changeset
  1359
        final K key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        Entry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        protected Entry(int hash, K key, V value, Entry<K,V> next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            this.hash = hash;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1365
            this.key =  key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1370
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        protected Object clone() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1372
            return new Entry<>(hash, key, value,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                                  (next==null ? null : (Entry<K,V>) next.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        // Map.Entry Ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            V oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1398
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
               (value==null ? e.getValue()==null : value.equals(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        public int hashCode() {
19853
832b09e2714c 8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents: 19208
diff changeset
  1405
            return hash ^ Objects.hashCode(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            return key.toString()+"="+value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    // Types of Enumerations/Iterations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    private static final int KEYS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    private static final int VALUES = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    private static final int ENTRIES = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * A hashtable enumerator class.  This class implements both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * Enumeration and Iterator interfaces, but individual instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * can be created with the Iterator methods disabled.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * to avoid unintentionally increasing the capabilities granted a user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * by passing an Enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1426
        final Entry<?,?>[] table = Hashtable.this.table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        int index = table.length;
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
  1428
        Entry<?,?> entry;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
  1429
        Entry<?,?> lastReturned;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1430
        final int type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
         * Indicates whether this Enumerator is serving as an Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
         * or an Enumeration.  (true -> Iterator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
         */
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1436
        final boolean iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
         * Hashtable should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
         */
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1443
        protected int expectedModCount = Hashtable.this.modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        Enumerator(int type, boolean iterator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            this.iterator = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        public boolean hasMoreElements() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1451
            Entry<?,?> e = entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            int i = index;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1453
            Entry<?,?>[] t = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            /* Use locals for faster loop iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            while (e == null && i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                e = t[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            entry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            return e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1463
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        public T nextElement() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1465
            Entry<?,?> et = entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            int i = index;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1467
            Entry<?,?>[] t = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            /* Use locals for faster loop iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            while (et == null && i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                et = t[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            entry = et;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            if (et != null) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1475
                Entry<?,?> e = lastReturned = entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                entry = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            throw new NoSuchElementException("Hashtable Enumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        // Iterator methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            return hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        public T next() {
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1488
            if (Hashtable.this.modCount != expectedModCount)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            return nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            if (!iterator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                throw new IllegalStateException("Hashtable Enumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            synchronized(Hashtable.this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1502
                Entry<?,?>[] tab = Hashtable.this.table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1505
                @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1506
                Entry<K,V> e = (Entry<K,V>)tab[index];
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1507
                for(Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                    if (e == lastReturned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                        if (prev == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                            tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                            prev.next = e.next;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1513
                        expectedModCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                        lastReturned = null;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1515
                        Hashtable.this.modCount++;
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 23746
diff changeset
  1516
                        Hashtable.this.count--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
}