]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/util/CaseInsensitiveString.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / util / CaseInsensitiveString.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2007, 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 string;
20
21     private int hash = 0;
22     
23     private String folded = null;
24     
25     private static String foldCase(String foldee)
26     {
27         return UCharacter.foldCase(foldee, true);
28     }
29     
30     private void getFolded()
31     {
32         if (folded == null) {
33             folded = foldCase(string);
34         }
35     }
36     
37     /**
38      * Constructs an CaseInsentiveString object from the given string
39      * @param s The string to construct this object from 
40      * @stable ICU 2.0
41      */
42     public CaseInsensitiveString(String s) {
43         string = s;
44     }
45     /**
46      * returns the underlying string 
47      * @return String
48      * @stable ICU 2.0
49      */
50     public String getString() {
51         return string;
52     }
53     /**
54      * Compare the object with this 
55      * @param o Object to compare this object with 
56      * @stable ICU 2.0
57      */
58     public boolean equals(Object o) {
59         getFolded();
60         
61         try {
62             CaseInsensitiveString cis = (CaseInsensitiveString) o;
63             
64             cis.getFolded();
65             
66             return folded.equals(cis.folded);
67         } catch (ClassCastException e) {
68             try {
69                 String s = (String) o;
70                 
71                 return folded.equals(foldCase(s));
72             } catch (ClassCastException e2) {
73                 return false;
74             }
75         }
76     }
77     
78     /**
79      * Returns the hashCode of this object
80      * @return int hashcode
81      * @stable ICU 2.0
82      */
83     public int hashCode() {
84         getFolded();
85         
86         if (hash == 0) {
87             hash = folded.hashCode();
88         }
89         
90         return hash;
91     }
92     
93     /**
94      * Overrides superclass method
95      * @stable ICU 3.6
96      */
97     public String toString() {
98         return string;
99     }
100 }