]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/util/CaseInsensitiveString.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / util / CaseInsensitiveString.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2013, 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         if (o == null) {
60             return false;
61         }
62         if (this == o) {
63             return true;
64         }
65         if (o instanceof CaseInsensitiveString) {
66             getFolded();
67             CaseInsensitiveString cis = (CaseInsensitiveString) o;
68             cis.getFolded();
69             return folded.equals(cis.folded);
70         }
71         return false;
72     }
73     
74     /**
75      * Returns the hashCode of this object
76      * @return int hashcode
77      * @stable ICU 2.0
78      */
79     public int hashCode() {
80         getFolded();
81         
82         if (hash == 0) {
83             hash = folded.hashCode();
84         }
85         
86         return hash;
87     }
88     
89     /**
90      * Overrides superclass method
91      * @stable ICU 3.6
92      */
93     public String toString() {
94         return string;
95     }
96 }