# HG changeset patch # User dirk # Date 1316235941 -7200 # Node ID c621a437ace5750278aecb35626147f007dc8e61 # Parent 27bd115e171cf208a5536d04b64b009045c882e9 implement hashCode and equals for Difference diff -r 27bd115e171c -r c621a437ace5 conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java --- 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; + } } diff -r 27bd115e171c -r c621a437ace5 conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java --- /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))); + } +}