1 38 39 40 package com.sun.xml.fastinfoset.util; 41 42 import com.sun.xml.fastinfoset.QualifiedName; 43 import com.sun.xml.fastinfoset.CommonResourceBundle; 44 import java.util.ArrayList ; 45 import java.util.List ; 46 47 public class LocalNameQualifiedNamesMap extends KeyIntMap { 48 49 private LocalNameQualifiedNamesMap _readOnlyMap; 50 51 private int _index; 52 53 public static class Entry { 54 final String _key; 55 final int _hash; 56 public QualifiedName[] _value; 57 public int _valueIndex; 58 Entry _next; 59 60 public Entry(String key, int hash, Entry next) { 61 _key = key; 62 _hash = hash; 63 _next = next; 64 _value = new QualifiedName[1]; 65 } 66 67 public void addQualifiedName(QualifiedName name) { 68 if (_valueIndex < _value.length) { 69 _value[_valueIndex++] = name; 70 } else if (_valueIndex == _value.length) { 71 QualifiedName[] newValue = new QualifiedName[_valueIndex * 3 / 2 + 1]; 72 System.arraycopy(_value, 0, newValue, 0, _valueIndex); 73 _value = newValue; 74 _value[_valueIndex++] = name; 75 } 76 } 77 } 78 79 private Entry[] _table; 80 81 public LocalNameQualifiedNamesMap(int initialCapacity, float loadFactor) { 82 super(initialCapacity, loadFactor); 83 84 _table = new Entry[_capacity]; 85 } 86 87 public LocalNameQualifiedNamesMap(int initialCapacity) { 88 this(initialCapacity, DEFAULT_LOAD_FACTOR); 89 } 90 91 public LocalNameQualifiedNamesMap() { 92 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); 93 } 94 95 public final void clear() { 96 for (int i = 0; i < _table.length; i++) { 97 _table[i] = null; 98 } 99 _size = 0; 100 101 if (_readOnlyMap != null) { 102 _index = _readOnlyMap.getIndex(); 103 } else { 104 _index = 0; 105 } 106 } 107 108 public final void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) { 109 if (!(readOnlyMap instanceof LocalNameQualifiedNamesMap)) { 110 throw new IllegalArgumentException (CommonResourceBundle.getInstance(). 111 getString("message.illegalClass", new Object []{readOnlyMap})); 112 } 113 114 setReadOnlyMap((LocalNameQualifiedNamesMap)readOnlyMap, clear); 115 } 116 117 public final void setReadOnlyMap(LocalNameQualifiedNamesMap readOnlyMap, boolean clear) { 118 _readOnlyMap = readOnlyMap; 119 if (_readOnlyMap != null) { 120 _readOnlyMapSize = _readOnlyMap.size(); 121 _index = _readOnlyMap.getIndex(); 122 if (clear) { 123 clear(); 124 } 125 } else { 126 _readOnlyMapSize = 0; 127 _index = 0; 128 } 129 } 130 131 public final int getNextIndex() { 132 return _index++; 133 } 134 135 public final int getIndex() { 136 return _index; 137 138 } 139 140 public final Entry obtainEntry(String key) { 141 final int hash = hashHash(key.hashCode()); 142 143 if (_readOnlyMap != null) { 144 final Entry entry = _readOnlyMap.getEntry(key, hash); 145 if (entry != null) { 146 return entry; 147 } 148 } 149 150 final int tableIndex = indexFor(hash, _table.length); 151 for (Entry e = _table[tableIndex]; e != null; e = e._next) { 152 if (e._hash == hash && eq(key, e._key)) { 153 return e; 154 } 155 } 156 157 return addEntry(key, hash, tableIndex); 158 } 159 160 private final Entry getEntry(String key, int hash) { 161 if (_readOnlyMap != null) { 162 final Entry entry = _readOnlyMap.getEntry(key, hash); 163 if (entry != null) { 164 return entry; 165 } 166 } 167 168 final int tableIndex = indexFor(hash, _table.length); 169 for (Entry e = _table[tableIndex]; e != null; e = e._next) { 170 if (e._hash == hash && eq(key, e._key)) { 171 return e; 172 } 173 } 174 175 return null; 176 } 177 178 179 private final Entry addEntry(String key, int hash, int bucketIndex) { 180 Entry e = _table[bucketIndex]; 181 _table[bucketIndex] = new Entry(key, hash, e); 182 e = _table[bucketIndex]; 183 if (_size++ >= _threshold) { 184 resize(2 * _table.length); 185 } 186 187 return e; 188 } 189 190 private final void resize(int newCapacity) { 191 _capacity = newCapacity; 192 Entry[] oldTable = _table; 193 int oldCapacity = oldTable.length; 194 if (oldCapacity == MAXIMUM_CAPACITY) { 195 _threshold = Integer.MAX_VALUE; 196 return; 197 } 198 199 Entry[] newTable = new Entry[_capacity]; 200 transfer(newTable); 201 _table = newTable; 202 _threshold = (int)(_capacity * _loadFactor); 203 } 204 205 private final void transfer(Entry[] newTable) { 206 Entry[] src = _table; 207 int newCapacity = newTable.length; 208 for (int j = 0; j < src.length; j++) { 209 Entry e = src[j]; 210 if (e != null) { 211 src[j] = null; 212 do { 213 Entry next = e._next; 214 int i = indexFor(e._hash, newCapacity); 215 e._next = newTable[i]; 216 newTable[i] = e; 217 e = next; 218 } while (e != null); 219 } 220 } 221 } 222 223 private final boolean eq(String x, String y) { 224 return x == y || x.equals(y); 225 } 226 227 } 228 | Popular Tags |