Merge branch 'develop' into features/JAL-2094_colourInterface
[jalview.git] / src / jalview / util / CaseInsensitiveString.java
diff --git a/src/jalview/util/CaseInsensitiveString.java b/src/jalview/util/CaseInsensitiveString.java
new file mode 100644 (file)
index 0000000..aa42228
--- /dev/null
@@ -0,0 +1,57 @@
+package jalview.util;
+
+/**
+ * A class to wrap a case insensitive string. For use in collections where we
+ * want to preserve case, but do not want to duplicate upper and lower case
+ * variants
+ */
+public final class CaseInsensitiveString
+{
+  String value;
+
+  public CaseInsensitiveString(String s)
+  {
+    this.value = s;
+  }
+
+  @Override
+  public String toString()
+  {
+    return value;
+  }
+
+  /**
+   * Answers true if the object compared to is a CaseInsensitiveString wrapping
+   * the same string value (ignoring case), or if both wrap a null value, else
+   * false
+   */
+  @Override
+  public boolean equals(Object o)
+  {
+    if (o == null)
+    {
+      return false;
+    }
+    if (!(o instanceof CaseInsensitiveString))
+    {
+      return false;
+    }
+    CaseInsensitiveString obj = (CaseInsensitiveString) o;
+    if (value == null)
+    {
+      return obj.value == null;
+    }
+    return value.equalsIgnoreCase(obj.value);
+  }
+
+  /**
+   * hashCode overriden to guarantee that 'equal' objects have the same hash
+   * code
+   */
+  @Override
+  public int hashCode()
+  {
+    return value == null ? super.hashCode() : value.toUpperCase()
+            .hashCode();
+  }
+}