2121
2222import org .apache .lucene .util .Bits ;
2323import org .apache .lucene .util .SloppyMath ;
24+ import org .elasticsearch .common .io .stream .StreamInput ;
25+ import org .elasticsearch .common .io .stream .StreamOutput ;
26+ import org .elasticsearch .common .io .stream .Writeable ;
2427import org .elasticsearch .common .unit .DistanceUnit ;
2528import org .elasticsearch .index .fielddata .FieldData ;
2629import org .elasticsearch .index .fielddata .GeoPointValues ;
2730import org .elasticsearch .index .fielddata .MultiGeoPointValues ;
2831import org .elasticsearch .index .fielddata .NumericDoubleValues ;
2932import org .elasticsearch .index .fielddata .SortedNumericDoubleValues ;
3033import org .elasticsearch .index .fielddata .SortingNumericDoubleValues ;
31-
34+ import java . io . IOException ;
3235import java .util .Locale ;
3336
3437/**
3538 * Geo distance calculation.
3639 */
37- public enum GeoDistance {
40+ public enum GeoDistance implements Writeable < GeoDistance > {
3841 /**
3942 * Calculates distance as points on a plane. Faster, but less accurate than {@link #ARC}.
4043 */
41- PLANE () {
44+ PLANE {
4245 @ Override
4346 public double calculate (double sourceLatitude , double sourceLongitude , double targetLatitude , double targetLongitude , DistanceUnit unit ) {
4447 double px = targetLongitude - sourceLongitude ;
@@ -60,7 +63,7 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
6063 /**
6164 * Calculates distance factor.
6265 */
63- FACTOR () {
66+ FACTOR {
6467 @ Override
6568 public double calculate (double sourceLatitude , double sourceLongitude , double targetLatitude , double targetLongitude , DistanceUnit unit ) {
6669 double longitudeDifference = targetLongitude - sourceLongitude ;
@@ -82,7 +85,7 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
8285 /**
8386 * Calculates distance as points on a globe.
8487 */
85- ARC () {
88+ ARC {
8689 @ Override
8790 public double calculate (double sourceLatitude , double sourceLongitude , double targetLatitude , double targetLongitude , DistanceUnit unit ) {
8891 double x1 = sourceLatitude * Math .PI / 180D ;
@@ -109,7 +112,7 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
109112 * Calculates distance as points on a globe in a sloppy way. Close to the pole areas the accuracy
110113 * of this function decreases.
111114 */
112- SLOPPY_ARC () {
115+ SLOPPY_ARC {
113116
114117 @ Override
115118 public double normalize (double distance , DistanceUnit unit ) {
@@ -127,12 +130,31 @@ public FixedSourceDistance fixedSourceDistance(double sourceLatitude, double sou
127130 }
128131 };
129132
133+ /** Returns a GeoDistance object as read from the StreamInput. */
134+ @ Override
135+ public GeoDistance readFrom (StreamInput in ) throws IOException {
136+ int ord = in .readVInt ();
137+ if (ord < 0 || ord >= values ().length ) {
138+ throw new IOException ("Unknown GeoDistance ordinal [" + ord + "]" );
139+ }
140+ return GeoDistance .values ()[ord ];
141+ }
142+
143+ public static GeoDistance readGeoDistanceFrom (StreamInput in ) throws IOException {
144+ return DEFAULT .readFrom (in );
145+ }
146+
147+ @ Override
148+ public void writeTo (StreamOutput out ) throws IOException {
149+ out .writeVInt (this .ordinal ());
150+ }
151+
130152 /**
131153 * Default {@link GeoDistance} function. This method should be used, If no specific function has been selected.
132154 * This is an alias for <code>SLOPPY_ARC</code>
133155 */
134- public static final GeoDistance DEFAULT = SLOPPY_ARC ;
135-
156+ public static final GeoDistance DEFAULT = SLOPPY_ARC ;
157+
136158 public abstract double normalize (double distance , DistanceUnit unit );
137159
138160 public abstract double calculate (double sourceLatitude , double sourceLongitude , double targetLatitude , double targetLongitude , DistanceUnit unit );
@@ -180,14 +202,14 @@ public static DistanceBoundingCheck distanceBoundingCheck(double sourceLatitude,
180202
181203 /**
182204 * Get a {@link GeoDistance} according to a given name. Valid values are
183- *
205+ *
184206 * <ul>
185207 * <li><b>plane</b> for <code>GeoDistance.PLANE</code></li>
186208 * <li><b>sloppy_arc</b> for <code>GeoDistance.SLOPPY_ARC</code></li>
187209 * <li><b>factor</b> for <code>GeoDistance.FACTOR</code></li>
188210 * <li><b>arc</b> for <code>GeoDistance.ARC</code></li>
189211 * </ul>
190- *
212+ *
191213 * @param name name of the {@link GeoDistance}
192214 * @return a {@link GeoDistance}
193215 */
@@ -336,7 +358,7 @@ public double calculate(double targetLatitude, double targetLongitude) {
336358
337359 /**
338360 * Basic implementation of {@link FixedSourceDistance}. This class keeps the basic parameters for a distance
339- * functions based on a fixed source. Namely latitude, longitude and unit.
361+ * functions based on a fixed source. Namely latitude, longitude and unit.
340362 */
341363 public static abstract class FixedSourceDistanceBase implements FixedSourceDistance {
342364 protected final double sourceLatitude ;
@@ -349,7 +371,7 @@ public FixedSourceDistanceBase(double sourceLatitude, double sourceLongitude, Di
349371 this .unit = unit ;
350372 }
351373 }
352-
374+
353375 public static class ArcFixedSourceDistance extends FixedSourceDistanceBase {
354376
355377 public ArcFixedSourceDistance (double sourceLatitude , double sourceLongitude , DistanceUnit unit ) {
0 commit comments