jdk/src/java.base/share/classes/java/util/Hashtable.java
changeset 38436 4676f6c9ebee
parent 32108 aa5490a167ee
child 44743 f0bbd698c486
equal deleted inserted replaced
38435:292ad46c1bf1 38436:4676f6c9ebee
     1 /*
     1 /*
     2  * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   228         this(Math.max(2*t.size(), 11), 0.75f);
   228         this(Math.max(2*t.size(), 11), 0.75f);
   229         putAll(t);
   229         putAll(t);
   230     }
   230     }
   231 
   231 
   232     /**
   232     /**
       
   233      * A constructor chained from {@link Properties} keeps Hashtable fields
       
   234      * uninitialized since they are not used.
       
   235      *
       
   236      * @param dummy a dummy parameter
       
   237      */
       
   238     Hashtable(Void dummy) {}
       
   239 
       
   240     /**
   233      * Returns the number of keys in this hashtable.
   241      * Returns the number of keys in this hashtable.
   234      *
   242      *
   235      * @return  the number of keys in this hashtable.
   243      * @return  the number of keys in this hashtable.
   236      */
   244      */
   237     public synchronized int size() {
   245     public synchronized int size() {
   547      * This is a relatively expensive operation.
   555      * This is a relatively expensive operation.
   548      *
   556      *
   549      * @return  a clone of the hashtable
   557      * @return  a clone of the hashtable
   550      */
   558      */
   551     public synchronized Object clone() {
   559     public synchronized Object clone() {
       
   560         Hashtable<?,?> t = cloneHashtable();
       
   561         t.table = new Entry<?,?>[table.length];
       
   562         for (int i = table.length ; i-- > 0 ; ) {
       
   563             t.table[i] = (table[i] != null)
       
   564                 ? (Entry<?,?>) table[i].clone() : null;
       
   565         }
       
   566         t.keySet = null;
       
   567         t.entrySet = null;
       
   568         t.values = null;
       
   569         t.modCount = 0;
       
   570         return t;
       
   571     }
       
   572 
       
   573     /** Calls super.clone() */
       
   574     final Hashtable<?,?> cloneHashtable() {
   552         try {
   575         try {
   553             Hashtable<?,?> t = (Hashtable<?,?>)super.clone();
   576             return (Hashtable<?,?>)super.clone();
   554             t.table = new Entry<?,?>[table.length];
       
   555             for (int i = table.length ; i-- > 0 ; ) {
       
   556                 t.table[i] = (table[i] != null)
       
   557                     ? (Entry<?,?>) table[i].clone() : null;
       
   558             }
       
   559             t.keySet = null;
       
   560             t.entrySet = null;
       
   561             t.values = null;
       
   562             t.modCount = 0;
       
   563             return t;
       
   564         } catch (CloneNotSupportedException e) {
   577         } catch (CloneNotSupportedException e) {
   565             // this shouldn't happen, since we are Cloneable
   578             // this shouldn't happen, since we are Cloneable
   566             throw new InternalError(e);
   579             throw new InternalError(e);
   567         }
   580         }
   568     }
   581     }
  1187      *             for each key-value mapping represented by the Hashtable
  1200      *             for each key-value mapping represented by the Hashtable
  1188      *             The key-value mappings are emitted in no particular order.
  1201      *             The key-value mappings are emitted in no particular order.
  1189      */
  1202      */
  1190     private void writeObject(java.io.ObjectOutputStream s)
  1203     private void writeObject(java.io.ObjectOutputStream s)
  1191             throws IOException {
  1204             throws IOException {
       
  1205         writeHashtable(s);
       
  1206     }
       
  1207 
       
  1208     /**
       
  1209      * Perform serialization of the Hashtable to an ObjectOutputStream.
       
  1210      * The Properties class overrides this method.
       
  1211      */
       
  1212     void writeHashtable(java.io.ObjectOutputStream s)
       
  1213             throws IOException {
  1192         Entry<Object, Object> entryStack = null;
  1214         Entry<Object, Object> entryStack = null;
  1193 
  1215 
  1194         synchronized (this) {
  1216         synchronized (this) {
  1195             // Write out the threshold and loadFactor
  1217             // Write out the threshold and loadFactor
  1196             s.defaultWriteObject();
  1218             s.defaultWriteObject();
  1217             entryStack = entryStack.next;
  1239             entryStack = entryStack.next;
  1218         }
  1240         }
  1219     }
  1241     }
  1220 
  1242 
  1221     /**
  1243     /**
       
  1244      * Called by Properties to write out a simulated threshold and loadfactor.
       
  1245      */
       
  1246     final void defaultWriteHashtable(java.io.ObjectOutputStream s, int length,
       
  1247             float loadFactor) throws IOException {
       
  1248         this.threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
       
  1249         this.loadFactor = loadFactor;
       
  1250         s.defaultWriteObject();
       
  1251     }
       
  1252 
       
  1253     /**
  1222      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
  1254      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
  1223      */
  1255      */
  1224     private void readObject(java.io.ObjectInputStream s)
  1256     private void readObject(java.io.ObjectInputStream s)
  1225          throws IOException, ClassNotFoundException
  1257             throws IOException, ClassNotFoundException {
  1226     {
  1258         readHashtable(s);
       
  1259     }
       
  1260 
       
  1261     /**
       
  1262      * Perform deserialization of the Hashtable from an ObjectInputStream.
       
  1263      * The Properties class overrides this method.
       
  1264      */
       
  1265     void readHashtable(java.io.ObjectInputStream s)
       
  1266             throws IOException, ClassNotFoundException {
  1227         // Read in the threshold and loadFactor
  1267         // Read in the threshold and loadFactor
  1228         s.defaultReadObject();
  1268         s.defaultReadObject();
  1229 
  1269 
  1230         // Validate loadFactor (ignore threshold - it will be re-computed)
  1270         // Validate loadFactor (ignore threshold - it will be re-computed)
  1231         if (loadFactor <= 0 || Float.isNaN(loadFactor))
  1271         if (loadFactor <= 0 || Float.isNaN(loadFactor))