summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-07-15 14:29:51 +0000
committerArne Juul <arnej@verizonmedia.com>2020-07-15 15:39:26 +0000
commit4b7e33430e02f1be1cda65cda6fa7e5736bf8fc0 (patch)
treea456fc74ed740d6c5c5fb6ed61f100c09c951c24 /vespajlib
parent6c09dfe6706762122b57c321e7f05e6b0822c62c (diff)
change DistanceParser API, mark as Beta
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/geo/DistanceParser.java27
1 files changed, 24 insertions, 3 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/geo/DistanceParser.java b/vespajlib/src/main/java/com/yahoo/geo/DistanceParser.java
index e184e4c36ba..1ae68afa4ac 100644
--- a/vespajlib/src/main/java/com/yahoo/geo/DistanceParser.java
+++ b/vespajlib/src/main/java/com/yahoo/geo/DistanceParser.java
@@ -2,6 +2,12 @@
package com.yahoo.geo;
+import com.google.common.annotations.Beta;
+
+/**
+ * Utility for parsing a geographical distance with unit.
+ **/
+@Beta
public class DistanceParser {
// according to wikipedia:
// Earth's equatorial radius = 6378137 meter - not used
@@ -13,9 +19,24 @@ public class DistanceParser {
public final static double km2deg = 1000.000 * 180.0 / (Math.PI * 6356752.0);
public final static double mi2deg = 1609.344 * 180.0 / (Math.PI * 6356752.0);
- public final double degrees;
+ private final double degrees;
+
+ public double getDegrees() { return degrees; }
+
+ /**
+ * Parse a distance in some kind of units, converting to geographical degrees.
+ * Note that the number and the unit should be separated by a single space only,
+ * or not separated at all.
+ * Supported units are "m", "km", "miles", and "deg",
+ * the last one meaning degrees with no conversion.
+ * For brevity "mi" = "miles" and "d" = "deg".
+ **/
+ static public double parse(String distance) {
+ var parser = new DistanceParser(distance, false);
+ return parser.degrees;
+ }
- public DistanceParser(String distance, boolean assume_micro_degrees) {
+ DistanceParser(String distance, boolean assumeMicroDegrees) {
if (distance.endsWith(" km")) {
double km = Double.valueOf(distance.substring(0, distance.length()-3));
degrees = km * km2deg;
@@ -48,7 +69,7 @@ public class DistanceParser {
degrees = Double.valueOf(distance.substring(0, distance.length()-3));
} else if (distance.endsWith("d")) {
degrees = Double.valueOf(distance.substring(0, distance.length()-1));
- } else if (assume_micro_degrees) {
+ } else if (assumeMicroDegrees) {
degrees = Integer.parseInt(distance) * 0.000001;
} else {
throw new IllegalArgumentException("missing unit for distance: "+distance);