jdk/src/share/classes/java/util/Hashtable.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 56 48451b4616e8
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This class implements a hashtable, which maps keys to values. Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * non-<code>null</code> object can be used as a key or as a value. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * To successfully store and retrieve objects from a hashtable, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * objects used as keys must implement the <code>hashCode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * method and the <code>equals</code> method. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An instance of <code>Hashtable</code> has two parameters that affect its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <i>initial capacity</i> is simply the capacity at the time the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is created.  Note that the hash table is <i>open</i>: in the case of a "hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * collision", a single bucket stores multiple entries, which must be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * sequentially.  The <i>load factor</i> is a measure of how full the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * table is allowed to get before its capacity is automatically increased.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The initial capacity and load factor parameters are merely hints to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the implementation.  The exact details as to when and whether the rehash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * method is invoked are implementation-dependent.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Generally, the default load factor (.75) offers a good tradeoff between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * time and space costs.  Higher values decrease the space overhead but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * increase the time cost to look up an entry (which is reflected in most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <tt>Hashtable</tt> operations, including <tt>get</tt> and <tt>put</tt>).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The initial capacity controls a tradeoff between wasted space and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * need for <code>rehash</code> operations, which are time-consuming.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * No <code>rehash</code> operations will <i>ever</i> occur if the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * capacity is greater than the maximum number of entries the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tt>Hashtable</tt> will contain divided by its load factor.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * setting the initial capacity too high can waste space.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * If many entries are to be made into a <code>Hashtable</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * creating it with a sufficiently large capacity may allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * entries to be inserted more efficiently than letting it perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * automatic rehashing as needed to grow the table. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * This example creates a hashtable of numbers. It uses the names of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * the numbers as keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <pre>   {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   Hashtable<String, Integer> numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     = new Hashtable<String, Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   numbers.put("one", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   numbers.put("two", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   numbers.put("three", 3);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>To retrieve a number, use the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <pre>   {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   Integer n = numbers.get("two");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     System.out.println("two = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>The iterators returned by the <tt>iterator</tt> method of the collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * returned by all of this class's "collection view methods" are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <em>fail-fast</em>: if the Hashtable is structurally modified at any time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * after the iterator is created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <tt>remove</tt> method, the iterator will throw a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * arbitrary, non-deterministic behavior at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The Enumerations returned by Hashtable's keys and elements methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <em>not</em> fail-fast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * implement the {@link Map} interface, making it a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Collections Framework</a>.  Unlike the new collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * implementations, {@code Hashtable} is synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @see     Object#equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @see     Hashtable#rehash()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
public class Hashtable<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    extends Dictionary<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    implements Map<K,V>, Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The hash table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private transient Entry[] table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The total number of entries in the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private transient int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The table is rehashed when its size exceeds this threshold.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * value of this field is (int)(capacity * loadFactor).)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The load factor for the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The number of times this Hashtable has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Structural modifications are those that change the number of entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * the Hashtable or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * the Hashtable fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final long serialVersionUID = 1421746759512286392L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Constructs a new, empty hashtable with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * capacity and the specified load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param      initialCapacity   the initial capacity of the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param      loadFactor        the load factor of the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @exception  IllegalArgumentException  if the initial capacity is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *             than zero, or if the load factor is nonpositive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public Hashtable(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new IllegalArgumentException("Illegal Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (initialCapacity==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            initialCapacity = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.loadFactor = loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        table = new Entry[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        threshold = (int)(initialCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Constructs a new, empty hashtable with the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * and default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param     initialCapacity   the initial capacity of the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception IllegalArgumentException if the initial capacity is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *              than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Hashtable(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this(initialCapacity, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Constructs a new, empty hashtable with a default initial capacity (11)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * and load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Hashtable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        this(11, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Constructs a new hashtable with the same mappings as the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Map.  The hashtable is created with an initial capacity sufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * hold the mappings in the given Map and a default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param t the map whose mappings are to be placed in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws NullPointerException if the specified map is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public Hashtable(Map<? extends K, ? extends V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this(Math.max(2*t.size(), 11), 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        putAll(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Returns the number of keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return  the number of keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Tests if this hashtable maps no keys to values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return  <code>true</code> if this hashtable maps no keys to values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public synchronized boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return count == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns an enumeration of the keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return  an enumeration of the keys in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see     Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see     #elements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see     #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public synchronized Enumeration<K> keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return this.<K>getEnumeration(KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Returns an enumeration of the values in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Use the Enumeration methods on the returned object to fetch the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return  an enumeration of the values in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see     java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @see     #keys()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see     #values()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public synchronized Enumeration<V> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return this.<V>getEnumeration(VALUES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Tests if some key maps into the specified value in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * This operation is more expensive than the {@link #containsKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * containsKey} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p>Note that this method is identical in functionality to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * {@link #containsValue containsValue}, (which is part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * {@link Map} interface in the collections framework).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param      value   a value to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return     <code>true</code> if and only if some key maps to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *             <code>value</code> argument in this hashtable as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *             determined by the <tt>equals</tt> method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *             <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception  NullPointerException  if the value is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public synchronized boolean contains(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        for (int i = tab.length ; i-- > 0 ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if (e.value.equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns true if this hashtable maps one or more keys to this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <p>Note that this method is identical in functionality to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * #contains contains} (which predates the {@link Map} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param value value whose presence in this hashtable is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @throws NullPointerException  if the value is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return contains(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Tests if the specified object is a key in this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return  <code>true</code> if and only if the specified object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *          is a key in this hashtable, as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          <tt>equals</tt> method; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws  NullPointerException  if the key is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see     #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public synchronized boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * {@code k} to a value {@code v} such that {@code (key.equals(k))},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param key the key whose associated value is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return the value to which the specified key is mapped, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *         {@code null} if this map contains no mapping for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see     #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public synchronized V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Increases the capacity of and internally reorganizes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * hashtable, in order to accommodate and access its entries more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * efficiently.  This method is called automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * number of keys in the hashtable exceeds this hashtable's capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    protected void rehash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int oldCapacity = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        Entry[] oldMap = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int newCapacity = oldCapacity * 2 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Entry[] newMap = new Entry[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        threshold = (int)(newCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        table = newMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        for (int i = oldCapacity ; i-- > 0 ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (Entry<K,V> old = oldMap[i] ; old != null ; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                Entry<K,V> e = old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                old = old.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                e.next = newMap[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                newMap[index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Maps the specified <code>key</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>value</code> in this hashtable. Neither the key nor the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * value can be <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * The value can be retrieved by calling the <code>get</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * with a key that is equal to the original key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param      key     the hashtable key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param      value   the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @return     the previous value of the specified key in this hashtable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *             or <code>null</code> if it did not have one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @exception  NullPointerException  if the key or value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *               <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see     #get(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public synchronized V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Make sure the value is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // Makes sure the key is not already in the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                V old = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (count >= threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // Rehash the table if the threshold is exceeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            rehash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // Creates the new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        Entry<K,V> e = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        tab[index] = new Entry<K,V>(hash, key, value, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Removes the key (and its corresponding value) from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * hashtable. This method does nothing if the key is not in the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param   key   the key that needs to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return  the value to which the key had been mapped in this hashtable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *          or <code>null</code> if the key did not have a mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @throws  NullPointerException  if the key is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public synchronized V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                V oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Copies all of the mappings from the specified map to this hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * These mappings will replace any mappings that this hashtable had for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param t mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public synchronized void putAll(Map<? extends K, ? extends V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        for (Map.Entry<? extends K, ? extends V> e : t.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Clears this hashtable so that it contains no keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public synchronized void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        Entry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        for (int index = tab.length; --index >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            tab[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        count = 0;
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
     * Creates a shallow copy of this hashtable. All the structure of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * hashtable itself is copied, but the keys and values are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * This is a relatively expensive operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return  a clone of the hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public synchronized Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            Hashtable<K,V> t = (Hashtable<K,V>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            t.table = new Entry[table.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            for (int i = table.length ; i-- > 0 ; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                t.table[i] = (table[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    ? (Entry<K,V>) table[i].clone() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            t.keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            t.entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            t.values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            t.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Returns a string representation of this <tt>Hashtable</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * in the form of a set of entries, enclosed in braces and separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * by the ASCII characters "<tt>,&nbsp;</tt>" (comma and space). Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * entry is rendered as the key, an equals sign <tt>=</tt>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * associated element, where the <tt>toString</tt> method is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * convert the key and element to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return  a string representation of this hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int max = size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (max == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return "{}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        Iterator<Map.Entry<K,V>> it = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        sb.append('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            Map.Entry<K,V> e = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            sb.append(key   == this ? "(this Map)" : key.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            sb.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            sb.append(value == this ? "(this Map)" : value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (i == max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                return sb.append('}').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private <T> Enumeration<T> getEnumeration(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return Collections.emptyEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            return new Enumerator<T>(type, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    private <T> Iterator<T> getIterator(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return Collections.emptyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return new Enumerator<T>(type, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Each of these fields are initialized to contain an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * appropriate view the first time this view is requested.  The views are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * stateless, so there's no reason to create more than one of each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private transient volatile Set<K> keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private transient volatile Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private transient volatile Collection<V> values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * the iterator's own <tt>remove</tt> operation), the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * which removes the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (keySet == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            keySet = Collections.synchronizedSet(new KeySet(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            return getIterator(KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return Hashtable.this.remove(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            Hashtable.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * the iterator's own <tt>remove</tt> operation, or through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <tt>setValue</tt> operation on a map entry returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <tt>clear</tt> operations.  It does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            entrySet = Collections.synchronizedSet(new EntrySet(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            return getIterator(ENTRIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        public boolean add(Map.Entry<K,V> o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return super.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            Map.Entry entry = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            Object key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            for (Entry e = tab[index]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (e.hash==hash && e.equals(entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            K key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            for (Entry<K,V> e = tab[index], prev = null; e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                 prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                if (e.hash==hash && e.equals(entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    if (prev != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            Hashtable.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * modified while an iteration over the collection is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * (except through the iterator's own <tt>remove</tt> operation),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            values = Collections.synchronizedCollection(new ValueCollection(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                                        this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    private class ValueCollection extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return getIterator(VALUES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            Hashtable.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * Compares the specified Object with this Map for equality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * as per the definition in the Map interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @param  o object to be compared for equality with this hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @return true if the specified Object is equal to this Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @see Map#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public synchronized boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (!(o instanceof Map))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        Map<K,V> t = (Map<K,V>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (t.size() != size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            Iterator<Map.Entry<K,V>> i = entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                Map.Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                K key = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                V value = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    if (!(t.get(key)==null && t.containsKey(key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    if (!value.equals(t.get(key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } catch (ClassCastException unused)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        } catch (NullPointerException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Returns the hash code value for this Map as per the definition in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Map interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @see Map#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public synchronized int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         * This code detects the recursion caused by computing the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * of a self-referential hash table and prevents the stack overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * that would otherwise result.  This allows certain 1.1-era
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * applets with self-referential hash tables to work.  This code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         * abuses the loadFactor field to do double-duty as a hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * in progress flag, so as not to worsen the space performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         * A negative load factor indicates that hash code computation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (count == 0 || loadFactor < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return h;  // Returns zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        loadFactor = -loadFactor;  // Mark hashCode computation in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        for (int i = 0; i < tab.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            for (Entry e = tab[i]; e != null; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                h += e.key.hashCode() ^ e.value.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        loadFactor = -loadFactor;  // Mark hashCode computation complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Save the state of the Hashtable to a stream (i.e., serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @serialData The <i>capacity</i> of the Hashtable (the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *             bucket array) is emitted (int), followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *             <i>size</i> of the Hashtable (the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *             for each key-value mapping represented by the Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *             The key-value mappings are emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        // Write out the length, threshold, loadfactor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // Write out length, count of elements and then the key/value objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        s.writeInt(table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        s.writeInt(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        for (int index = table.length-1; index >= 0; index--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            Entry entry = table[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            while (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                s.writeObject(entry.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                s.writeObject(entry.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Reconstitute the Hashtable from a stream (i.e., deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        // Read in the length, threshold, and loadfactor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        // Read the original length of the array and number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        int origlength = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        int elements = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        // Compute new size with a bit of room 5% to grow but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        // no larger than the original size.  Make the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        // odd if it's large enough, this helps distribute the entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // Guard against the length ending up zero, that's not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int length = (int)(elements * loadFactor) + (elements / 20) + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (length > elements && (length & 1) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            length--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (origlength > 0 && length > origlength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            length = origlength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        Entry[] table = new Entry[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        // Read the number of elements and then all the key/value objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        for (; elements > 0; elements--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            K key = (K)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            V value = (V)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // synch could be eliminated for performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            reconstitutionPut(table, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        this.table = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * The put method used by readObject. This is provided because put
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * is overridable and should not be called in readObject since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * subclass will not yet be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <p>This differs from the regular put method in several ways. No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * checking for rehashing is necessary since the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * initially in the table is known. The modCount is not incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * because we are creating a new instance. Also, no return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    private void reconstitutionPut(Entry[] tab, K key, V value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        throws StreamCorruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        // Makes sure the key is not already in the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        // This should not happen in deserialized version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // Creates the new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        Entry<K,V> e = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        tab[index] = new Entry<K,V>(hash, key, value, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Hashtable collision list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    private static class Entry<K,V> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        Entry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        protected Entry(int hash, K key, V value, Entry<K,V> next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            this.hash = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        protected Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            return new Entry<K,V>(hash, key, value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                                  (next==null ? null : (Entry<K,V>) next.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        // Map.Entry Ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            V oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            Map.Entry e = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
               (value==null ? e.getValue()==null : value.equals(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            return hash ^ (value==null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return key.toString()+"="+value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    // Types of Enumerations/Iterations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    private static final int KEYS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    private static final int VALUES = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    private static final int ENTRIES = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * A hashtable enumerator class.  This class implements both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Enumeration and Iterator interfaces, but individual instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * can be created with the Iterator methods disabled.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * to avoid unintentionally increasing the capabilities granted a user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * by passing an Enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        Entry[] table = Hashtable.this.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        int index = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        Entry<K,V> entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        Entry<K,V> lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * Indicates whether this Enumerator is serving as an Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         * or an Enumeration.  (true -> Iterator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        boolean iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * Hashtable should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        protected int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        Enumerator(int type, boolean iterator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            this.iterator = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            Entry<K,V> e = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            int i = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            Entry[] t = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            /* Use locals for faster loop iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            while (e == null && i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                e = t[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            entry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        public T nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            Entry<K,V> et = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            int i = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            Entry[] t = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            /* Use locals for faster loop iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            while (et == null && i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                et = t[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            entry = et;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            if (et != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                Entry<K,V> e = lastReturned = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                entry = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            throw new NoSuchElementException("Hashtable Enumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        // Iterator methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        public T next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            return nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if (!iterator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                throw new IllegalStateException("Hashtable Enumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            synchronized(Hashtable.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                Entry[] tab = Hashtable.this.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                for (Entry<K,V> e = tab[index], prev = null; e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                     prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    if (e == lastReturned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        expectedModCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        if (prev == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                            tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                            prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                        count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                        lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
}