changeset 18:c621a437ace5

implement hashCode and equals for Difference
author dirk
date Sat, 17 Sep 2011 07:05:41 +0200
parents 27bd115e171c
children 97a113e27ed8
files conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java
diffstat 2 files changed, 90 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java	Sat Sep 17 06:55:38 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java	Sat Sep 17 07:05:41 2011 +0200
@@ -28,4 +28,67 @@
         buf.append("\"");
         return buf.toString();
     }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((currentValue == null) ? 0 : currentValue.hashCode());
+        result = prime * result + ((key == null) ? 0 : key.hashCode());
+        result = prime * result + ((otherValue == null) ? 0 : otherValue.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        Difference other = (Difference)obj;
+        if (currentValue == null)
+        {
+            if (other.currentValue != null)
+            {
+                return false;
+            }
+        }
+        else if (!currentValue.equals(other.currentValue))
+        {
+            return false;
+        }
+        if (key == null)
+        {
+            if (other.key != null)
+            {
+                return false;
+            }
+        }
+        else if (!key.equals(other.key))
+        {
+            return false;
+        }
+        if (otherValue == null)
+        {
+            if (other.otherValue != null)
+            {
+                return false;
+            }
+        }
+        else if (!otherValue.equals(other.otherValue))
+        {
+            return false;
+        }
+        return true;
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java	Sat Sep 17 07:05:41 2011 +0200
@@ -0,0 +1,27 @@
+
+package de.codedo.conflicteditor;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import org.junit.Test;
+
+public class DifferenceTestCase extends Object
+{
+    @Test
+    public void differencesWithSameValuesShouldBeEqual()
+    {
+        Difference diff1 = new Difference("key", "current", "other");
+        Difference diff2 = new Difference("key", "current", "other");
+        assertThat(diff1, equalTo(diff2));
+    }
+
+    @Test
+    public void differencesWithDifferentValuesShouldNotBeEqual()
+    {
+        Difference diff1 = new Difference("key", "current", "other");
+        Difference diff2 = new Difference("other-key", "current", "other");
+        assertThat(diff1, not(equalTo(diff2)));
+    }
+}