From f6abcf5dd79ea9ee8e076cdeff71644012c1f7ac Mon Sep 17 00:00:00 2001 From: Zafar Khaja Date: Thu, 23 Jul 2015 14:03:04 +0300 Subject: [PATCH] Add methods for checking major and minor compatibility --- .../com/github/zafarkhaja/semver/Version.java | 33 +++++++++++++++++++ .../github/zafarkhaja/semver/VersionTest.java | 18 ++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java index 55c9053..f5076bf 100644 --- a/src/main/java/com/github/zafarkhaja/semver/Version.java +++ b/src/main/java/com/github/zafarkhaja/semver/Version.java @@ -560,6 +560,39 @@ public class Version implements Comparable { return compareTo(other) <= 0; } + /** + * Checks if this version is compatible with the + * other version in terms of their major versions. + * + * When checking compatibility no assumptions + * are made about the versions' precedence. + * + * @param other the other version to check with + * @return {@code true} if this version is compatible with + * the other version or {@code false} otherwise + * @since 0.10.0 + */ + public boolean isMajorVersionCompatible(Version other) { + return this.getMajorVersion() == other.getMajorVersion(); + } + + /** + * Checks if this version is compatible with the + * other version in terms of their minor versions. + * + * When checking compatibility no assumptions + * are made about the versions' precedence. + * + * @param other the other version to check with + * @return {@code true} if this version is compatible with + * the other version or {@code false} otherwise + * @since 0.10.0 + */ + public boolean isMinorVersionCompatible(Version other) { + return this.getMajorVersion() == other.getMajorVersion() + && this.getMinorVersion() == other.getMinorVersion(); + } + /** * Checks if this version equals the other version. * diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java index e7c747a..f9e6a92 100644 --- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java @@ -314,6 +314,24 @@ public class VersionTest { assertTrue(v.satisfies(gte("1.0.0").and(lt("2.0.0")))); assertFalse(v.satisfies(gte("2.0.0").and(lt("3.0.0")))); } + + @Test + public void shouldCheckIfMajorVersionCompatible() { + Version v1 = Version.valueOf("1.0.0"); + Version v2 = Version.valueOf("1.2.3"); + Version v3 = Version.valueOf("2.0.0"); + assertTrue(v1.isMajorVersionCompatible(v2)); + assertFalse(v1.isMajorVersionCompatible(v3)); + } + + @Test + public void shouldCheckIfMinorVersionCompatible() { + Version v1 = Version.valueOf("1.1.1"); + Version v2 = Version.valueOf("1.1.2"); + Version v3 = Version.valueOf("1.2.3"); + assertTrue(v1.isMinorVersionCompatible(v2)); + assertFalse(v1.isMinorVersionCompatible(v3)); + } } public static class EqualsMethodTest {