-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCIHashtable.java
More file actions
277 lines (233 loc) · 6.47 KB
/
CIHashtable.java
File metadata and controls
277 lines (233 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* @(#)CIHashtable.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* This class implements a Hashtable with case-insensitive Strings as keys.
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
*/
class CIHashtable extends Hashtable
{
// Constructors
/**
* Create a new CIHashtable with the specified initial capacity and the
* specified load factor.
*
* @param intialCapacity the initial number of buckets
* @param loadFactor a number between 0.0 and 1.0
* @see java.util.Hashtable(int, float)
*/
public CIHashtable(int initialCapacity, float loadFactor)
{
super(initialCapacity, loadFactor);
}
/**
* Create a new CIHashtable with the specified initial capacity.
*
* @param intialCapacity the initial number of buckets
* @see java.util.Hashtable(int)
*/
public CIHashtable(int initialCapacity)
{
super(initialCapacity);
}
/**
* Create a new CIHashtable with a default initial capacity.
*
* @see java.util.Hashtable()
*/
public CIHashtable()
{
super();
}
// Methods
/**
* Retrieves the object associated with the specified key. The key lookup
* is case-insensitive.
*
* @param key the key
* @return the object associated with the key, or null if none found.
* @see java.util.Hashtable.get(Object)
*/
public Object get(String key)
{
return super.get(new CIString(key));
}
/**
* Stores the specified object with the specified key.
*
* @param key the key
* @param value the object to be associated with the key
* @return the object previously associated with the key, or null if
* there was none.
* @see java.util.Hashtable.put(Object, Object)
*/
@SuppressWarnings("unchecked")
public Object put(String key, Object value)
{
return super.put(new CIString(key), value);
}
/**
* Looks whether any object is associated with the specified key. The
* key lookup is case insensitive.
*
* @param key the key
* @return true is there is an object associated with key, false otherwise
* @see java.util.Hashtable.containsKey(Object)
*/
public boolean containsKey(String key)
{
return super.containsKey(new CIString(key));
}
/**
* Removes the object associated with this key from the Hashtable. The
* key lookup is case insensitive.
*
* @param key the key
* @return the object associated with this key, or null if there was none.
* @see java.util.Hashtable.remove(Object)
*/
public Object remove(String key)
{
return super.remove(new CIString(key));
}
/**
* Returns an enumeration of all the keys in the Hashtable.
*
* @return the requested Enumerator
* @see java.util.Hashtable.keys(Object)
*/
public Enumeration keys()
{
return new CIHashtableEnumeration(super.keys());
}
}
/**
* A simple enumerator which delegates everything to the real enumerator.
* If a CIString element is returned, then the string it represents is
* returned instead.
*/
final class CIHashtableEnumeration implements Enumeration
{
Enumeration HTEnum;
public CIHashtableEnumeration(Enumeration enumera)
{
HTEnum = enumera;
}
public boolean hasMoreElements()
{
return HTEnum.hasMoreElements();
}
public Object nextElement()
{
Object tmp = HTEnum.nextElement();
if (tmp instanceof CIString)
return ((CIString) tmp).getString();
return tmp;
}
}
/**
* This class' raison d'etre is that I want to use a Hashtable using
* Strings as keys and I want the lookup be case insensitive, but I
* also want to be able retrieve the keys with original case (otherwise
* I could just use toLowerCase() in the get() and put()). Since the
* class String is final we create a new class that holds the string
* and overrides the methods hashCode() and equals().
*/
final class CIString
{
/** the string */
private String string;
/** the hash code */
private int hash;
/** the constructor */
public CIString(String string)
{
this.string = string;
this.hash = calcHashCode(string);
}
/** return the original string */
public final String getString()
{
return string;
}
/** the hash code was precomputed */
public int hashCode()
{
return hash;
}
/**
* We smash case before calculation so that the hash code is
* "case insensitive". This is based on code snarfed from
* java.lang.String.hashCode().
*/
private static final int calcHashCode(String str)
{
int hash = 0;
char llc[] = lc;
int len = str.length();
for (int idx= 0; idx<len; idx++)
hash = 31*hash + llc[str.charAt(idx)];
return hash;
}
/**
* Uses the case insensitive comparison.
*/
public boolean equals(Object obj)
{
if (obj != null)
{
if (obj instanceof CIString)
return string.equalsIgnoreCase(((CIString) obj).string);
if (obj instanceof String)
return string.equalsIgnoreCase((String) obj);
}
return false;
}
/**
* Just return the internal string.
*/
public String toString()
{
return string;
}
private static final char[] lc = new char[256];
static
{
// just ISO-8859-1
for (char idx=0; idx<256; idx++)
lc[idx] = Character.toLowerCase(idx);
}
}