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

Save This Page
Home » openjdk-7 » com.sun.corba.se.impl » util » [javadoc | source]