01: /**
02: *******************************************************************************
03: * Copyright (C) 2001-2006, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */package com.ibm.icu.util;
07:
08: import com.ibm.icu.lang.UCharacter;
09:
10: /**
11: * A string used as a key in java.util.Hashtable and other
12: * collections. It retains case information, but its equals() and
13: * hashCode() methods ignore case.
14: * @stable ICU 2.0
15: */
16: public class CaseInsensitiveString {
17:
18: private String string;
19:
20: private int hash = 0;
21:
22: /**
23: * Constructs an CaseInsentiveString object from the given string
24: * @param s The string to construct this object from
25: * @stable ICU 2.0
26: */
27: public CaseInsensitiveString(String s) {
28: string = s;
29: }
30:
31: /**
32: * returns the underlying string
33: * @return String
34: * @stable ICU 2.0
35: */
36: public String getString() {
37: return string;
38: }
39:
40: /**
41: * Compare the object with this
42: * @param o Object to compare this object with
43: * @stable ICU 2.0
44: */
45: public boolean equals(Object o) {
46: try {
47: return string
48: .equalsIgnoreCase(((CaseInsensitiveString) o).string);
49: } catch (ClassCastException e) {
50: try {
51: return string.equalsIgnoreCase((String) o);
52: } catch (ClassCastException e2) {
53: return false;
54: }
55: }
56: }
57:
58: /**
59: * Returns the hashCode of this object
60: * @return int hashcode
61: * @stable ICU 2.0
62: */
63: public int hashCode() {
64: if (hash == 0) {
65: hash = UCharacter.foldCase(string, true).hashCode();
66: }
67: return hash;
68: }
69:
70: /**
71: * Overrides superclass method
72: * @stable ICU 3.6
73: */
74: public String toString() {
75: return string;
76: }
77: }
|