aa422281fcb5277f319f9aa02a068cf47491a7ed
[jalview.git] / src / jalview / util / CaseInsensitiveString.java
1 package jalview.util;
2
3 /**
4  * A class to wrap a case insensitive string. For use in collections where we
5  * want to preserve case, but do not want to duplicate upper and lower case
6  * variants
7  */
8 public final class CaseInsensitiveString
9 {
10   String value;
11
12   public CaseInsensitiveString(String s)
13   {
14     this.value = s;
15   }
16
17   @Override
18   public String toString()
19   {
20     return value;
21   }
22
23   /**
24    * Answers true if the object compared to is a CaseInsensitiveString wrapping
25    * the same string value (ignoring case), or if both wrap a null value, else
26    * false
27    */
28   @Override
29   public boolean equals(Object o)
30   {
31     if (o == null)
32     {
33       return false;
34     }
35     if (!(o instanceof CaseInsensitiveString))
36     {
37       return false;
38     }
39     CaseInsensitiveString obj = (CaseInsensitiveString) o;
40     if (value == null)
41     {
42       return obj.value == null;
43     }
44     return value.equalsIgnoreCase(obj.value);
45   }
46
47   /**
48    * hashCode overriden to guarantee that 'equal' objects have the same hash
49    * code
50    */
51   @Override
52   public int hashCode()
53   {
54     return value == null ? super.hashCode() : value.toUpperCase()
55             .hashCode();
56   }
57 }