Package org.opensearch.common.unit
Enum DistanceUnit
- All Implemented Interfaces:
Serializable
,Comparable<DistanceUnit>
,org.opensearch.core.common.io.stream.Writeable
public enum DistanceUnit
extends Enum<DistanceUnit>
implements org.opensearch.core.common.io.stream.Writeable
The DistanceUnit enumerates several units for measuring distances. These units
provide methods for converting strings and methods to convert units among each
others. Some methods like
getEarthCircumference()
refer to
the earth ellipsoid defined in GeoUtils
. The default unit used within
this project is METERS
which is defined by DEFAULT
- Opensearch.internal:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
This class implements a value+unit tuple.Nested classes/interfaces inherited from interface org.opensearch.core.common.io.stream.Writeable
org.opensearch.core.common.io.stream.Writeable.Reader<V>, org.opensearch.core.common.io.stream.Writeable.WriteableRegistry, org.opensearch.core.common.io.stream.Writeable.Writer<V>
-
Enum Constant Summary
Enum ConstantDescription -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble
convert
(double distance, DistanceUnit unit) Convert a given value into another unitstatic double
convert
(double distance, DistanceUnit from, DistanceUnit to) Converts the given distance from the given DistanceUnit, to the given DistanceUnitdouble
fromMeters
(double distance) Convert a value given in meters to a value of this unitstatic DistanceUnit
fromString
(String unit) Convert a String to aDistanceUnit
double
Measures a longitude in this unitdouble
Measures the circumference of earth in this unitdouble
Measures the radius of earth in this unitdouble
parse
(String distance, DistanceUnit defaultUnit) Parses a given distance and converts it to this unit.static double
parse
(String distance, DistanceUnit defaultUnit, DistanceUnit to) Parses a given distance and converts it to the specified unit.static DistanceUnit
parseUnit
(String distance, DistanceUnit defaultUnit) Parses the suffix of a given distance string and return the correspondingDistanceUnit
static DistanceUnit
readFromStream
(org.opensearch.core.common.io.stream.StreamInput in) Read aDistanceUnit
from aStreamInput
.double
toMeters
(double distance) Convert a value into meterstoString()
toString
(double distance) Convert a value to a distance stringstatic DistanceUnit
Returns the enum constant of this type with the specified name.static DistanceUnit[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.void
writeTo
(org.opensearch.core.common.io.stream.StreamOutput out) Write aDistanceUnit
to aStreamOutput
.
-
Enum Constant Details
-
INCH
-
YARD
-
FEET
-
KILOMETERS
-
NAUTICALMILES
-
MILLIMETERS
-
CENTIMETERS
-
MILES
-
METERS
-
-
Field Details
-
DEFAULT
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
getEarthCircumference
public double getEarthCircumference()Measures the circumference of earth in this unit- Returns:
- length of earth circumference in this unit
-
getEarthRadius
public double getEarthRadius()Measures the radius of earth in this unit- Returns:
- length of earth radius in this unit
-
getDistancePerDegree
public double getDistancePerDegree()Measures a longitude in this unit- Returns:
- length of a longitude degree in this unit
-
toMeters
public double toMeters(double distance) Convert a value into meters- Parameters:
distance
- distance in this unit- Returns:
- value in meters
-
fromMeters
public double fromMeters(double distance) Convert a value given in meters to a value of this unit- Parameters:
distance
- distance in meters- Returns:
- value in this unit
-
convert
Convert a given value into another unit- Parameters:
distance
- value in this unitunit
- source unit- Returns:
- value in this unit
-
toString
Convert a value to a distance string- Parameters:
distance
- value to convert- Returns:
- String representation of the distance
-
toString
- Overrides:
toString
in classEnum<DistanceUnit>
-
convert
Converts the given distance from the given DistanceUnit, to the given DistanceUnit- Parameters:
distance
- Distance to convertfrom
- Unit to convert the distance fromto
- Unit of distance to convert to- Returns:
- Given distance converted to the distance in the given unit
-
parse
Parses a given distance and converts it to the specified unit.- Parameters:
distance
- String defining a distance (value and unit)defaultUnit
- unit assumed if none is definedto
- unit of result- Returns:
- parsed distance
-
parse
Parses a given distance and converts it to this unit.- Parameters:
distance
- String defining a distance (value and unit)defaultUnit
- unit to expect if none if provided- Returns:
- parsed distance
-
fromString
Convert a String to aDistanceUnit
- Parameters:
unit
- name of the unit- Returns:
- unit matching the given name
- Throws:
IllegalArgumentException
- if no unit matches the given name
-
parseUnit
Parses the suffix of a given distance string and return the correspondingDistanceUnit
- Parameters:
distance
- string representing a distancedefaultUnit
- default unit to use, if no unit is provided by the string- Returns:
- unit of the given distance
-
readFromStream
public static DistanceUnit readFromStream(org.opensearch.core.common.io.stream.StreamInput in) throws IOException Read aDistanceUnit
from aStreamInput
.- Parameters:
in
-StreamInput
to read theDistanceUnit
from- Returns:
DistanceUnit
read from theStreamInput
- Throws:
IOException
- if no unit can be read from theStreamInput
IllegalArgumentException
- if no matchingDistanceUnit
can be found
-
writeTo
Write aDistanceUnit
to aStreamOutput
.- Specified by:
writeTo
in interfaceorg.opensearch.core.common.io.stream.Writeable
- Parameters:
out
-StreamOutput
to write to- Throws:
IOException
-