[jsemver] 05/95: Override equals() & hashCode() methods

Alexandre Viau reazem-guest at moszumanska.debian.org
Mon Feb 16 14:58:24 UTC 2015


This is an automated email from the git hooks/post-receive script.

reazem-guest pushed a commit to branch master
in repository jsemver.

commit d670f15b16482fcb0c1fd311ede237545dfaca6e
Author: Zafar Khaja <zafarkhaja at gmail.com>
Date:   Tue Nov 20 22:17:29 2012 +0400

    Override equals() & hashCode() methods
---
 .../java/com/github/zafarkhaja/semver/Version.java | 26 +++++++++
 .../com/github/zafarkhaja/semver/VersionTest.java  | 64 ++++++++++++++++++++++
 2 files changed, 90 insertions(+)

diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java
index 17cbd3c..ac4c575 100644
--- a/src/main/java/com/github/zafarkhaja/semver/Version.java
+++ b/src/main/java/com/github/zafarkhaja/semver/Version.java
@@ -104,6 +104,32 @@ public class Version implements Comparable<Version> {
     }
     
     @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        if (null == other || this.getClass() != other.getClass()) {
+            return false;
+        }
+        return compareTo((Version) other) == 0 ? true : false;
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = 5;
+        hash = 97 * hash + majorVersion;
+        hash = 97 * hash + minorVersion;
+        hash = 97 * hash + patchVersion;
+        hash = 97 * hash + (
+            preReleaseVersion != null ? preReleaseVersion.hashCode(): 0
+        );
+        hash = 97 * hash + (
+            buildVersion != null ? buildVersion.hashCode() : 0
+        );
+        return hash;
+    }
+    
+    @Override
     public int compareTo(Version other) {
         int result = compareNormalVersions(other);
         if (result == 0 && preReleaseVersion != null) {
diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
index e618002..726acd1 100644
--- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
+++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
@@ -157,4 +157,68 @@ public class VersionTest {
         Version version2 = new Version("1.3.7-beta+build.a");
         assertEquals(-1, version1.compareTo(version2));
     }
+    
+    @Test public void
+    shouldOverrideEqualsMethodForConvenience() {
+        Version version1 = new Version("2.3.7");
+        Version version2 = new Version("2.3.7");
+        Version version3 = new Version("1.3.7");
+        assertTrue(version1.equals(version1));
+        assertTrue(version1.equals(version2));
+        assertFalse(version1.equals(version3));
+    }
+    
+    @Test public void
+    equalsMethodShouldBeReflexive() {
+        Version version1 = new Version("2.3.7");
+        assertTrue(version1.equals(version1));
+    }
+    
+    @Test public void
+    equalsMethodShouldBeSymmetric() {
+        Version version1 = new Version("2.3.7");
+        Version version2 = new Version("2.3.7");
+        assertTrue(version1.equals(version2));
+        assertTrue(version2.equals(version1));
+    }
+    
+    @Test public void
+    equalsMethodShouldBeTransitive() {
+        Version version1 = new Version("2.3.7");
+        Version version2 = new Version("2.3.7");
+        Version version3 = new Version("2.3.7");
+        assertTrue(version1.equals(version2));
+        assertTrue(version2.equals(version3));
+        assertTrue(version1.equals(version3));
+    }
+    
+    @Test public void
+    equalsMethodShouldBeConsistent() {
+        Version version1 = new Version("2.3.7");
+        Version version2 = new Version("2.3.7");
+        assertTrue(version1.equals(version2));
+        assertTrue(version1.equals(version2));
+        assertTrue(version1.equals(version2));
+    }
+    
+    @Test public void
+    equalsMethodShouldReturnFalseIfOtherVersionIsNotInstanceOfVersion() {
+        Version version1 = new Version("2.3.7");
+        assertFalse(version1.equals(new String("2.3.7")));
+    }
+    
+    @Test public void
+    equalsMethodShouldReturnFalseIfOtherVersionIsNull() {
+        Version version1 = new Version("2.3.7");
+        Version version2 = null;
+        assertFalse(version1.equals(version2));
+    }
+    
+    @Test public void
+    hashCodeMethodShouldReturnSameHashCodeIfVersionsAreEqual() {
+        Version version1 = new Version("2.3.7");
+        Version version2 = new Version("2.3.7");
+        assertTrue(version1.equals(version2));
+        assertEquals(version1.hashCode(), version2.hashCode());
+    }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jsemver.git



More information about the pkg-java-commits mailing list