KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import com.sun.xml.fastinfoset.CommonResourceBundle;
45
46 public class StringIntMap extends KeyIntMap {
47
48     protected StringIntMap _readOnlyMap;
49     
50     protected static class Entry extends BaseEntry {
51         final String JavaDoc _key;
52         Entry _next;
53         
54         public Entry(String JavaDoc key, int hash, int value, Entry next) {
55             super(hash, value);
56             _key = key;
57             _next = next;
58         }
59     }
60     
61     protected Entry[] _table;
62     
63     public StringIntMap(int initialCapacity, float loadFactor) {
64         super(initialCapacity, loadFactor);
65
66         _table = new Entry[_capacity];
67     }
68     
69     public StringIntMap(int initialCapacity) {
70         this(initialCapacity, DEFAULT_LOAD_FACTOR);
71     }
72
73     public StringIntMap() {
74         this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
75     }
76
77     public void clear() {
78         for (int i = 0; i < _table.length; i++) {
79             _table[i] = null;
80         }
81         _size = 0;
82     }
83
84     public void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) {
85         if (!(readOnlyMap instanceof StringIntMap)) {
86             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().
87                     getString("message.illegalClass", new Object JavaDoc[]{readOnlyMap}));
88         }
89         
90         setReadOnlyMap((StringIntMap)readOnlyMap, clear);
91     }
92     
93     public final void setReadOnlyMap(StringIntMap readOnlyMap, boolean clear) {
94         _readOnlyMap = readOnlyMap;
95         if (_readOnlyMap != null) {
96             _readOnlyMapSize = _readOnlyMap.size();
97             
98             if (clear) {
99                 clear();
100             }
101         } else {
102             _readOnlyMapSize = 0;
103         }
104     }
105     
106     public final int obtainIndex(String JavaDoc key) {
107         final int hash = hashHash(key.hashCode());
108         
109         if (_readOnlyMap != null) {
110             final int index = _readOnlyMap.get(key, hash);
111             if (index != -1) {
112                 return index;
113             }
114         }
115         
116         final int tableIndex = indexFor(hash, _table.length);
117         for (Entry e = _table[tableIndex]; e != null; e = e._next) {
118             if (e._hash == hash && eq(key, e._key)) {
119                 return e._value;
120             }
121         }
122
123         addEntry(key, hash, _size + _readOnlyMapSize, tableIndex);
124         return NOT_PRESENT;
125     }
126
127     public final void add(String JavaDoc key) {
128         final int hash = hashHash(key.hashCode());
129         final int tableIndex = indexFor(hash, _table.length);
130         addEntry(key, hash, _size + _readOnlyMapSize, tableIndex);
131     }
132
133     public final int get(String JavaDoc key) {
134         return get(key, hashHash(key.hashCode()));
135     }
136     
137     private final int get(String JavaDoc key, int hash) {
138         if (_readOnlyMap != null) {
139             final int i = _readOnlyMap.get(key, hash);
140             if (i != -1) {
141                 return i;
142             }
143         }
144
145         final int tableIndex = indexFor(hash, _table.length);
146         for (Entry e = _table[tableIndex]; e != null; e = e._next) {
147             if (e._hash == hash && eq(key, e._key)) {
148                 return e._value;
149             }
150         }
151                 
152         return -1;
153     }
154
155
156     private final void addEntry(String JavaDoc key, int hash, int value, int bucketIndex) {
157     Entry e = _table[bucketIndex];
158         _table[bucketIndex] = new Entry(key, hash, value, e);
159         if (_size++ >= _threshold) {
160             resize(2 * _table.length);
161         }
162     }
163     
164     protected final void resize(int newCapacity) {
165         _capacity = newCapacity;
166         Entry[] oldTable = _table;
167         int oldCapacity = oldTable.length;
168         if (oldCapacity == MAXIMUM_CAPACITY) {
169             _threshold = Integer.MAX_VALUE;
170             return;
171         }
172
173         Entry[] newTable = new Entry[_capacity];
174         transfer(newTable);
175         _table = newTable;
176         _threshold = (int)(_capacity * _loadFactor);
177     }
178
179     private final void transfer(Entry[] newTable) {
180         Entry[] src = _table;
181         int newCapacity = newTable.length;
182         for (int j = 0; j < src.length; j++) {
183             Entry e = src[j];
184             if (e != null) {
185                 src[j] = null;
186                 do {
187                     Entry next = e._next;
188                     int i = indexFor(e._hash, newCapacity);
189                     e._next = newTable[i];
190                     newTable[i] = e;
191                     e = next;
192                 } while (e != null);
193             }
194         }
195     }
196         
197     private final boolean eq(String JavaDoc x, String JavaDoc y) {
198         return x == y || x.equals(y);
199     }
200 }
201
Popular Tags