|
1 /* |
|
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 * |
|
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 */ |
|
27 |
|
28 package com.sun.xml.internal.fastinfoset.util; |
|
29 |
|
30 import com.sun.xml.internal.fastinfoset.QualifiedName; |
|
31 import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
|
32 |
|
33 public class LocalNameQualifiedNamesMap extends KeyIntMap { |
|
34 |
|
35 private LocalNameQualifiedNamesMap _readOnlyMap; |
|
36 |
|
37 private int _index; |
|
38 |
|
39 public static class Entry { |
|
40 final String _key; |
|
41 final int _hash; |
|
42 public QualifiedName[] _value; |
|
43 public int _valueIndex; |
|
44 Entry _next; |
|
45 |
|
46 public Entry(String key, int hash, Entry next) { |
|
47 _key = key; |
|
48 _hash = hash; |
|
49 _next = next; |
|
50 _value = new QualifiedName[1]; |
|
51 } |
|
52 |
|
53 public void addQualifiedName(QualifiedName name) { |
|
54 if (_valueIndex < _value.length) { |
|
55 _value[_valueIndex++] = name; |
|
56 } else if (_valueIndex == _value.length) { |
|
57 QualifiedName[] newValue = new QualifiedName[_valueIndex * 3 / 2 + 1]; |
|
58 System.arraycopy(_value, 0, newValue, 0, _valueIndex); |
|
59 _value = newValue; |
|
60 _value[_valueIndex++] = name; |
|
61 } |
|
62 } |
|
63 } |
|
64 |
|
65 private Entry[] _table; |
|
66 |
|
67 public LocalNameQualifiedNamesMap(int initialCapacity, float loadFactor) { |
|
68 super(initialCapacity, loadFactor); |
|
69 |
|
70 _table = new Entry[_capacity]; |
|
71 } |
|
72 |
|
73 public LocalNameQualifiedNamesMap(int initialCapacity) { |
|
74 this(initialCapacity, DEFAULT_LOAD_FACTOR); |
|
75 } |
|
76 |
|
77 public LocalNameQualifiedNamesMap() { |
|
78 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); |
|
79 } |
|
80 |
|
81 public final void clear() { |
|
82 for (int i = 0; i < _table.length; i++) { |
|
83 _table[i] = null; |
|
84 } |
|
85 _size = 0; |
|
86 |
|
87 if (_readOnlyMap != null) { |
|
88 _index = _readOnlyMap.getIndex(); |
|
89 } else { |
|
90 _index = 0; |
|
91 } |
|
92 } |
|
93 |
|
94 public final void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) { |
|
95 if (!(readOnlyMap instanceof LocalNameQualifiedNamesMap)) { |
|
96 throw new IllegalArgumentException(CommonResourceBundle.getInstance(). |
|
97 getString("message.illegalClass", new Object[]{readOnlyMap})); |
|
98 } |
|
99 |
|
100 setReadOnlyMap((LocalNameQualifiedNamesMap)readOnlyMap, clear); |
|
101 } |
|
102 |
|
103 public final void setReadOnlyMap(LocalNameQualifiedNamesMap readOnlyMap, boolean clear) { |
|
104 _readOnlyMap = readOnlyMap; |
|
105 if (_readOnlyMap != null) { |
|
106 _readOnlyMapSize = _readOnlyMap.size(); |
|
107 _index = _readOnlyMap.getIndex(); |
|
108 if (clear) { |
|
109 clear(); |
|
110 } |
|
111 } else { |
|
112 _readOnlyMapSize = 0; |
|
113 _index = 0; |
|
114 } |
|
115 } |
|
116 |
|
117 public final boolean isQNameFromReadOnlyMap(QualifiedName name) { |
|
118 return (_readOnlyMap != null && name.index <= _readOnlyMap.getIndex()); |
|
119 } |
|
120 |
|
121 public final int getNextIndex() { |
|
122 return _index++; |
|
123 } |
|
124 |
|
125 public final int getIndex() { |
|
126 return _index; |
|
127 } |
|
128 |
|
129 public final Entry obtainEntry(String key) { |
|
130 final int hash = hashHash(key.hashCode()); |
|
131 |
|
132 if (_readOnlyMap != null) { |
|
133 final Entry entry = _readOnlyMap.getEntry(key, hash); |
|
134 if (entry != null) { |
|
135 return entry; |
|
136 } |
|
137 } |
|
138 |
|
139 final int tableIndex = indexFor(hash, _table.length); |
|
140 for (Entry e = _table[tableIndex]; e != null; e = e._next) { |
|
141 if (e._hash == hash && eq(key, e._key)) { |
|
142 return e; |
|
143 } |
|
144 } |
|
145 |
|
146 return addEntry(key, hash, tableIndex); |
|
147 } |
|
148 |
|
149 public final Entry obtainDynamicEntry(String key) { |
|
150 final int hash = hashHash(key.hashCode()); |
|
151 |
|
152 final int tableIndex = indexFor(hash, _table.length); |
|
153 for (Entry e = _table[tableIndex]; e != null; e = e._next) { |
|
154 if (e._hash == hash && eq(key, e._key)) { |
|
155 return e; |
|
156 } |
|
157 } |
|
158 |
|
159 return addEntry(key, hash, tableIndex); |
|
160 } |
|
161 |
|
162 private final Entry getEntry(String key, int hash) { |
|
163 if (_readOnlyMap != null) { |
|
164 final Entry entry = _readOnlyMap.getEntry(key, hash); |
|
165 if (entry != null) { |
|
166 return entry; |
|
167 } |
|
168 } |
|
169 |
|
170 final int tableIndex = indexFor(hash, _table.length); |
|
171 for (Entry e = _table[tableIndex]; e != null; e = e._next) { |
|
172 if (e._hash == hash && eq(key, e._key)) { |
|
173 return e; |
|
174 } |
|
175 } |
|
176 |
|
177 return null; |
|
178 } |
|
179 |
|
180 |
|
181 private final Entry addEntry(String key, int hash, int bucketIndex) { |
|
182 Entry e = _table[bucketIndex]; |
|
183 _table[bucketIndex] = new Entry(key, hash, e); |
|
184 e = _table[bucketIndex]; |
|
185 if (_size++ >= _threshold) { |
|
186 resize(2 * _table.length); |
|
187 } |
|
188 |
|
189 return e; |
|
190 } |
|
191 |
|
192 private final void resize(int newCapacity) { |
|
193 _capacity = newCapacity; |
|
194 Entry[] oldTable = _table; |
|
195 int oldCapacity = oldTable.length; |
|
196 if (oldCapacity == MAXIMUM_CAPACITY) { |
|
197 _threshold = Integer.MAX_VALUE; |
|
198 return; |
|
199 } |
|
200 |
|
201 Entry[] newTable = new Entry[_capacity]; |
|
202 transfer(newTable); |
|
203 _table = newTable; |
|
204 _threshold = (int)(_capacity * _loadFactor); |
|
205 } |
|
206 |
|
207 private final void transfer(Entry[] newTable) { |
|
208 Entry[] src = _table; |
|
209 int newCapacity = newTable.length; |
|
210 for (int j = 0; j < src.length; j++) { |
|
211 Entry e = src[j]; |
|
212 if (e != null) { |
|
213 src[j] = null; |
|
214 do { |
|
215 Entry next = e._next; |
|
216 int i = indexFor(e._hash, newCapacity); |
|
217 e._next = newTable[i]; |
|
218 newTable[i] = e; |
|
219 e = next; |
|
220 } while (e != null); |
|
221 } |
|
222 } |
|
223 } |
|
224 |
|
225 private final boolean eq(String x, String y) { |
|
226 return x == y || x.equals(y); |
|
227 } |
|
228 |
|
229 } |