From e15d4d1124fb407e8ba003bee083a899a47e6cde Mon Sep 17 00:00:00 2001 From: Matthias Wahl Date: Fri, 9 Jan 2015 11:23:56 +0100 Subject: [PATCH] Internal: Fix equality check of timevalue after serialization closes #9218 --- .../elasticsearch/common/unit/TimeValue.java | 14 +++++------- .../common/unit/TimeValueTests.java | 22 ++++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/unit/TimeValue.java b/src/main/java/org/elasticsearch/common/unit/TimeValue.java index 5ae3a1cbfa2..6ad1bbc0d2e 100644 --- a/src/main/java/org/elasticsearch/common/unit/TimeValue.java +++ b/src/main/java/org/elasticsearch/common/unit/TimeValue.java @@ -268,6 +268,9 @@ public class TimeValue implements Serializable, Streamable { return timeValue; } + /** + * serialization converts TimeValue internally to NANOSECONDS + */ @Override public void readFrom(StreamInput in) throws IOException { duration = in.readLong(); @@ -285,17 +288,12 @@ public class TimeValue implements Serializable, Streamable { if (o == null || getClass() != o.getClass()) return false; TimeValue timeValue = (TimeValue) o; - - if (duration != timeValue.duration) return false; - if (timeUnit != timeValue.timeUnit) return false; - - return true; + return timeUnit.toNanos(duration) == timeValue.timeUnit.toNanos(timeValue.duration); } @Override public int hashCode() { - int result = (int) (duration ^ (duration >>> 32)); - result = 31 * result + (timeUnit != null ? timeUnit.hashCode() : 0); - return result; + long normalized = timeUnit.toNanos(duration); + return (int) (normalized ^ (normalized >>> 32)); } } diff --git a/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java b/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java index 6ca424a27a1..371c5b8e231 100644 --- a/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java +++ b/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java @@ -19,13 +19,15 @@ package org.elasticsearch.common.unit; +import org.elasticsearch.common.io.stream.BytesStreamInput; +import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.test.ElasticsearchTestCase; import org.joda.time.PeriodType; import org.junit.Test; +import java.io.IOException; import java.util.concurrent.TimeUnit; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.lessThan; @@ -66,4 +68,22 @@ public class TimeValueTests extends ElasticsearchTestCase { public void testMinusOne() { assertThat(new TimeValue(-1).nanos(), lessThan(0l)); } + + private void assertEqualityAfterSerialize(TimeValue value) throws IOException { + BytesStreamOutput out = new BytesStreamOutput(); + value.writeTo(out); + + BytesStreamInput in = new BytesStreamInput(out.bytes()); + TimeValue inValue = TimeValue.readTimeValue(in); + + assertThat(inValue, equalTo(value)); + } + + @Test + public void testSerialize() throws Exception { + assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS)); + assertEqualityAfterSerialize(new TimeValue(-1)); + assertEqualityAfterSerialize(new TimeValue(1, TimeUnit.NANOSECONDS)); + + } }