diff --git a/core/src/main/java/org/elasticsearch/Version.java b/core/src/main/java/org/elasticsearch/Version.java index 474b220a9c5..bac6ccbf3fd 100644 --- a/core/src/main/java/org/elasticsearch/Version.java +++ b/core/src/main/java/org/elasticsearch/Version.java @@ -28,8 +28,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.monitor.jvm.JvmInfo; import java.io.IOException; +import java.util.Comparator; -public class Version { +public class Version implements Comparable { /* * The logic for ID is: XXYYZZAA, where XX is major version, YY is minor version, ZZ is revision, and AA is alpha/beta/rc indicator AA * values below 25 are for alpha builder (since 5.0), and above 25 and below 50 are beta builds, and below 99 are RC builds, with 99 @@ -310,6 +311,11 @@ public class Version { return version.id >= id; } + @Override + public int compareTo(Version other) { + return Integer.compare(this.id, other.id); + } + /** * Returns the minimum compatible version based on the current * version. Ie a node needs to have at least the return version in order diff --git a/core/src/test/java/org/elasticsearch/VersionTests.java b/core/src/test/java/org/elasticsearch/VersionTests.java index 1c9964547b0..35c36bee643 100644 --- a/core/src/test/java/org/elasticsearch/VersionTests.java +++ b/core/src/test/java/org/elasticsearch/VersionTests.java @@ -38,7 +38,10 @@ import static org.elasticsearch.Version.V_5_0_0_alpha1; import static org.elasticsearch.test.VersionUtils.randomVersion; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.sameInstance; public class VersionTests extends ESTestCase { @@ -64,6 +67,10 @@ public class VersionTests extends ESTestCase { assertTrue(Version.fromString("5.0.0").onOrAfter(Version.fromString("5.0.0-beta2"))); assertTrue(Version.fromString("5.0.0-rc1").onOrAfter(Version.fromString("5.0.0-beta24"))); assertTrue(Version.fromString("5.0.0-alpha24").before(Version.fromString("5.0.0-beta0"))); + + assertThat(V_2_2_0, is(lessThan(V_5_0_0_alpha1))); + assertThat(V_2_2_0.compareTo(V_2_2_0), is(0)); + assertThat(V_5_0_0_alpha1, is(greaterThan(V_2_2_0))); } public void testMin() {