KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > util > LocalNameQualifiedNamesMap


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

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 JavaDoc;
45 import java.util.List JavaDoc;
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 JavaDoc _key;
55         final int _hash;
56         public QualifiedName[] _value;
57         public int _valueIndex;
58         Entry _next;
59         
60         public Entry(String JavaDoc 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 JavaDoc(CommonResourceBundle.getInstance().
111                     getString("message.illegalClass", new Object JavaDoc[]{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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc x, String JavaDoc y) {
224         return x == y || x.equals(y);
225     }
226
227 }
228
Popular Tags