File tree Expand file tree Collapse file tree 2 files changed +27
-9
lines changed
main/java/org/elasticsearch/common/unit
test/java/org/elasticsearch/common/unit Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -268,6 +268,9 @@ public static TimeValue readTimeValue(StreamInput in) throws IOException {
268268 return timeValue ;
269269 }
270270
271+ /**
272+ * serialization converts TimeValue internally to NANOSECONDS
273+ */
271274 @ Override
272275 public void readFrom (StreamInput in ) throws IOException {
273276 duration = in .readLong ();
@@ -285,17 +288,12 @@ public boolean equals(Object o) {
285288 if (o == null || getClass () != o .getClass ()) return false ;
286289
287290 TimeValue timeValue = (TimeValue ) o ;
288-
289- if (duration != timeValue .duration ) return false ;
290- if (timeUnit != timeValue .timeUnit ) return false ;
291-
292- return true ;
291+ return timeUnit .toNanos (duration ) == timeValue .timeUnit .toNanos (timeValue .duration );
293292 }
294293
295294 @ Override
296295 public int hashCode () {
297- int result = (int ) (duration ^ (duration >>> 32 ));
298- result = 31 * result + (timeUnit != null ? timeUnit .hashCode () : 0 );
299- return result ;
296+ long normalized = timeUnit .toNanos (duration );
297+ return (int ) (normalized ^ (normalized >>> 32 ));
300298 }
301299}
Original file line number Diff line number Diff line change 1919
2020package org .elasticsearch .common .unit ;
2121
22+ import org .elasticsearch .common .io .stream .BytesStreamInput ;
23+ import org .elasticsearch .common .io .stream .BytesStreamOutput ;
2224import org .elasticsearch .test .ElasticsearchTestCase ;
2325import org .joda .time .PeriodType ;
2426import org .junit .Test ;
2527
28+ import java .io .IOException ;
2629import java .util .concurrent .TimeUnit ;
2730
28- import static org .hamcrest .MatcherAssert .assertThat ;
2931import static org .hamcrest .Matchers .equalTo ;
3032import static org .hamcrest .Matchers .lessThan ;
3133
@@ -66,4 +68,22 @@ public void testFormat() {
6668 public void testMinusOne () {
6769 assertThat (new TimeValue (-1 ).nanos (), lessThan (0l ));
6870 }
71+
72+ private void assertEqualityAfterSerialize (TimeValue value ) throws IOException {
73+ BytesStreamOutput out = new BytesStreamOutput ();
74+ value .writeTo (out );
75+
76+ BytesStreamInput in = new BytesStreamInput (out .bytes ());
77+ TimeValue inValue = TimeValue .readTimeValue (in );
78+
79+ assertThat (inValue , equalTo (value ));
80+ }
81+
82+ @ Test
83+ public void testSerialize () throws Exception {
84+ assertEqualityAfterSerialize (new TimeValue (100 , TimeUnit .DAYS ));
85+ assertEqualityAfterSerialize (new TimeValue (-1 ));
86+ assertEqualityAfterSerialize (new TimeValue (1 , TimeUnit .NANOSECONDS ));
87+
88+ }
6989}
You can’t perform that action at this time.
0 commit comments