KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > CaseInsensitiveString


1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.util;
8
9 import com.ibm.icu.lang.UCharacter;
10
11 /**
12  * A string used as a key in java.util.Hashtable and other
13  * collections. It retains case information, but its equals() and
14  * hashCode() methods ignore case.
15  * @stable ICU 2.0
16  */

17 public class CaseInsensitiveString {
18     
19     private String JavaDoc string;
20
21     private int hash = 0;
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 JavaDoc s) {
28         string = s;
29     }
30     /**
31      * returns the underlying string
32      * @return String
33      * @stable ICU 2.0
34      */

35     public String JavaDoc getString() {
36         return string;
37     }
38     /**
39      * Compare the object with this
40      * @param o Object to compare this object with
41      * @stable ICU 2.0
42      */

43     public boolean equals(Object JavaDoc o) {
44         try {
45             return string.equalsIgnoreCase(((CaseInsensitiveString)o).string);
46         } catch (ClassCastException JavaDoc e) {
47             try {
48                 return string.equalsIgnoreCase((String JavaDoc)o);
49             } catch (ClassCastException JavaDoc e2) {
50                 return false;
51             }
52         }
53     }
54     
55     /**
56      * Returns the hashCode of this object
57      * @return int hashcode
58      * @stable ICU 2.0
59      */

60     public int hashCode() {
61         if (hash == 0) {
62             hash = UCharacter.foldCase(string, true).hashCode();
63         }
64         return hash;
65     }
66     /**
67      * Overrides superclass method
68      * @stable ICU 3.6
69      */

70     public String JavaDoc toString() {
71         return string;
72     }
73 }
74
Popular Tags