From 609ad0e57257a838b8f711ed766c9440ecbb565a Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Fri, 31 May 2013 17:01:21 +0200 Subject: [PATCH] Changing version semantics to be more readable The Version class had hard to understand semantics when two versions were compared against each other. Sample of the new logic: * V_0_20_0.before(V_0_90_0) => true * V_0_90_0.after(V_0_20_0) => true Closes #3124 --- src/main/java/org/elasticsearch/Version.java | 18 ++++++------- .../action/mlt/MoreLikeThisRequest.java | 4 +-- .../elasticsearch/test/unit/VersionTests.java | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/elasticsearch/Version.java b/src/main/java/org/elasticsearch/Version.java index 6c06e53e9fe..83a801ea4d5 100644 --- a/src/main/java/org/elasticsearch/Version.java +++ b/src/main/java/org/elasticsearch/Version.java @@ -261,21 +261,21 @@ public class Version implements Serializable { } public boolean after(Version version) { - return version.id > id; - } - - public boolean onOrAfter(Version version) { - return version.id >= id; - } - - public boolean before(Version version) { return version.id < id; } - public boolean onOrBefore(Version version) { + public boolean onOrAfter(Version version) { return version.id <= id; } + public boolean before(Version version) { + return version.id > id; + } + + public boolean onOrBefore(Version version) { + return version.id >= id; + } + /** * Just the version number (without -SNAPSHOT if snapshot). */ diff --git a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java index f7bdb4729c2..9affa0d1f0b 100644 --- a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java +++ b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java @@ -595,7 +595,7 @@ public class MoreLikeThisRequest extends ActionRequest { searchSize = in.readVInt(); searchFrom = in.readVInt(); - if (Version.V_0_90_1.onOrAfter(in.getVersion())) { + if (in.getVersion().onOrAfter(Version.V_0_90_1)) { routing = in.readOptionalString(); } } @@ -665,7 +665,7 @@ public class MoreLikeThisRequest extends ActionRequest { out.writeVInt(searchSize); out.writeVInt(searchFrom); - if (Version.V_0_90_1.onOrAfter(out.getVersion())) { + if (out.getVersion().onOrAfter(Version.V_0_90_1)) { out.writeOptionalString(routing); } } diff --git a/src/test/java/org/elasticsearch/test/unit/VersionTests.java b/src/test/java/org/elasticsearch/test/unit/VersionTests.java index 4b8cf4a6048..0b418352650 100644 --- a/src/test/java/org/elasticsearch/test/unit/VersionTests.java +++ b/src/test/java/org/elasticsearch/test/unit/VersionTests.java @@ -19,6 +19,32 @@ package org.elasticsearch.test.unit; +import org.testng.annotations.Test; + +import static org.elasticsearch.Version.V_0_20_0; +import static org.elasticsearch.Version.V_0_90_0; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + public class VersionTests { + @Test + public void testVersions() throws Exception { + assertThat(V_0_20_0.before(V_0_90_0), is(true)); + assertThat(V_0_20_0.before(V_0_20_0), is(false)); + assertThat(V_0_90_0.before(V_0_20_0), is(false)); + + assertThat(V_0_20_0.onOrBefore(V_0_90_0), is(true)); + assertThat(V_0_20_0.onOrBefore(V_0_20_0), is(true)); + assertThat(V_0_90_0.onOrBefore(V_0_20_0), is(false)); + + assertThat(V_0_20_0.after(V_0_90_0), is(false)); + assertThat(V_0_20_0.after(V_0_20_0), is(false)); + assertThat(V_0_90_0.after(V_0_20_0), is(true)); + + assertThat(V_0_20_0.onOrAfter(V_0_90_0), is(false)); + assertThat(V_0_20_0.onOrAfter(V_0_20_0), is(true)); + assertThat(V_0_90_0.onOrAfter(V_0_20_0), is(true)); + } + } \ No newline at end of file