JAL-1803 extracted class ChainId to jalview.util.CaseInsensitiveString,
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 13 Sep 2016 14:03:51 +0000 (15:03 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 13 Sep 2016 14:03:51 +0000 (15:03 +0100)
tightened equals(), hashcode(), tests

src/jalview/datamodel/PDBEntry.java
src/jalview/util/CaseInsensitiveString.java [new file with mode: 0644]
test/jalview/datamodel/PDBEntryTest.java
test/jalview/util/CaseInsensitiveStringTest.java [new file with mode: 0644]

index 1c1d192..198b4a6 100755 (executable)
@@ -20,6 +20,8 @@
  */
 package jalview.datamodel;
 
+import jalview.util.CaseInsensitiveString;
+
 import java.util.Hashtable;
 
 public class PDBEntry
@@ -63,31 +65,6 @@ public class PDBEntry
     }
   }
 
-  public final class ChainId
-  {
-    String chainId;
-
-    public ChainId(String chainId)
-    {
-      this.chainId = chainId;
-    }
-
-    @Override
-    public String toString()
-    {
-      return chainId;
-    }
-
-    @Override
-    public boolean equals(Object o){
-      if (o==null)
-      {
-        return false;
-      }
-      return chainId.equalsIgnoreCase(o.toString());
-    }
-  }
-
   /**
    * constant for storing chain code in properties table
    */
@@ -237,7 +214,7 @@ public class PDBEntry
       return;
     }
     // update property for non-null chainCode
-    properties.put(CHAIN_ID, new ChainId(chainCode));
+    properties.put(CHAIN_ID, new CaseInsensitiveString(chainCode));
   }
 
   @Override
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();
+  }
+}
index 42bb091..1195393 100644 (file)
  */
 package jalview.datamodel;
 
-import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertNull;
 
+//import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -40,52 +44,60 @@ public class PDBEntryTest
   }
 
   @Test(groups = { "Functional" })
-  public void test()
+  public void testEquals()
   {
-    try
-    {
+    PDBEntry pdbEntry = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB,
+            "x/y/z/File");
 
-      PDBEntry pdbEntry = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB,
-              "x/y/z/File");
+    PDBEntry case1 = new PDBEntry("1XYZ", "A", PDBEntry.Type.PDB,
+            "x/y/z/File");
+    PDBEntry case2 = new PDBEntry("1xyz", "a", PDBEntry.Type.PDB,
+            "x/y/z/File");
+    PDBEntry case3 = new PDBEntry("1xyz", "A", PDBEntry.Type.FILE,
+            "x/y/z/File");
+    PDBEntry case4 = new PDBEntry(null, null, null, null);
+    PDBEntry case5 = new PDBEntry(null, "A", PDBEntry.Type.PDB,
+            "x/y/z/File");
+    PDBEntry case6 = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
+            "x/y/z/File");
+    PDBEntry case7 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
+    PDBEntry case8 = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB, null);
+    PDBEntry case9 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
 
-      PDBEntry case1 = new PDBEntry("1XYZ", "A", PDBEntry.Type.PDB,
-              "x/y/z/File");
-      PDBEntry case2 = new PDBEntry("1xyz", "a", PDBEntry.Type.PDB,
-              "x/y/z/File");
-      PDBEntry case3 = new PDBEntry("1xyz", "A", PDBEntry.Type.FILE,
-              "x/y/z/File");
-      PDBEntry case4 = new PDBEntry(null, null, null, null);
-      PDBEntry case5 = new PDBEntry(null, "A", PDBEntry.Type.PDB,
-              "x/y/z/File");
-      PDBEntry case6 = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
-              "x/y/z/File");
-      PDBEntry case7 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
-      PDBEntry case8 = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB, null);
-      PDBEntry case9 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
-
-      // System.out.println(">>>> Testing case 1");
-      assertTrue(pdbEntry.equals(case1));
-      // System.out.println(">>>> Testing case 2");
-      assertTrue(pdbEntry.equals(case2));
-      // System.out.println(">>>> Testing case 3");
-      assertTrue(!pdbEntry.equals(case3));
-      // System.out.println(">>>> Testing case 4");
-      assertTrue(!pdbEntry.equals(case4));
-      // System.out.println(">>>> Testing case 5");
-      assertTrue(!pdbEntry.equals(case5));
-      // System.out.println(">>>> Testing case 6");
-      assertTrue(!pdbEntry.equals(case6));
-      // System.out.println(">>>> Testing case 7");
-      assertTrue(!pdbEntry.equals(case7));
-      // System.out.println(">>>> Testing case 8");
-      assertTrue(pdbEntry.equals(case8));
-      assertTrue(pdbEntry.equals(case8));
-      assertTrue(case7.equals(case9));
-    } catch (Exception e)
-    {
-      e.printStackTrace();
-    }
+    /*
+     * assertions will invoke PDBEntry.equals()
+     */
+    assertFalse(pdbEntry.equals(null));
+    assertFalse(pdbEntry.equals("a"));
+    assertEquals(case1, pdbEntry);
+    assertEquals(case2, pdbEntry);
+    assertNotEquals(case3, pdbEntry);
+    assertNotEquals(case4, pdbEntry);
+    assertNotEquals(case5, pdbEntry);
+    assertNotEquals(case6, pdbEntry);
+    assertNotEquals(case7, pdbEntry);
+    assertEquals(case8, pdbEntry);
+    assertEquals(case7, case9);
 
+    /*
+     * change string wrapper property to string...
+     */
+    case1.getProperty().put("chain_code", "a");
+    assertFalse(pdbEntry.equals(case1));
+    assertFalse(case1.equals(pdbEntry));
   }
 
+  @Test(groups = { "Functional" })
+  public void testSetChainCode()
+  {
+    PDBEntry pdbEntry = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
+            "x/y/z/File");
+    assertNull(pdbEntry.getChainCode());
+
+    pdbEntry.setChainCode("a");
+    assertEquals("a", pdbEntry.getChainCode());
+
+    pdbEntry.setChainCode(null);
+    assertNull(pdbEntry.getChainCode());
+  }
 }
diff --git a/test/jalview/util/CaseInsensitiveStringTest.java b/test/jalview/util/CaseInsensitiveStringTest.java
new file mode 100644 (file)
index 0000000..9a846e6
--- /dev/null
@@ -0,0 +1,43 @@
+package jalview.util;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertTrue;
+
+import org.testng.annotations.Test;
+
+public class CaseInsensitiveStringTest
+{
+  @Test(groups = "Functional")
+  public void testEquals()
+  {
+    CaseInsensitiveString s1 = new CaseInsensitiveString(null);
+    CaseInsensitiveString s2 = new CaseInsensitiveString("a");
+    CaseInsensitiveString s3 = new CaseInsensitiveString("A");
+    CaseInsensitiveString s4 = new CaseInsensitiveString("b");
+
+    assertFalse(s1.equals(null));
+    assertTrue(s1.equals(s1));
+    assertFalse(s1.equals(s2));
+    assertTrue(s2.equals(s2));
+    assertFalse(s2.equals(s1));
+    assertTrue(s2.equals(s3));
+    assertTrue(s3.equals(s2));
+    assertFalse(s3.equals(s4));
+    assertFalse(s4.equals(s3));
+  }
+
+  @Test(groups = "Functional")
+  public void testHashcode()
+  {
+    CaseInsensitiveString s1 = new CaseInsensitiveString(null);
+    CaseInsensitiveString s2 = new CaseInsensitiveString("a");
+    CaseInsensitiveString s3 = new CaseInsensitiveString("A");
+    CaseInsensitiveString s4 = new CaseInsensitiveString("b");
+
+    assertNotEquals(s1.hashCode(), s2.hashCode());
+    assertEquals(s2.hashCode(), s3.hashCode());
+    assertNotEquals(s3.hashCode(), s4.hashCode());
+  }
+}