Commit cf0482c2 authored by Nico Mack's avatar Nico Mack

Added parseAngle method to AngleUtils class

parent cd241985
package lu.list.itis.dkd.tui.utility;
// ***************************************************************************
// * Enum Definition and Members *
// ***************************************************************************
public enum AngleUnit {
DEG(1), RAD((2 * Math.PI) / 360);
public final double factor;
// ***************************************************************************
// * Constants *
// ***************************************************************************
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
private AngleUnit(double factor) {
this.factor = factor;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
public double convertTo(double value, AngleUnit target) {
double normalized = value / this.factor;
return value * target.factor;
}
}
\ No newline at end of file
......@@ -16,6 +16,11 @@
*/
package lu.list.itis.dkd.tui.utility;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author mack
* @since [major].[minor]
......@@ -37,6 +42,15 @@ public class AngleUtils {
public static final double ONE_EIGHTY = 180d;
public static final double NINETY = 90d;
private static final Pattern ANGLE_PATTERN = Pattern.compile("([+-]?\\d+\\.?\\d*)\\s*(rad|deg)?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static Map<String, AngleUnit> LOOKUP_TABLE = new HashMap<>();
static {
LOOKUP_TABLE.put("rad", AngleUnit.RAD);
LOOKUP_TABLE.put("deg", AngleUnit.DEG);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
......@@ -143,4 +157,23 @@ public class AngleUtils {
return quadrant;
}
// ---------------------------------------------------------------------------
/**
* @param angleWithUnit
* @param targetUnit
* @return
*/
// ---------------------------------------------------------------------------
public static double parseAngle(String angleWithUnit, AngleUnit targetUnit) {
double value = Double.NaN;
Matcher areaMatcher = ANGLE_PATTERN.matcher(angleWithUnit);
if (areaMatcher.matches()) {
value = Double.parseDouble(areaMatcher.group(1));
AngleUnit angleUnit = LOOKUP_TABLE.get(areaMatcher.group(2));
value = angleUnit.convertTo(value, targetUnit);
}
return value;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment