325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A *
325N/A * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.fastinfoset.util;
325N/A
325N/Aimport com.sun.xml.internal.fastinfoset.QualifiedName;
325N/Aimport com.sun.xml.internal.fastinfoset.CommonResourceBundle;
325N/A
325N/Apublic class LocalNameQualifiedNamesMap extends KeyIntMap {
325N/A
325N/A private LocalNameQualifiedNamesMap _readOnlyMap;
325N/A
325N/A private int _index;
325N/A
325N/A public static class Entry {
325N/A final String _key;
325N/A final int _hash;
325N/A public QualifiedName[] _value;
325N/A public int _valueIndex;
325N/A Entry _next;
325N/A
325N/A public Entry(String key, int hash, Entry next) {
325N/A _key = key;
325N/A _hash = hash;
325N/A _next = next;
325N/A _value = new QualifiedName[1];
325N/A }
325N/A
325N/A public void addQualifiedName(QualifiedName name) {
325N/A if (_valueIndex < _value.length) {
325N/A _value[_valueIndex++] = name;
325N/A } else if (_valueIndex == _value.length) {
325N/A QualifiedName[] newValue = new QualifiedName[_valueIndex * 3 / 2 + 1];
325N/A System.arraycopy(_value, 0, newValue, 0, _valueIndex);
325N/A _value = newValue;
325N/A _value[_valueIndex++] = name;
325N/A }
325N/A }
325N/A }
325N/A
325N/A private Entry[] _table;
325N/A
325N/A public LocalNameQualifiedNamesMap(int initialCapacity, float loadFactor) {
325N/A super(initialCapacity, loadFactor);
325N/A
325N/A _table = new Entry[_capacity];
325N/A }
325N/A
325N/A public LocalNameQualifiedNamesMap(int initialCapacity) {
325N/A this(initialCapacity, DEFAULT_LOAD_FACTOR);
325N/A }
325N/A
325N/A public LocalNameQualifiedNamesMap() {
325N/A this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
325N/A }
325N/A
325N/A public final void clear() {
325N/A for (int i = 0; i < _table.length; i++) {
325N/A _table[i] = null;
325N/A }
325N/A _size = 0;
325N/A
325N/A if (_readOnlyMap != null) {
325N/A _index = _readOnlyMap.getIndex();
325N/A } else {
325N/A _index = 0;
325N/A }
325N/A }
325N/A
325N/A public final void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) {
325N/A if (!(readOnlyMap instanceof LocalNameQualifiedNamesMap)) {
325N/A throw new IllegalArgumentException(CommonResourceBundle.getInstance().
325N/A getString("message.illegalClass", new Object[]{readOnlyMap}));
325N/A }
325N/A
325N/A setReadOnlyMap((LocalNameQualifiedNamesMap)readOnlyMap, clear);
325N/A }
325N/A
325N/A public final void setReadOnlyMap(LocalNameQualifiedNamesMap readOnlyMap, boolean clear) {
325N/A _readOnlyMap = readOnlyMap;
325N/A if (_readOnlyMap != null) {
325N/A _readOnlyMapSize = _readOnlyMap.size();
325N/A _index = _readOnlyMap.getIndex();
325N/A if (clear) {
325N/A clear();
325N/A }
325N/A } else {
325N/A _readOnlyMapSize = 0;
325N/A _index = 0;
325N/A }
325N/A }
325N/A
325N/A public final boolean isQNameFromReadOnlyMap(QualifiedName name) {
325N/A return (_readOnlyMap != null && name.index <= _readOnlyMap.getIndex());
325N/A }
325N/A
325N/A public final int getNextIndex() {
325N/A return _index++;
325N/A }
325N/A
325N/A public final int getIndex() {
325N/A return _index;
325N/A }
325N/A
325N/A public final Entry obtainEntry(String key) {
325N/A final int hash = hashHash(key.hashCode());
325N/A
325N/A if (_readOnlyMap != null) {
325N/A final Entry entry = _readOnlyMap.getEntry(key, hash);
325N/A if (entry != null) {
325N/A return entry;
325N/A }
325N/A }
325N/A
325N/A final int tableIndex = indexFor(hash, _table.length);
325N/A for (Entry e = _table[tableIndex]; e != null; e = e._next) {
325N/A if (e._hash == hash && eq(key, e._key)) {
325N/A return e;
325N/A }
325N/A }
325N/A
325N/A return addEntry(key, hash, tableIndex);
325N/A }
325N/A
325N/A public final Entry obtainDynamicEntry(String key) {
325N/A final int hash = hashHash(key.hashCode());
325N/A
325N/A final int tableIndex = indexFor(hash, _table.length);
325N/A for (Entry e = _table[tableIndex]; e != null; e = e._next) {
325N/A if (e._hash == hash && eq(key, e._key)) {
325N/A return e;
325N/A }
325N/A }
325N/A
325N/A return addEntry(key, hash, tableIndex);
325N/A }
325N/A
325N/A private final Entry getEntry(String key, int hash) {
325N/A if (_readOnlyMap != null) {
325N/A final Entry entry = _readOnlyMap.getEntry(key, hash);
325N/A if (entry != null) {
325N/A return entry;
325N/A }
325N/A }
325N/A
325N/A final int tableIndex = indexFor(hash, _table.length);
325N/A for (Entry e = _table[tableIndex]; e != null; e = e._next) {
325N/A if (e._hash == hash && eq(key, e._key)) {
325N/A return e;
325N/A }
325N/A }
325N/A
325N/A return null;
325N/A }
325N/A
325N/A
325N/A private final Entry addEntry(String key, int hash, int bucketIndex) {
325N/A Entry e = _table[bucketIndex];
325N/A _table[bucketIndex] = new Entry(key, hash, e);
325N/A e = _table[bucketIndex];
325N/A if (_size++ >= _threshold) {
325N/A resize(2 * _table.length);
325N/A }
325N/A
325N/A return e;
325N/A }
325N/A
325N/A private final void resize(int newCapacity) {
325N/A _capacity = newCapacity;
325N/A Entry[] oldTable = _table;
325N/A int oldCapacity = oldTable.length;
325N/A if (oldCapacity == MAXIMUM_CAPACITY) {
325N/A _threshold = Integer.MAX_VALUE;
325N/A return;
325N/A }
325N/A
325N/A Entry[] newTable = new Entry[_capacity];
325N/A transfer(newTable);
325N/A _table = newTable;
325N/A _threshold = (int)(_capacity * _loadFactor);
325N/A }
325N/A
325N/A private final void transfer(Entry[] newTable) {
325N/A Entry[] src = _table;
325N/A int newCapacity = newTable.length;
325N/A for (int j = 0; j < src.length; j++) {
325N/A Entry e = src[j];
325N/A if (e != null) {
325N/A src[j] = null;
325N/A do {
325N/A Entry next = e._next;
325N/A int i = indexFor(e._hash, newCapacity);
325N/A e._next = newTable[i];
325N/A newTable[i] = e;
325N/A e = next;
325N/A } while (e != null);
325N/A }
325N/A }
325N/A }
325N/A
325N/A private final boolean eq(String x, String y) {
325N/A return x == y || x.equals(y);
325N/A }
325N/A
325N/A}