src/share/jaxws_classes/com/sun/xml/internal/dtdparser/SimpleHashtable.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.dtdparser;
ohair@286 27
ohair@286 28 import java.util.Enumeration;
ohair@286 29
ohair@286 30
ohair@286 31 // This could be replaced by Collections class unless we want
ohair@286 32 // to be able to run on JDK 1.1
ohair@286 33
ohair@286 34
ohair@286 35 /**
ohair@286 36 * This class implements a special purpose hashtable. It works like a
ohair@286 37 * normal <code>java.util.Hashtable</code> except that: <OL>
ohair@286 38 * <p/>
ohair@286 39 * <LI> Keys to "get" are strings which are known to be interned,
ohair@286 40 * so that "==" is used instead of "String.equals". (Interning
ohair@286 41 * could be document-relative instead of global.)
ohair@286 42 * <p/>
ohair@286 43 * <LI> It's not synchronized, since it's to be used only by
ohair@286 44 * one thread at a time.
ohair@286 45 * <p/>
ohair@286 46 * <LI> The keys () enumerator allocates no memory, with live
ohair@286 47 * updates to the data disallowed.
ohair@286 48 * <p/>
ohair@286 49 * <LI> It's got fewer bells and whistles: fixed threshold and
ohair@286 50 * load factor, no JDK 1.2 collection support, only keys can be
ohair@286 51 * enumerated, things can't be removed, simpler inheritance; more.
ohair@286 52 * <p/>
ohair@286 53 * </OL>
ohair@286 54 * <p/>
ohair@286 55 * <P> The overall result is that it's less expensive to use these in
ohair@286 56 * performance-critical locations, in terms both of CPU and memory,
ohair@286 57 * than <code>java.util.Hashtable</code> instances. In this package
ohair@286 58 * it makes a significant difference when normalizing attributes,
ohair@286 59 * which is done for each start-element construct.
ohair@286 60 *
ohair@286 61 * @version $Revision: 1.2 $
ohair@286 62 */
ohair@286 63 final class SimpleHashtable implements Enumeration {
ohair@286 64 // entries ...
ohair@286 65 private Entry table[];
ohair@286 66
ohair@286 67 // currently enumerated key
ohair@286 68 private Entry current = null;
ohair@286 69 private int currentBucket = 0;
ohair@286 70
ohair@286 71 private int count;
ohair@286 72 private int threshold;
ohair@286 73
ohair@286 74 private static final float loadFactor = 0.75f;
ohair@286 75
ohair@286 76
ohair@286 77 /**
ohair@286 78 * Constructs a new, empty hashtable with the specified initial
ohair@286 79 * capacity.
ohair@286 80 *
ohair@286 81 * @param initialCapacity the initial capacity of the hashtable.
ohair@286 82 */
ohair@286 83 public SimpleHashtable(int initialCapacity) {
ohair@286 84 if (initialCapacity < 0)
ohair@286 85 throw new IllegalArgumentException("Illegal Capacity: " +
ohair@286 86 initialCapacity);
ohair@286 87 if (initialCapacity == 0)
ohair@286 88 initialCapacity = 1;
ohair@286 89 table = new Entry[initialCapacity];
ohair@286 90 threshold = (int) (initialCapacity * loadFactor);
ohair@286 91 }
ohair@286 92
ohair@286 93 /**
ohair@286 94 * Constructs a new, empty hashtable with a default capacity.
ohair@286 95 */
ohair@286 96 public SimpleHashtable() {
ohair@286 97 this(11);
ohair@286 98 }
ohair@286 99
ohair@286 100 /**
ohair@286 101 */
ohair@286 102 public void clear() {
ohair@286 103 count = 0;
ohair@286 104 currentBucket = 0;
ohair@286 105 current = null;
ohair@286 106 for (int i = 0; i < table.length; i++)
ohair@286 107 table[i] = null;
ohair@286 108 }
ohair@286 109
ohair@286 110 /**
ohair@286 111 * Returns the number of keys in this hashtable.
ohair@286 112 *
ohair@286 113 * @return the number of keys in this hashtable.
ohair@286 114 */
ohair@286 115 public int size() {
ohair@286 116 return count;
ohair@286 117 }
ohair@286 118
ohair@286 119 /**
ohair@286 120 * Returns an enumeration of the keys in this hashtable.
ohair@286 121 *
ohair@286 122 * @return an enumeration of the keys in this hashtable.
ohair@286 123 * @see Enumeration
ohair@286 124 */
ohair@286 125 public Enumeration keys() {
ohair@286 126 currentBucket = 0;
ohair@286 127 current = null;
ohair@286 128 return this;
ohair@286 129 }
ohair@286 130
ohair@286 131 /**
ohair@286 132 * Used to view this as an enumeration; returns true if there
ohair@286 133 * are more keys to be enumerated.
ohair@286 134 */
ohair@286 135 public boolean hasMoreElements() {
ohair@286 136 if (current != null)
ohair@286 137 return true;
ohair@286 138 while (currentBucket < table.length) {
ohair@286 139 current = table[currentBucket++];
ohair@286 140 if (current != null)
ohair@286 141 return true;
ohair@286 142 }
ohair@286 143 return false;
ohair@286 144 }
ohair@286 145
ohair@286 146 /**
ohair@286 147 * Used to view this as an enumeration; returns the next key
ohair@286 148 * in the enumeration.
ohair@286 149 */
ohair@286 150 public Object nextElement() {
ohair@286 151 Object retval;
ohair@286 152
ohair@286 153 if (current == null)
ohair@286 154 throw new IllegalStateException();
ohair@286 155 retval = current.key;
ohair@286 156 current = current.next;
ohair@286 157 return retval;
ohair@286 158 }
ohair@286 159
ohair@286 160
ohair@286 161 /**
ohair@286 162 * Returns the value to which the specified key is mapped in this hashtable.
ohair@286 163 */
ohair@286 164 public Object get(String key) {
ohair@286 165 Entry tab[] = table;
ohair@286 166 int hash = key.hashCode();
ohair@286 167 int index = (hash & 0x7FFFFFFF) % tab.length;
ohair@286 168 for (Entry e = tab[index]; e != null; e = e.next) {
ohair@286 169 if ((e.hash == hash) && (e.key == key))
ohair@286 170 return e.value;
ohair@286 171 }
ohair@286 172 return null;
ohair@286 173 }
ohair@286 174
ohair@286 175 /**
ohair@286 176 * Returns the value to which the specified key is mapped in this
ohair@286 177 * hashtable ... the key isn't necessarily interned, though.
ohair@286 178 */
ohair@286 179 public Object getNonInterned(String key) {
ohair@286 180 Entry tab[] = table;
ohair@286 181 int hash = key.hashCode();
ohair@286 182 int index = (hash & 0x7FFFFFFF) % tab.length;
ohair@286 183 for (Entry e = tab[index]; e != null; e = e.next) {
ohair@286 184 if ((e.hash == hash) && e.key.equals(key))
ohair@286 185 return e.value;
ohair@286 186 }
ohair@286 187 return null;
ohair@286 188 }
ohair@286 189
ohair@286 190 /**
ohair@286 191 * Increases the capacity of and internally reorganizes this
ohair@286 192 * hashtable, in order to accommodate and access its entries more
ohair@286 193 * efficiently. This method is called automatically when the
ohair@286 194 * number of keys in the hashtable exceeds this hashtable's capacity
ohair@286 195 * and load factor.
ohair@286 196 */
ohair@286 197 private void rehash() {
ohair@286 198 int oldCapacity = table.length;
ohair@286 199 Entry oldMap[] = table;
ohair@286 200
ohair@286 201 int newCapacity = oldCapacity * 2 + 1;
ohair@286 202 Entry newMap[] = new Entry[newCapacity];
ohair@286 203
ohair@286 204 threshold = (int) (newCapacity * loadFactor);
ohair@286 205 table = newMap;
ohair@286 206
ohair@286 207 /*
ohair@286 208 System.out.println("rehash old=" + oldCapacity
ohair@286 209 + ", new=" + newCapacity
ohair@286 210 + ", thresh=" + threshold
ohair@286 211 + ", count=" + count);
ohair@286 212 */
ohair@286 213
ohair@286 214 for (int i = oldCapacity; i-- > 0;) {
ohair@286 215 for (Entry old = oldMap[i]; old != null;) {
ohair@286 216 Entry e = old;
ohair@286 217 old = old.next;
ohair@286 218
ohair@286 219 int index = (e.hash & 0x7FFFFFFF) % newCapacity;
ohair@286 220 e.next = newMap[index];
ohair@286 221 newMap[index] = e;
ohair@286 222 }
ohair@286 223 }
ohair@286 224 }
ohair@286 225
ohair@286 226 /**
ohair@286 227 * Maps the specified <code>key</code> to the specified
ohair@286 228 * <code>value</code> in this hashtable. Neither the key nor the
ohair@286 229 * value can be <code>null</code>.
ohair@286 230 * <p/>
ohair@286 231 * <P>The value can be retrieved by calling the <code>get</code> method
ohair@286 232 * with a key that is equal to the original key.
ohair@286 233 */
ohair@286 234 public Object put(Object key, Object value) {
ohair@286 235 // Make sure the value is not null
ohair@286 236 if (value == null) {
ohair@286 237 throw new NullPointerException();
ohair@286 238 }
ohair@286 239
ohair@286 240 // Makes sure the key is not already in the hashtable.
ohair@286 241 Entry tab[] = table;
ohair@286 242 int hash = key.hashCode();
ohair@286 243 int index = (hash & 0x7FFFFFFF) % tab.length;
ohair@286 244 for (Entry e = tab[index]; e != null; e = e.next) {
ohair@286 245 // if ((e.hash == hash) && e.key.equals(key)) {
ohair@286 246 if ((e.hash == hash) && (e.key == key)) {
ohair@286 247 Object old = e.value;
ohair@286 248 e.value = value;
ohair@286 249 return old;
ohair@286 250 }
ohair@286 251 }
ohair@286 252
ohair@286 253 if (count >= threshold) {
ohair@286 254 // Rehash the table if the threshold is exceeded
ohair@286 255 rehash();
ohair@286 256
ohair@286 257 tab = table;
ohair@286 258 index = (hash & 0x7FFFFFFF) % tab.length;
ohair@286 259 }
ohair@286 260
ohair@286 261 // Creates the new entry.
ohair@286 262 Entry e = new Entry(hash, key, value, tab[index]);
ohair@286 263 tab[index] = e;
ohair@286 264 count++;
ohair@286 265 return null;
ohair@286 266 }
ohair@286 267
ohair@286 268
ohair@286 269 /**
ohair@286 270 * Hashtable collision list.
ohair@286 271 */
ohair@286 272 private static class Entry {
ohair@286 273 int hash;
ohair@286 274 Object key;
ohair@286 275 Object value;
ohair@286 276 Entry next;
ohair@286 277
ohair@286 278 protected Entry(int hash, Object key, Object value, Entry next) {
ohair@286 279 this.hash = hash;
ohair@286 280 this.key = key;
ohair@286 281 this.value = value;
ohair@286 282 this.next = next;
ohair@286 283 }
ohair@286 284 }
ohair@286 285 }

mercurial