KickJava   Java API By Example, From Geeks To Geeks.

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


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