src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/QNameMap.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.util;
aoqi@0 27 import java.util.AbstractSet;
aoqi@0 28 import java.util.Iterator;
aoqi@0 29 import java.util.NoSuchElementException;
aoqi@0 30 import java.util.Set;
aoqi@0 31 import java.util.Map;
aoqi@0 32 import java.util.Collection;
aoqi@0 33 import java.util.HashSet;
aoqi@0 34
aoqi@0 35 import javax.xml.namespace.QName;
aoqi@0 36
aoqi@0 37 import com.sun.xml.internal.bind.v2.runtime.Name;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Map keyed by {@link QName}.
aoqi@0 41 *
aoqi@0 42 * This specialized map allows a look up operation without constructing
aoqi@0 43 * a new QName instance, for a performance reason. This {@link Map} assumes
aoqi@0 44 * that both namespace URI and local name are {@link String#intern() intern}ed.
aoqi@0 45 *
aoqi@0 46 * @since JAXB 2.0
aoqi@0 47 */
aoqi@0 48 public final class QNameMap<V> {
aoqi@0 49 /**
aoqi@0 50 * The default initial capacity - MUST be a power of two.
aoqi@0 51 */
aoqi@0 52 private static final int DEFAULT_INITIAL_CAPACITY = 16;
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * The maximum capacity, used if a higher value is implicitly specified
aoqi@0 56 * by either of the constructors with arguments.
aoqi@0 57 * MUST be a power of two <= 1<<30.
aoqi@0 58 */
aoqi@0 59 private static final int MAXIMUM_CAPACITY = 1 << 30;
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * The table, resized as necessary. Length MUST Always be a power of two.
aoqi@0 63 */
aoqi@0 64 transient Entry<V>[] table = new Entry[DEFAULT_INITIAL_CAPACITY];
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * The number of key-value mappings contained in this identity hash map.
aoqi@0 68 */
aoqi@0 69 transient int size;
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * The next size value at which to resize . Taking it as
aoqi@0 73 * MAXIMUM_CAPACITY
aoqi@0 74 * @serial
aoqi@0 75 */
aoqi@0 76 private int threshold;
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * The load factor used when none specified in constructor.
aoqi@0 80 **/
aoqi@0 81 private static final float DEFAULT_LOAD_FACTOR = 0.75f;
aoqi@0 82
aoqi@0 83
aoqi@0 84
aoqi@0 85 /**
aoqi@0 86 * Gives an entrySet view of this map
aoqi@0 87 */
aoqi@0 88 private Set<Entry<V>> entrySet = null;
aoqi@0 89
aoqi@0 90 public QNameMap() {
aoqi@0 91 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
aoqi@0 92 table = new Entry[DEFAULT_INITIAL_CAPACITY];
aoqi@0 93
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 /**
aoqi@0 97 * Associates the specified value with the specified keys in this map.
aoqi@0 98 * If the map previously contained a mapping for this key, the old
aoqi@0 99 * value is replaced.
aoqi@0 100 *
aoqi@0 101 * @param namespaceUri First key with which the specified value is to be associated.
aoqi@0 102 * @param localname Second key with which the specified value is to be associated.
aoqi@0 103 * @param value value to be associated with the specified key.
aoqi@0 104 *
aoqi@0 105 */
aoqi@0 106 public void put(String namespaceUri,String localname, V value ) {
aoqi@0 107 //keys cannot be null
aoqi@0 108 assert localname !=null;
aoqi@0 109 assert namespaceUri !=null;
aoqi@0 110 // keys must be interned
aoqi@0 111 assert localname == localname.intern();
aoqi@0 112 assert namespaceUri == namespaceUri.intern();
aoqi@0 113
aoqi@0 114 int hash = hash(localname);
aoqi@0 115 int i = indexFor(hash, table.length);
aoqi@0 116
aoqi@0 117 for (Entry<V> e = table[i]; e != null; e = e.next) {
aoqi@0 118 if (e.hash == hash && localname == e.localName && namespaceUri==e.nsUri) {
aoqi@0 119 e.value = value;
aoqi@0 120 return;
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 addEntry(hash, namespaceUri,localname, value, i);
aoqi@0 125
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 public void put(QName name, V value ) {
aoqi@0 129 put(name.getNamespaceURI(),name.getLocalPart(),value);
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 public void put(Name name, V value ) {
aoqi@0 133 put(name.nsUri,name.localName,value);
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 /**
aoqi@0 137 * Returns the value to which the specified keys are mapped in this QNameMap,
aoqi@0 138 * or <tt>null</tt> if the map contains no mapping for this key.
aoqi@0 139 *
aoqi@0 140 * @param nsUri the namespaceUri key whose associated value is to be returned.
aoqi@0 141 * @param localPart the localPart key whose associated value is to be returned.
aoqi@0 142 * @return the value to which this map maps the specified set of keya, or
aoqi@0 143 * <tt>null</tt> if the map contains no mapping for this set of keys.
aoqi@0 144 * @see #put(String,String, Object)
aoqi@0 145 */
aoqi@0 146 public V get( String nsUri, String localPart ) {
aoqi@0 147 Entry<V> e = getEntry(nsUri,localPart);
aoqi@0 148 if(e==null) return null;
aoqi@0 149 else return e.value;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 public V get( QName name ) {
aoqi@0 153 return get(name.getNamespaceURI(),name.getLocalPart());
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 /**
aoqi@0 157 * Returns the number of keys-value mappings in this map.
aoqi@0 158 *
aoqi@0 159 * @return the number of keys-value mappings in this map.
aoqi@0 160 */
aoqi@0 161 public int size() {
aoqi@0 162 return size;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 /**
aoqi@0 166 * Copies all of the mappings from the specified map to this map
aoqi@0 167 * These mappings will replace any mappings that
aoqi@0 168 * this map had for any of the keys currently in the specified map.
aoqi@0 169 *
aoqi@0 170 * @param map mappings to be stored in this map.
aoqi@0 171 *
aoqi@0 172 */
aoqi@0 173 public QNameMap<V> putAll(QNameMap<? extends V> map) {
aoqi@0 174 int numKeysToBeAdded = map.size();
aoqi@0 175 if (numKeysToBeAdded == 0)
aoqi@0 176 return this;
aoqi@0 177
aoqi@0 178
aoqi@0 179 if (numKeysToBeAdded > threshold) {
aoqi@0 180 int targetCapacity = numKeysToBeAdded;
aoqi@0 181 if (targetCapacity > MAXIMUM_CAPACITY)
aoqi@0 182 targetCapacity = MAXIMUM_CAPACITY;
aoqi@0 183 int newCapacity = table.length;
aoqi@0 184 while (newCapacity < targetCapacity)
aoqi@0 185 newCapacity <<= 1;
aoqi@0 186 if (newCapacity > table.length)
aoqi@0 187 resize(newCapacity);
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 for( Entry<? extends V> e : map.entrySet() )
aoqi@0 191 put(e.nsUri,e.localName,e.getValue());
aoqi@0 192 return this;
aoqi@0 193 }
aoqi@0 194
aoqi@0 195
aoqi@0 196 /**
aoqi@0 197 * Returns a hash value for the specified object.The hash value is computed
aoqi@0 198 * for the localName.
aoqi@0 199 */
aoqi@0 200 private static int hash(String x) {
aoqi@0 201 int h = x.hashCode();
aoqi@0 202
aoqi@0 203 h += ~(h << 9);
aoqi@0 204 h ^= (h >>> 14);
aoqi@0 205 h += (h << 4);
aoqi@0 206 h ^= (h >>> 10);
aoqi@0 207 return h;
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 /**
aoqi@0 211 * Returns index for hash code h.
aoqi@0 212 */
aoqi@0 213 private static int indexFor(int h, int length) {
aoqi@0 214 return h & (length-1);
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 /**
aoqi@0 218 * Add a new entry with the specified keys, value and hash code to
aoqi@0 219 * the specified bucket. It is the responsibility of this
aoqi@0 220 * method to resize the table if appropriate.
aoqi@0 221 *
aoqi@0 222 */
aoqi@0 223 private void addEntry(int hash, String nsUri, String localName, V value, int bucketIndex) {
aoqi@0 224 Entry<V> e = table[bucketIndex];
aoqi@0 225 table[bucketIndex] = new Entry<V>(hash, nsUri, localName, value, e);
aoqi@0 226 if (size++ >= threshold)
aoqi@0 227 resize(2 * table.length);
aoqi@0 228 }
aoqi@0 229
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * Rehashes the contents of this map into a new array with a
aoqi@0 233 * larger capacity. This method is called automatically when the
aoqi@0 234 * number of keys in this map reaches its threshold.
aoqi@0 235 */
aoqi@0 236 private void resize(int newCapacity) {
aoqi@0 237 Entry[] oldTable = table;
aoqi@0 238 int oldCapacity = oldTable.length;
aoqi@0 239 if (oldCapacity == MAXIMUM_CAPACITY) {
aoqi@0 240 threshold = Integer.MAX_VALUE;
aoqi@0 241 return;
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 Entry[] newTable = new Entry[newCapacity];
aoqi@0 245 transfer(newTable);
aoqi@0 246 table = newTable;
aoqi@0 247 threshold = newCapacity;
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 /**
aoqi@0 251 * Transfer all entries from current table to newTable.
aoqi@0 252 */
aoqi@0 253 private void transfer(Entry<V>[] newTable) {
aoqi@0 254 Entry<V>[] src = table;
aoqi@0 255 int newCapacity = newTable.length;
aoqi@0 256 for (int j = 0; j < src.length; j++) {
aoqi@0 257 Entry<V> e = src[j];
aoqi@0 258 if (e != null) {
aoqi@0 259 src[j] = null;
aoqi@0 260 do {
aoqi@0 261 Entry<V> next = e.next;
aoqi@0 262 int i = indexFor(e.hash, newCapacity);
aoqi@0 263 e.next = newTable[i];
aoqi@0 264 newTable[i] = e;
aoqi@0 265 e = next;
aoqi@0 266 } while (e != null);
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 /**
aoqi@0 272 * Returns one random item in the map.
aoqi@0 273 * If this map is empty, return null.
aoqi@0 274 *
aoqi@0 275 * <p>
aoqi@0 276 * This method is useful to obtain the value from a map that only contains one element.
aoqi@0 277 */
aoqi@0 278 public Entry<V> getOne() {
aoqi@0 279 for( Entry<V> e : table ) {
aoqi@0 280 if(e!=null)
aoqi@0 281 return e;
aoqi@0 282 }
aoqi@0 283 return null;
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 public Collection<QName> keySet() {
aoqi@0 287 Set<QName> r = new HashSet<QName>();
aoqi@0 288 for (Entry<V> e : entrySet()) {
aoqi@0 289 r.add(e.createQName());
aoqi@0 290 }
aoqi@0 291 return r;
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 private abstract class HashIterator<E> implements Iterator<E> {
aoqi@0 295 Entry<V> next; // next entry to return
aoqi@0 296 int index; // current slot
aoqi@0 297
aoqi@0 298 HashIterator() {
aoqi@0 299 Entry<V>[] t = table;
aoqi@0 300 int i = t.length;
aoqi@0 301 Entry<V> n = null;
aoqi@0 302 if (size != 0) { // advance to first entry
aoqi@0 303 while (i > 0 && (n = t[--i]) == null) {}
aoqi@0 304 }
aoqi@0 305 next = n;
aoqi@0 306 index = i;
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 public boolean hasNext() {
aoqi@0 310 return next != null;
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 Entry<V> nextEntry() {
aoqi@0 314 Entry<V> e = next;
aoqi@0 315 if (e == null)
aoqi@0 316 throw new NoSuchElementException();
aoqi@0 317
aoqi@0 318 Entry<V> n = e.next;
aoqi@0 319 Entry<V>[] t = table;
aoqi@0 320 int i = index;
aoqi@0 321 while (n == null && i > 0)
aoqi@0 322 n = t[--i];
aoqi@0 323 index = i;
aoqi@0 324 next = n;
aoqi@0 325 return e;
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 public void remove() {
aoqi@0 329 throw new UnsupportedOperationException();
aoqi@0 330 }
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 public boolean containsKey(String nsUri,String localName) {
aoqi@0 334 return getEntry(nsUri,localName)!=null;
aoqi@0 335 }
aoqi@0 336
aoqi@0 337
aoqi@0 338 /**
aoqi@0 339 * Returns true if this map is empty.
aoqi@0 340 */
aoqi@0 341 public boolean isEmpty() {
aoqi@0 342 return size == 0;
aoqi@0 343 }
aoqi@0 344
aoqi@0 345
aoqi@0 346 public static final class Entry<V> {
aoqi@0 347 /** The namespace URI. */
aoqi@0 348 public final String nsUri;
aoqi@0 349
aoqi@0 350 /** The localPart. */
aoqi@0 351 public final String localName;
aoqi@0 352
aoqi@0 353 V value;
aoqi@0 354 final int hash;
aoqi@0 355 Entry<V> next;
aoqi@0 356
aoqi@0 357 /**
aoqi@0 358 * Create new entry.
aoqi@0 359 */
aoqi@0 360 Entry(int h, String nsUri, String localName, V v, Entry<V> n) {
aoqi@0 361 value = v;
aoqi@0 362 next = n;
aoqi@0 363 this.nsUri = nsUri;
aoqi@0 364 this.localName = localName;
aoqi@0 365 hash = h;
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 /**
aoqi@0 369 * Creates a new QName object from {@link #nsUri} and {@link #localName}.
aoqi@0 370 */
aoqi@0 371 public QName createQName() {
aoqi@0 372 return new QName(nsUri,localName);
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 public V getValue() {
aoqi@0 376 return value;
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 public V setValue(V newValue) {
aoqi@0 380 V oldValue = value;
aoqi@0 381 value = newValue;
aoqi@0 382 return oldValue;
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 @Override
aoqi@0 386 public boolean equals(Object o) {
aoqi@0 387 if (!(o instanceof Entry))
aoqi@0 388 return false;
aoqi@0 389 Entry e = (Entry)o;
aoqi@0 390 String k1 = nsUri;
aoqi@0 391 String k2 = e.nsUri;
aoqi@0 392 String k3 = localName;
aoqi@0 393 String k4 = e.localName;
aoqi@0 394 if (k1 == k2 || (k1 != null && k1.equals(k2)) &&
aoqi@0 395 (k3 == k4 ||(k3 !=null && k3.equals(k4)))) {
aoqi@0 396 Object v1 = getValue();
aoqi@0 397 Object v2 = e.getValue();
aoqi@0 398 if (v1 == v2 || (v1 != null && v1.equals(v2)))
aoqi@0 399 return true;
aoqi@0 400 }
aoqi@0 401 return false;
aoqi@0 402 }
aoqi@0 403
aoqi@0 404 @Override
aoqi@0 405 public int hashCode() {
aoqi@0 406 return ( localName.hashCode()) ^
aoqi@0 407 (value==null ? 0 : value.hashCode());
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 @Override
aoqi@0 411 public String toString() {
aoqi@0 412 return '"'+nsUri +"\",\"" +localName + "\"=" + getValue();
aoqi@0 413 }
aoqi@0 414 }
aoqi@0 415
aoqi@0 416 public Set<Entry<V>> entrySet() {
aoqi@0 417 Set<Entry<V>> es = entrySet;
aoqi@0 418 return es != null ? es : (entrySet = new EntrySet());
aoqi@0 419 }
aoqi@0 420
aoqi@0 421 private Iterator<Entry<V>> newEntryIterator() {
aoqi@0 422 return new EntryIterator();
aoqi@0 423 }
aoqi@0 424
aoqi@0 425 private class EntryIterator extends HashIterator<Entry<V>> {
aoqi@0 426 public Entry<V> next() {
aoqi@0 427 return nextEntry();
aoqi@0 428 }
aoqi@0 429 }
aoqi@0 430 private class EntrySet extends AbstractSet<Entry<V>> {
aoqi@0 431 public Iterator<Entry<V>> iterator() {
aoqi@0 432 return newEntryIterator();
aoqi@0 433 }
aoqi@0 434 @Override
aoqi@0 435 public boolean contains(Object o) {
aoqi@0 436 if (!(o instanceof Entry))
aoqi@0 437 return false;
aoqi@0 438 Entry<V> e = (Entry<V>) o;
aoqi@0 439 Entry<V> candidate = getEntry(e.nsUri,e.localName);
aoqi@0 440 return candidate != null && candidate.equals(e);
aoqi@0 441 }
aoqi@0 442 @Override
aoqi@0 443 public boolean remove(Object o) {
aoqi@0 444 throw new UnsupportedOperationException();
aoqi@0 445 }
aoqi@0 446 public int size() {
aoqi@0 447 return size;
aoqi@0 448 }
aoqi@0 449 }
aoqi@0 450
aoqi@0 451 private Entry<V> getEntry(String nsUri,String localName) {
aoqi@0 452 // strings must be interned
aoqi@0 453 assert nsUri==nsUri.intern();
aoqi@0 454 assert localName==localName.intern();
aoqi@0 455
aoqi@0 456 int hash = hash(localName);
aoqi@0 457 int i = indexFor(hash, table.length);
aoqi@0 458 Entry<V> e = table[i];
aoqi@0 459 while (e != null && !(localName == e.localName && nsUri == e.nsUri))
aoqi@0 460 e = e.next;
aoqi@0 461 return e;
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 @Override
aoqi@0 465 public String toString() {
aoqi@0 466 StringBuilder buf = new StringBuilder();
aoqi@0 467 buf.append('{');
aoqi@0 468
aoqi@0 469 for( Entry<V> e : entrySet() ) {
aoqi@0 470 if(buf.length()>1)
aoqi@0 471 buf.append(',');
aoqi@0 472 buf.append('[');
aoqi@0 473 buf.append(e);
aoqi@0 474 buf.append(']');
aoqi@0 475 }
aoqi@0 476
aoqi@0 477 buf.append('}');
aoqi@0 478 return buf.toString();
aoqi@0 479 }
aoqi@0 480 }

mercurial