ICU-5073 Use UCharacter.foldCase for equals() method as well as hashCode(). Split case folding out to a static method, cache case folded string.
X-SVN-Rev: 21574
This commit is contained in:
parent
1f1a5deb79
commit
9e6566c584
@ -19,6 +19,14 @@ public class CaseInsensitiveString {
|
|||||||
private String string;
|
private String string;
|
||||||
|
|
||||||
private int hash = 0;
|
private int hash = 0;
|
||||||
|
|
||||||
|
private String folded = null;
|
||||||
|
|
||||||
|
private static String fold(String foldee)
|
||||||
|
{
|
||||||
|
return UCharacter.foldCase(foldee, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an CaseInsentiveString object from the given string
|
* Constructs an CaseInsentiveString object from the given string
|
||||||
* @param s The string to construct this object from
|
* @param s The string to construct this object from
|
||||||
@ -41,11 +49,15 @@ public class CaseInsensitiveString {
|
|||||||
* @stable ICU 2.0
|
* @stable ICU 2.0
|
||||||
*/
|
*/
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
|
if (folded == null) {
|
||||||
|
folded = fold(string);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return string.equalsIgnoreCase(((CaseInsensitiveString)o).string);
|
return folded.equals(fold(((CaseInsensitiveString)o).string));
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
try {
|
try {
|
||||||
return string.equalsIgnoreCase((String)o);
|
return folded.equals(fold((String) o));
|
||||||
} catch (ClassCastException e2) {
|
} catch (ClassCastException e2) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -58,11 +70,17 @@ public class CaseInsensitiveString {
|
|||||||
* @stable ICU 2.0
|
* @stable ICU 2.0
|
||||||
*/
|
*/
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
if (hash == 0) {
|
if (folded == null) {
|
||||||
hash = UCharacter.foldCase(string, true).hashCode();
|
folded = fold(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hash == 0) {
|
||||||
|
hash = folded.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overrides superclass method
|
* Overrides superclass method
|
||||||
* @stable ICU 3.6
|
* @stable ICU 3.6
|
||||||
|
Loading…
Reference in New Issue
Block a user