01: /*
02: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
03: * reserved.
04: *
05: * Redistribution and use in source and binary forms, with or without
06: * modification, are permitted provided that the following conditions
07: * are met:
08: *
09: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in
14: * the documentation and/or other materials provided with the
15: * distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: *
29: */
30:
31: package rcm.util;
32:
33: /**
34: * String which compares case-insensitively with other strings.
35: * Especially useful as a case-insensitive hashtable key.
36: */
37: public class CaselessString {
38: String string;
39:
40: public CaselessString(String string) {
41: this .string = string;
42: }
43:
44: public boolean equals(Object obj) {
45: if (obj instanceof String || obj instanceof CaselessString)
46: return string.equalsIgnoreCase(obj.toString());
47: else
48: return false;
49: }
50:
51: public int hashCode() {
52: int hash = 0;
53: int len = string.length();
54:
55: if (len < 16) {
56: // use all characters
57: for (int i = 0; i < len; ++i)
58: hash = (hash * 37)
59: + Character.toUpperCase(string.charAt(i));
60: } else {
61: int skip = len / 8; // sample every 8th char
62: for (int i = 0; i < len; i += skip)
63: hash = (hash * 39)
64: + Character.toUpperCase(string.charAt(i));
65: }
66:
67: return hash;
68: }
69:
70: public String toString() {
71: return string;
72: }
73: }
|