1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /** 4 ******************************************************************************* 5 * Copyright (C) 2001-2013, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.util; 10 11 import com.ibm.icu.lang.UCharacter; 12 13 /** 14 * A string used as a key in java.util.Hashtable and other 15 * collections. It retains case information, but its equals() and 16 * hashCode() methods ignore case. 17 * @stable ICU 2.0 18 */ 19 public class CaseInsensitiveString { 20 21 private String string; 22 23 private int hash = 0; 24 25 private String folded = null; 26 27 private static String foldCase(String foldee) 28 { 29 return UCharacter.foldCase(foldee, true); 30 } 31 32 private void getFolded() 33 { 34 if (folded == null) { 35 folded = foldCase(string); 36 } 37 } 38 39 /** 40 * Constructs an CaseInsentiveString object from the given string 41 * @param s The string to construct this object from 42 * @stable ICU 2.0 43 */ 44 public CaseInsensitiveString(String s) { 45 string = s; 46 } 47 /** 48 * returns the underlying string 49 * @return String 50 * @stable ICU 2.0 51 */ 52 public String getString() { 53 return string; 54 } 55 /** 56 * Compare the object with this 57 * @param o Object to compare this object with 58 * @stable ICU 2.0 59 */ 60 public boolean equals(Object o) { 61 if (o == null) { 62 return false; 63 } 64 if (this == o) { 65 return true; 66 } 67 if (o instanceof CaseInsensitiveString) { 68 getFolded(); 69 CaseInsensitiveString cis = (CaseInsensitiveString) o; 70 cis.getFolded(); 71 return folded.equals(cis.folded); 72 } 73 return false; 74 } 75 76 /** 77 * Returns the hashCode of this object 78 * @return int hashcode 79 * @stable ICU 2.0 80 */ 81 public int hashCode() { 82 getFolded(); 83 84 if (hash == 0) { 85 hash = folded.hashCode(); 86 } 87 88 return hash; 89 } 90 91 /** 92 * Overrides superclass method 93 * @stable ICU 3.6 94 */ 95 public String toString() { 96 return string; 97 } 98 } 99