PandaLib/pandalib-util/src/main/java/fr/pandacube/lib/util/DistanceUtil.java
Marc Baloup d411618e63 Fix Javadoc warnings due to Java 21 update (+ some other warnings)
The default implicit constructor must also have a doc comment, so I have to make it explicit and either properly restrict the visibility of the constructor, or actually document it.
2024-06-06 23:59:32 +02:00

111 lines
2.9 KiB
Java

package fr.pandacube.lib.util;
import java.text.DecimalFormat;
import java.util.Arrays;
/**
* This class contains various methods to manipulate and display distances.
*/
public class DistanceUtil {
/**
* Generate a string representation of the provided distance, with a specific decimal precision and one of the
* specified metric prefix
* @param meterDist the distance to display, in meter
* @param precision the number of digit to display after the decimal separator
* @param desiredUnits the preferred unit prefix to use for conversion.
* @return a string representation of the provided distance
*/
public static String distanceToString(double meterDist, int precision, DistanceUnit... desiredUnits) {
Arrays.sort(desiredUnits);
DistanceUnit chosenUnit = desiredUnits[0]; // la plus petite unité
for (DistanceUnit unit : desiredUnits) {
if (meterDist / unit.multiplier < 1) continue;
chosenUnit = unit;
}
if (chosenUnit != desiredUnits[0] && precision <= 2) precision = 2;
String precisionFormat = "##0";
if (precision > 0) precisionFormat += ".";
precisionFormat += "0".repeat(precision);
DecimalFormat df = new DecimalFormat(precisionFormat);
double dist = meterDist / chosenUnit.multiplier;
return df.format(dist) + chosenUnit.unitStr;
}
/**
*
* Generate a string representation of the provided distance, with a specific decimal precision, and using either
* {@code km} or {@code m} as a unit.
* <p>
* Calling this method is equivalent to {@code distanceToString(meterDist, precision, DistanceUnit.M, DistanceUnit.KM)}.
* @param meterDist the distance to display, in meter
* @param precision the number of digit to display after the decimal separator
* @return a string representation of the provided distance
*/
public static String distanceToString(double meterDist, int precision) {
return distanceToString(meterDist, precision, DistanceUnit.M, DistanceUnit.KM);
}
/**
* Enumeration of commonly used distance metric unit
*/
public enum DistanceUnit {
/**
* Nanometer unit. One billionth of a meter. 10<sup>-9</sup> = 0.000000001m.
*/
NM(0.000000001, "nm"),
/**
* Micrometer unit. One millionth of a meter. 10<sup>-6</sup> = 0.000001m.
*/
UM(0.000001, "µm"),
/**
* Millimeter unit. One thousandth of a meter. 10<sup>-3</sup> = 0.001m.
*/
MM(0.001, "mm"),
/**
* Centimeter unit. One hundredth of a meter. 10<sup>-2</sup> = 0.01m
*/
CM(0.01, "cm"),
/**
* Meter unit. One meter. 10<sup>0</sup> = 1m.
*/
M(1, "m"),
/**
* Kilometer unit. One thousand meter. 10<sup>3</sup> = 1000m.
*/
KM(1000, "km");
/**
* The value of this unit in meter.
*/
public final double multiplier;
/**
* String representation of the unit symbol.
*/
public final String unitStr;
DistanceUnit(double multiplier, String s) {
this.multiplier = multiplier;
unitStr = s;
}
}
private DistanceUtil() {}
}