corba/src/share/classes/com/sun/corba/se/impl/util/IdentityHashtable.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
child 14348 6175970f2b3a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 * Copyright IBM Corp. 1998 1999  All Rights Reserved
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
package com.sun.corba.se.impl.util;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import java.util.Dictionary;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.util.Enumeration;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import java.util.NoSuchElementException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
 * IdentityHashtable is a modified copy of the 1.1.6 Hashtable class which
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
 * does not rely on the hashCode() and equals() methods of the key or value;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
 * instead, it uses the System.identityHashcode() method and pointer comparison.
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
 * In addition, all synchronization has been removed.
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
 * IdentityHashtable collision list.
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
class IdentityHashtableEntry {
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
        int hash;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
        Object key;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
        Object value;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
        IdentityHashtableEntry next;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
}
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
public final class IdentityHashtable extends Dictionary {
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
     * The hash table data.
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    private transient IdentityHashtableEntry table[];
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
     * The total number of entries in the hash table.
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
    private transient int count;
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
     * Rehashes the table when count exceeds this threshold.
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    private int threshold;
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
     * The load factor for the hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
    private float loadFactor;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
     * Constructs a new, empty hashtable with the specified initial
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
     * capacity and the specified load factor.
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
     * @param      initialCapacity   the initial capacity of the hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
     * @param      loadFactor        a number between 0.0 and 1.0.
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
     * @exception  IllegalArgumentException  if the initial capacity is less
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
     *               than or equal to zero, or if the load factor is less than
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
     *               or equal to zero.
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
     * @since      JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
    public IdentityHashtable(int initialCapacity, float loadFactor) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
        if ((initialCapacity <= 0) || (loadFactor <= 0.0)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
            throw new IllegalArgumentException();
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        this.loadFactor = loadFactor;
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        table = new IdentityHashtableEntry[initialCapacity];
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        threshold = (int)(initialCapacity * loadFactor);
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
     * Constructs a new, empty hashtable with the specified initial capacity
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
     * and default load factor.
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
     * @param   initialCapacity   the initial capacity of the hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
    public IdentityHashtable(int initialCapacity) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        this(initialCapacity, 0.75f);
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
     * Constructs a new, empty hashtable with a default capacity and load
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
     * factor.
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
    public IdentityHashtable() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        this(101, 0.75f);
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
     * Returns the number of keys in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
     * @return  the number of keys in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
    public int size() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
        return count;
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
     * Tests if this hashtable maps no keys to values.
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
     * @return  <code>true</code> if this hashtable maps no keys to values;
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
     *          <code>false</code> otherwise.
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
    public boolean isEmpty() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
        return count == 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
     * Returns an enumeration of the keys in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
     * @return  an enumeration of the keys in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
     * @see     java.util.Enumeration
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
     * @see     java.util.Hashtable#elements()
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
    public Enumeration keys() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
        return new IdentityHashtableEnumerator(table, true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
     * Returns an enumeration of the values in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
     * Use the Enumeration methods on the returned object to fetch the elements
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
     * sequentially.
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
     * @return  an enumeration of the values in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
     * @see     java.util.Enumeration
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
     * @see     java.util.Hashtable#keys()
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
    public Enumeration elements() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
        return new IdentityHashtableEnumerator(table, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
     * Tests if some key maps into the specified value in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
     * This operation is more expensive than the <code>containsKey</code>
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
     * method.
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
     * @param      value   a value to search for.
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
     * @return     <code>true</code> if some key maps to the
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
     *             <code>value</code> argument in this hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
     *             <code>false</code> otherwise.
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
     * @exception  NullPointerException  if the value is <code>null</code>.
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
     * @see        java.util.Hashtable#containsKey(java.lang.Object)
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
     * @since      JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
    public boolean contains(Object value) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
        if (value == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
            throw new NullPointerException();
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
        IdentityHashtableEntry tab[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        for (int i = tab.length ; i-- > 0 ;) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
            for (IdentityHashtableEntry e = tab[i] ; e != null ; e = e.next) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
                if (e.value == value) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
                    return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
     * Tests if the specified object is a key in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
     * @param   key   possible key.
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
     * @return  <code>true</code> if the specified object is a key in this
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
     *          hashtable; <code>false</code> otherwise.
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
     * @see     java.util.Hashtable#contains(java.lang.Object)
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
    public boolean containsKey(Object key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
        IdentityHashtableEntry tab[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
        int hash = System.identityHashCode(key);
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
        int index = (hash & 0x7FFFFFFF) % tab.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
        for (IdentityHashtableEntry e = tab[index] ; e != null ; e = e.next) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
            if ((e.hash == hash) && e.key == key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
                return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
        return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
     * Returns the value to which the specified key is mapped in this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
     * @param   key   a key in the hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
     * @return  the value to which the key is mapped in this hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
     *          <code>null</code> if the key is not mapped to any value in
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
     *          this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
     * @see     java.util.Hashtable#put(java.lang.Object, java.lang.Object)
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
    public Object get(Object key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        IdentityHashtableEntry tab[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
        int hash = System.identityHashCode(key);
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
        int index = (hash & 0x7FFFFFFF) % tab.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
        for (IdentityHashtableEntry e = tab[index] ; e != null ; e = e.next) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
            if ((e.hash == hash) && e.key == key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
                return e.value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
     * Rehashes the contents of the hashtable into a hashtable with a
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
     * larger capacity. This method is called automatically when the
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
     * number of keys in the hashtable exceeds this hashtable's capacity
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
     * and load factor.
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
    protected void rehash() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
        int oldCapacity = table.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
        IdentityHashtableEntry oldTable[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
        int newCapacity = oldCapacity * 2 + 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        IdentityHashtableEntry newTable[] = new IdentityHashtableEntry[newCapacity];
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
        threshold = (int)(newCapacity * loadFactor);
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
        table = newTable;
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
        //System.out.println("rehash old=" + oldCapacity + ", new=" + newCapacity + ", thresh=" + threshold + ", count=" + count);
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
        for (int i = oldCapacity ; i-- > 0 ;) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
            for (IdentityHashtableEntry old = oldTable[i] ; old != null ; ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
                IdentityHashtableEntry e = old;
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
                old = old.next;
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
                e.next = newTable[index];
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
                newTable[index] = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
     * Maps the specified <code>key</code> to the specified
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
     * <code>value</code> in this hashtable. Neither the key nor the
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
     * value can be <code>null</code>.
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
     * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
     * The value can be retrieved by calling the <code>get</code> method
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
     * with a key that is equal to the original key.
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
     * @param      key     the hashtable key.
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
     * @param      value   the value.
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
     * @return     the previous value of the specified key in this hashtable,
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
     *             or <code>null</code> if it did not have one.
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
     * @exception  NullPointerException  if the key or value is
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
     *               <code>null</code>.
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
     * @see     java.util.Hashtable#get(java.lang.Object)
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
    public Object put(Object key, Object value) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
        // Make sure the value is not null
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
        if (value == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
            throw new NullPointerException();
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
        // Makes sure the key is not already in the hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
        IdentityHashtableEntry tab[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
        int hash = System.identityHashCode(key);
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
        int index = (hash & 0x7FFFFFFF) % tab.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
        for (IdentityHashtableEntry e = tab[index] ; e != null ; e = e.next) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
            if ((e.hash == hash) && e.key == key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
                Object old = e.value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
                e.value = value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
                return old;
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
        if (count >= threshold) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
            // Rehash the table if the threshold is exceeded
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
            rehash();
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
            return put(key, value);
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
        // Creates the new entry.
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
        IdentityHashtableEntry e = new IdentityHashtableEntry();
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
        e.hash = hash;
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
        e.key = key;
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
        e.value = value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
        e.next = tab[index];
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
        tab[index] = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
        count++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
        /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
     * Removes the key (and its corresponding value) from this
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
     * hashtable. This method does nothing if the key is not in the hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
     * @param   key   the key that needs to be removed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
     * @return  the value to which the key had been mapped in this hashtable,
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
     *          or <code>null</code> if the key did not have a mapping.
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
    public Object remove(Object key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
        IdentityHashtableEntry tab[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
        int hash = System.identityHashCode(key);
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
        int index = (hash & 0x7FFFFFFF) % tab.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
        for (IdentityHashtableEntry e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
            if ((e.hash == hash) && e.key == key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
                if (prev != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
                    prev.next = e.next;
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
                    tab[index] = e.next;
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
                count--;
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
                return e.value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
     * Clears this hashtable so that it contains no keys.
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
    public void clear() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
        IdentityHashtableEntry tab[] = table;
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        for (int index = tab.length; --index >= 0; )
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
            tab[index] = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
        count = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
     * Returns a rather long string representation of this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
     * @return  a string representation of this hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
     * @since   JDK1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
    public String toString() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
        int max = size() - 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
        StringBuffer buf = new StringBuffer();
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
        Enumeration k = keys();
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
        Enumeration e = elements();
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
        buf.append("{");
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
        for (int i = 0; i <= max; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
            String s1 = k.nextElement().toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
            String s2 = e.nextElement().toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
            buf.append(s1 + "=" + s2);
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
            if (i < max) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
                buf.append(", ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
        buf.append("}");
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
        return buf.toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
}