Move TimeValue into elasticsearch-core project (#29486)
This commit moves the `TimeValue` class into the elasticsearch-core project. This allows us to use this class in many of our other projects without relying on the entire `server` jar. Relates to #28504
This commit is contained in:
parent
0aa19186ae
commit
14097359a4
|
@ -19,15 +19,12 @@
|
|||
|
||||
package org.elasticsearch.common.unit;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ToXContentFragment;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimeValue implements Comparable<TimeValue>, ToXContentFragment {
|
||||
public class TimeValue implements Comparable<TimeValue> {
|
||||
|
||||
/** How many nano-seconds in one milli-second */
|
||||
public static final long NSEC_PER_MSEC = TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS);
|
||||
|
@ -352,9 +349,4 @@ public class TimeValue implements Comparable<TimeValue>, ToXContentFragment {
|
|||
double otherValue = ((double) timeValue.duration) * timeValue.timeUnit.toNanos(1);
|
||||
return Double.compare(thisValue, otherValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
return builder.value(toString());
|
||||
}
|
||||
}
|
|
@ -19,15 +19,10 @@
|
|||
|
||||
package org.elasticsearch.common.unit;
|
||||
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.elasticsearch.common.unit.TimeValue.timeValueNanos;
|
||||
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
@ -154,31 +149,6 @@ public class TimeValueTests extends ESTestCase {
|
|||
return randomFrom("nanos", "micros", "ms", "s", "m", "h", "d");
|
||||
}
|
||||
|
||||
private void assertEqualityAfterSerialize(TimeValue value, int expectedSize) throws IOException {
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
out.writeTimeValue(value);
|
||||
assertEquals(expectedSize, out.size());
|
||||
|
||||
StreamInput in = out.bytes().streamInput();
|
||||
TimeValue inValue = in.readTimeValue();
|
||||
|
||||
assertThat(inValue, equalTo(value));
|
||||
assertThat(inValue.duration(), equalTo(value.duration()));
|
||||
assertThat(inValue.timeUnit(), equalTo(value.timeUnit()));
|
||||
}
|
||||
|
||||
public void testSerialize() throws Exception {
|
||||
assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS), 3);
|
||||
assertEqualityAfterSerialize(timeValueNanos(-1), 2);
|
||||
assertEqualityAfterSerialize(timeValueNanos(1), 2);
|
||||
assertEqualityAfterSerialize(timeValueSeconds(30), 2);
|
||||
|
||||
final TimeValue timeValue = new TimeValue(randomIntBetween(0, 1024), randomFrom(TimeUnit.values()));
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
out.writeZLong(timeValue.duration());
|
||||
assertEqualityAfterSerialize(timeValue, 1 + out.bytes().length());
|
||||
}
|
||||
|
||||
public void testFailOnUnknownUnits() {
|
||||
try {
|
||||
TimeValue.parseTimeValue("23tw", null, "test");
|
|
@ -61,6 +61,7 @@ public class XContentElasticsearchExtension implements XContentBuilderExtension
|
|||
writers.put(FixedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
|
||||
writers.put(MutableDateTime.class, XContentBuilder::timeValue);
|
||||
writers.put(DateTime.class, XContentBuilder::timeValue);
|
||||
writers.put(TimeValue.class, (b, v) -> b.value(v.toString()));
|
||||
|
||||
writers.put(BytesReference.class, (b, v) -> {
|
||||
if (v == null) {
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.common.bytes.BytesArray;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.geo.GeoPoint;
|
||||
import org.elasticsearch.common.lucene.BytesRefs;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.util.BigArrays;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
@ -41,6 +42,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
@ -812,4 +814,30 @@ public class BytesStreamsTests extends ESTestCase {
|
|||
}
|
||||
assertEquals(0, input.available());
|
||||
}
|
||||
|
||||
private void assertEqualityAfterSerialize(TimeValue value, int expectedSize) throws IOException {
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
out.writeTimeValue(value);
|
||||
assertEquals(expectedSize, out.size());
|
||||
|
||||
StreamInput in = out.bytes().streamInput();
|
||||
TimeValue inValue = in.readTimeValue();
|
||||
|
||||
assertThat(inValue, equalTo(value));
|
||||
assertThat(inValue.duration(), equalTo(value.duration()));
|
||||
assertThat(inValue.timeUnit(), equalTo(value.timeUnit()));
|
||||
}
|
||||
|
||||
public void testTimeValueSerialize() throws Exception {
|
||||
assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS), 3);
|
||||
assertEqualityAfterSerialize(TimeValue.timeValueNanos(-1), 2);
|
||||
assertEqualityAfterSerialize(TimeValue.timeValueNanos(1), 2);
|
||||
assertEqualityAfterSerialize(TimeValue.timeValueSeconds(30), 2);
|
||||
|
||||
final TimeValue timeValue = new TimeValue(randomIntBetween(0, 1024), randomFrom(TimeUnit.values()));
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
out.writeZLong(timeValue.duration());
|
||||
assertEqualityAfterSerialize(timeValue, 1 + out.bytes().length());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue