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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.ws.util;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29
aoqi@0 30 import javax.xml.namespace.QName;
aoqi@0 31 import java.util.AbstractSet;
aoqi@0 32 import java.util.Collection;
aoqi@0 33 import java.util.HashSet;
aoqi@0 34 import java.util.Iterator;
aoqi@0 35 import java.util.Map;
aoqi@0 36 import java.util.NoSuchElementException;
aoqi@0 37 import java.util.Set;
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
aoqi@0 111 int hash = hash(localname);
aoqi@0 112 int i = indexFor(hash, table.length);
aoqi@0 113
aoqi@0 114 for (Entry<V> e = table[i]; e != null; e = e.next) {
aoqi@0 115 if (e.hash == hash && localname.equals(e.localName) && namespaceUri.equals(e.nsUri)) {
aoqi@0 116 e.value = value;
aoqi@0 117 return;
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 addEntry(hash, namespaceUri,localname, value, i);
aoqi@0 122
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 public void put(QName name, V value ) {
aoqi@0 126 put(name.getNamespaceURI(),name.getLocalPart(),value);
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 /**
aoqi@0 130 * Returns the value to which the specified keys are mapped in this QNameMap,
aoqi@0 131 * or <tt>null</tt> if the map contains no mapping for this key.
aoqi@0 132 *
aoqi@0 133 * @param nsUri the namespaceUri key whose associated value is to be returned.
aoqi@0 134 * @param localPart the localPart key whose associated value is to be returned.
aoqi@0 135 * @return the value to which this map maps the specified set of keya, or
aoqi@0 136 * <tt>null</tt> if the map contains no mapping for this set of keys.
aoqi@0 137 * @see #put(String,String, Object)
aoqi@0 138 */
aoqi@0 139 public V get( @NotNull String nsUri, String localPart ) {
aoqi@0 140 Entry<V> e = getEntry(nsUri,localPart);
aoqi@0 141 if(e==null) return null;
aoqi@0 142 else return e.value;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 public V get( QName name ) {
aoqi@0 146 return get(name.getNamespaceURI(),name.getLocalPart());
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 /**
aoqi@0 150 * Returns the number of keys-value mappings in this map.
aoqi@0 151 *
aoqi@0 152 * @return the number of keys-value mappings in this map.
aoqi@0 153 */
aoqi@0 154 public int size() {
aoqi@0 155 return size;
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 /**
aoqi@0 159 * Copies all of the mappings from the specified map to this map
aoqi@0 160 * These mappings will replace any mappings that
aoqi@0 161 * this map had for any of the keys currently in the specified map.
aoqi@0 162 *
aoqi@0 163 * @param map mappings to be stored in this map.
aoqi@0 164 *
aoqi@0 165 */
aoqi@0 166 public QNameMap<V> putAll(QNameMap<? extends V> map) {
aoqi@0 167 int numKeysToBeAdded = map.size();
aoqi@0 168 if (numKeysToBeAdded == 0)
aoqi@0 169 return this;
aoqi@0 170
aoqi@0 171
aoqi@0 172 if (numKeysToBeAdded > threshold) {
aoqi@0 173 int targetCapacity = numKeysToBeAdded;
aoqi@0 174 if (targetCapacity > MAXIMUM_CAPACITY)
aoqi@0 175 targetCapacity = MAXIMUM_CAPACITY;
aoqi@0 176 int newCapacity = table.length;
aoqi@0 177 while (newCapacity < targetCapacity)
aoqi@0 178 newCapacity <<= 1;
aoqi@0 179 if (newCapacity > table.length)
aoqi@0 180 resize(newCapacity);
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 for( Entry<? extends V> e : map.entrySet() )
aoqi@0 184 put(e.nsUri,e.localName,e.getValue());
aoqi@0 185 return this;
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 public QNameMap<V> putAll(Map<QName,? extends V> map) {
aoqi@0 189 for (Map.Entry<QName, ? extends V> e : map.entrySet()) {
aoqi@0 190 QName qn = e.getKey();
aoqi@0 191 put(qn.getNamespaceURI(),qn.getLocalPart(),e.getValue());
aoqi@0 192 }
aoqi@0 193 return this;
aoqi@0 194 }
aoqi@0 195
aoqi@0 196
aoqi@0 197 /**
aoqi@0 198 * Returns a hash value for the specified object.The hash value is computed
aoqi@0 199 * for the localName.
aoqi@0 200 */
aoqi@0 201 private static int hash(String x) {
aoqi@0 202 int h = x.hashCode();
aoqi@0 203
aoqi@0 204 h += ~(h << 9);
aoqi@0 205 h ^= (h >>> 14);
aoqi@0 206 h += (h << 4);
aoqi@0 207 h ^= (h >>> 10);
aoqi@0 208 return h;
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 /**
aoqi@0 212 * Returns index for hash code h.
aoqi@0 213 */
aoqi@0 214 private static int indexFor(int h, int length) {
aoqi@0 215 return h & (length-1);
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 /**
aoqi@0 219 * Add a new entry with the specified keys, value and hash code to
aoqi@0 220 * the specified bucket. It is the responsibility of this
aoqi@0 221 * method to resize the table if appropriate.
aoqi@0 222 *
aoqi@0 223 */
aoqi@0 224 private void addEntry(int hash, String nsUri, String localName, V value, int bucketIndex) {
aoqi@0 225 Entry<V> e = table[bucketIndex];
aoqi@0 226 table[bucketIndex] = new Entry<V>(hash, nsUri, localName, value, e);
aoqi@0 227 if (size++ >= threshold)
aoqi@0 228 resize(2 * table.length);
aoqi@0 229 }
aoqi@0 230
aoqi@0 231
aoqi@0 232 /**
aoqi@0 233 * Rehashes the contents of this map into a new array with a
aoqi@0 234 * larger capacity. This method is called automatically when the
aoqi@0 235 * number of keys in this map reaches its threshold.
aoqi@0 236 */
aoqi@0 237 private void resize(int newCapacity) {
aoqi@0 238 Entry[] oldTable = table;
aoqi@0 239 int oldCapacity = oldTable.length;
aoqi@0 240 if (oldCapacity == MAXIMUM_CAPACITY) {
aoqi@0 241 threshold = Integer.MAX_VALUE;
aoqi@0 242 return;
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 Entry[] newTable = new Entry[newCapacity];
aoqi@0 246 transfer(newTable);
aoqi@0 247 table = newTable;
aoqi@0 248 threshold = newCapacity;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 /**
aoqi@0 252 * Transfer all entries from current table to newTable.
aoqi@0 253 */
aoqi@0 254 private void transfer(Entry<V>[] newTable) {
aoqi@0 255 Entry<V>[] src = table;
aoqi@0 256 int newCapacity = newTable.length;
aoqi@0 257 for (int j = 0; j < src.length; j++) {
aoqi@0 258 Entry<V> e = src[j];
aoqi@0 259 if (e != null) {
aoqi@0 260 src[j] = null;
aoqi@0 261 do {
aoqi@0 262 Entry<V> next = e.next;
aoqi@0 263 int i = indexFor(e.hash, newCapacity);
aoqi@0 264 e.next = newTable[i];
aoqi@0 265 newTable[i] = e;
aoqi@0 266 e = next;
aoqi@0 267 } while (e != null);
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 /**
aoqi@0 273 * Returns one random item in the map.
aoqi@0 274 * If this map is empty, return null.
aoqi@0 275 *
aoqi@0 276 * <p>
aoqi@0 277 * This method is useful to obtain the value from a map that only contains one element.
aoqi@0 278 */
aoqi@0 279 public Entry<V> getOne() {
aoqi@0 280 for( Entry<V> e : table ) {
aoqi@0 281 if(e!=null)
aoqi@0 282 return e;
aoqi@0 283 }
aoqi@0 284 return null;
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 public Collection<QName> keySet() {
aoqi@0 288 Set<QName> r = new HashSet<QName>();
aoqi@0 289 for (Entry<V> e : entrySet()) {
aoqi@0 290 r.add(e.createQName());
aoqi@0 291 }
aoqi@0 292 return r;
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 public Iterable<V> values() {
aoqi@0 296 return views;
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 private transient Iterable<V> views = new Iterable<V>() {
aoqi@0 300 public Iterator<V> iterator() {
aoqi@0 301 return new ValueIterator();
aoqi@0 302 }
aoqi@0 303 };
aoqi@0 304
aoqi@0 305 private abstract class HashIterator<E> implements Iterator<E> {
aoqi@0 306 Entry<V> next; // next entry to return
aoqi@0 307 int index; // current slot
aoqi@0 308
aoqi@0 309 HashIterator() {
aoqi@0 310 Entry<V>[] t = table;
aoqi@0 311 int i = t.length;
aoqi@0 312 Entry<V> n = null;
aoqi@0 313 if (size != 0) { // advance to first entry
aoqi@0 314 while (i > 0 && (n = t[--i]) == null)
aoqi@0 315 ;
aoqi@0 316 }
aoqi@0 317 next = n;
aoqi@0 318 index = i;
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 public boolean hasNext() {
aoqi@0 322 return next != null;
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 Entry<V> nextEntry() {
aoqi@0 326 Entry<V> e = next;
aoqi@0 327 if (e == null)
aoqi@0 328 throw new NoSuchElementException();
aoqi@0 329
aoqi@0 330 Entry<V> n = e.next;
aoqi@0 331 Entry<V>[] t = table;
aoqi@0 332 int i = index;
aoqi@0 333 while (n == null && i > 0)
aoqi@0 334 n = t[--i];
aoqi@0 335 index = i;
aoqi@0 336 next = n;
aoqi@0 337 return e;
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 public void remove() {
aoqi@0 341 throw new UnsupportedOperationException();
aoqi@0 342 }
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 private class ValueIterator extends HashIterator<V> {
aoqi@0 346 public V next() {
aoqi@0 347 return nextEntry().value;
aoqi@0 348 }
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 public boolean containsKey(@NotNull String nsUri,String localName) {
aoqi@0 352 return getEntry(nsUri,localName)!=null;
aoqi@0 353 }
aoqi@0 354
aoqi@0 355
aoqi@0 356 /**
aoqi@0 357 * Returns true if this map is empty.
aoqi@0 358 */
aoqi@0 359 public boolean isEmpty() {
aoqi@0 360 return size == 0;
aoqi@0 361 }
aoqi@0 362
aoqi@0 363
aoqi@0 364 public static final class Entry<V> {
aoqi@0 365 /** The namespace URI. */
aoqi@0 366 public final String nsUri;
aoqi@0 367
aoqi@0 368 /** The localPart. */
aoqi@0 369 public final String localName;
aoqi@0 370
aoqi@0 371 V value;
aoqi@0 372 final int hash;
aoqi@0 373 Entry<V> next;
aoqi@0 374
aoqi@0 375 /**
aoqi@0 376 * Create new entry.
aoqi@0 377 */
aoqi@0 378 Entry(int h, String nsUri, String localName, V v, Entry<V> n) {
aoqi@0 379 value = v;
aoqi@0 380 next = n;
aoqi@0 381 this.nsUri = nsUri;
aoqi@0 382 this.localName = localName;
aoqi@0 383 hash = h;
aoqi@0 384 }
aoqi@0 385
aoqi@0 386 /**
aoqi@0 387 * Creates a new QName object from {@link #nsUri} and {@link #localName}.
aoqi@0 388 */
aoqi@0 389 public QName createQName() {
aoqi@0 390 return new QName(nsUri,localName);
aoqi@0 391 }
aoqi@0 392
aoqi@0 393 public V getValue() {
aoqi@0 394 return value;
aoqi@0 395 }
aoqi@0 396
aoqi@0 397 public V setValue(V newValue) {
aoqi@0 398 V oldValue = value;
aoqi@0 399 value = newValue;
aoqi@0 400 return oldValue;
aoqi@0 401 }
aoqi@0 402
aoqi@0 403 public boolean equals(Object o) {
aoqi@0 404 if (!(o instanceof Entry))
aoqi@0 405 return false;
aoqi@0 406 Entry e = (Entry)o;
aoqi@0 407 String k1 = nsUri;
aoqi@0 408 String k2 = e.nsUri;
aoqi@0 409 String k3 = localName;
aoqi@0 410 String k4 = e.localName;
aoqi@0 411 if (k1.equals(k2) && k3.equals(k4)) {
aoqi@0 412 Object v1 = getValue();
aoqi@0 413 Object v2 = e.getValue();
aoqi@0 414 if (v1 == v2 || (v1 != null && v1.equals(v2)))
aoqi@0 415 return true;
aoqi@0 416 }
aoqi@0 417 return false;
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 public int hashCode() {
aoqi@0 421 return ( localName.hashCode()) ^
aoqi@0 422 (value==null ? 0 : value.hashCode());
aoqi@0 423 }
aoqi@0 424
aoqi@0 425 public String toString() {
aoqi@0 426 return '"'+nsUri +"\",\"" +localName + "\"=" + getValue();
aoqi@0 427 }
aoqi@0 428 }
aoqi@0 429
aoqi@0 430 public Set<Entry<V>> entrySet() {
aoqi@0 431 Set<Entry<V>> es = entrySet;
aoqi@0 432 return es != null ? es : (entrySet = new EntrySet());
aoqi@0 433 }
aoqi@0 434
aoqi@0 435 private Iterator<Entry<V>> newEntryIterator() {
aoqi@0 436 return new EntryIterator();
aoqi@0 437 }
aoqi@0 438
aoqi@0 439 private class EntryIterator extends HashIterator<Entry<V>> {
aoqi@0 440 public Entry<V> next() {
aoqi@0 441 return nextEntry();
aoqi@0 442 }
aoqi@0 443 }
aoqi@0 444 private class EntrySet extends AbstractSet<Entry<V>> {
aoqi@0 445 public Iterator<Entry<V>> iterator() {
aoqi@0 446 return newEntryIterator();
aoqi@0 447 }
aoqi@0 448 public boolean contains(Object o) {
aoqi@0 449 if (!(o instanceof Entry))
aoqi@0 450 return false;
aoqi@0 451 Entry<V> e = (Entry<V>) o;
aoqi@0 452 Entry<V> candidate = getEntry(e.nsUri,e.localName);
aoqi@0 453 return candidate != null && candidate.equals(e);
aoqi@0 454 }
aoqi@0 455 public boolean remove(Object o) {
aoqi@0 456 throw new UnsupportedOperationException();
aoqi@0 457 }
aoqi@0 458 public int size() {
aoqi@0 459 return size;
aoqi@0 460 }
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 private Entry<V> getEntry(@NotNull String nsUri,String localName) {
aoqi@0 464 int hash = hash(localName);
aoqi@0 465 int i = indexFor(hash, table.length);
aoqi@0 466 Entry<V> e = table[i];
aoqi@0 467 while (e != null && !(localName.equals(e.localName) && nsUri.equals(e.nsUri)))
aoqi@0 468 e = e.next;
aoqi@0 469 return e;
aoqi@0 470 }
aoqi@0 471
aoqi@0 472 public String toString() {
aoqi@0 473 StringBuilder buf = new StringBuilder();
aoqi@0 474 buf.append('{');
aoqi@0 475
aoqi@0 476 for( Entry<V> e : entrySet() ) {
aoqi@0 477 if(buf.length()>1)
aoqi@0 478 buf.append(',');
aoqi@0 479 buf.append('[');
aoqi@0 480 buf.append(e);
aoqi@0 481 buf.append(']');
aoqi@0 482 }
aoqi@0 483
aoqi@0 484 buf.append('}');
aoqi@0 485 return buf.toString();
aoqi@0 486 }
aoqi@0 487 }

mercurial