Make distance unit writable.

Relates to #15178
This commit is contained in:
Isabel Drost-Fromm 2016-01-20 15:01:54 +01:00
parent f6db00fd33
commit d2dac8427e
2 changed files with 43 additions and 1 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.common.unit;
import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import java.io.IOException; import java.io.IOException;
@ -32,7 +33,7 @@ import java.io.IOException;
* the earth ellipsoid defined in {@link GeoUtils}. The default unit used within * the earth ellipsoid defined in {@link GeoUtils}. The default unit used within
* this project is <code>METERS</code> which is defined by <code>DEFAULT</code> * this project is <code>METERS</code> which is defined by <code>DEFAULT</code>
*/ */
public enum DistanceUnit { public enum DistanceUnit implements Writeable<DistanceUnit> {
INCH(0.0254, "in", "inch"), INCH(0.0254, "in", "inch"),
YARD(0.9144, "yd", "yards"), YARD(0.9144, "yd", "yards"),
FEET(0.3048, "ft", "feet"), FEET(0.3048, "ft", "feet"),
@ -322,4 +323,24 @@ public enum DistanceUnit {
return new Distance(Double.parseDouble(distance), defaultUnit); return new Distance(Double.parseDouble(distance), defaultUnit);
} }
} }
private static final DistanceUnit PROTOTYPE = DEFAULT;
@Override
public DistanceUnit readFrom(StreamInput in) throws IOException {
int ordinal = in.readVInt();
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown DistanceUnit ordinal [" + ordinal + "]");
}
return values()[ordinal];
}
public static DistanceUnit readUnitFrom(StreamInput in) throws IOException {
return PROTOTYPE.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(this.ordinal());
}
} }

View File

@ -19,6 +19,10 @@
package org.elasticsearch.common.unit; package org.elasticsearch.common.unit;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.closeTo;
@ -73,4 +77,21 @@ public class DistanceUnitTests extends ESTestCase {
assertEquals(7, DistanceUnit.MILES.ordinal()); assertEquals(7, DistanceUnit.MILES.ordinal());
assertEquals(8, DistanceUnit.METERS.ordinal()); assertEquals(8, DistanceUnit.METERS.ordinal());
} }
public void testReadWrite() throws Exception {
for (DistanceUnit unit : DistanceUnit.values()) {
try (BytesStreamOutput out = new BytesStreamOutput()) {
unit.writeTo(out);
try (StreamInput in = StreamInput.wrap(out.bytes())) {
assertThat("Roundtrip serialisation failed.", DistanceUnit.readDistanceUnit(in), equalTo(unit));
}
}
}
}
public void testFromString() {
for (DistanceUnit unit : DistanceUnit.values()) {
assertThat("Roundtrip string parsing failed.", DistanceUnit.fromString(unit.toString()), equalTo(unit));
}
}
} }