planetiler/planetiler-core/src/main/java/com/onthegomap/planetiler/util/Parse.java

113 wiersze
3.3 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.util;
2021-04-14 11:20:19 +00:00
2021-06-11 01:15:27 +00:00
import java.util.Map;
2021-06-14 11:04:03 +00:00
import java.util.regex.Pattern;
2021-06-11 01:15:27 +00:00
2021-09-10 00:46:20 +00:00
/**
* Utilities to parse values from strings.
*/
2021-04-14 11:20:19 +00:00
public class Parse {
private Parse() {}
2021-09-10 00:46:20 +00:00
private static final Pattern INT_SUBSTRING_PATTERN = Pattern.compile("^(-?\\d+)(\\D|$)");
private static final Pattern TO_ROUND_INT_SUBSTRING_PATTERN = Pattern.compile("^(-?[\\d.]+)(\\D|$)");
/** Returns {@code tag} as a long or null if invalid. */
2021-06-11 01:15:27 +00:00
public static Long parseLongOrNull(Object tag) {
try {
return tag == null ? null : tag instanceof Number number ? number.longValue() : Long.parseLong(tag.toString());
} catch (NumberFormatException e) {
return null;
}
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} as a long or 0 if invalid. */
2021-06-11 01:15:27 +00:00
public static long parseLong(Object tag) {
try {
return tag == null ? 0 : tag instanceof Number number ? number.longValue() : Long.parseLong(tag.toString());
} catch (NumberFormatException e) {
return 0;
}
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} as an integer or null if invalid, ignoring any non-numeric suffix. */
2021-06-14 11:04:03 +00:00
public static Integer parseIntSubstring(String tag) {
if (tag == null) {
return null;
}
try {
var matcher = INT_SUBSTRING_PATTERN.matcher(tag);
return matcher.find() ? Integer.parseInt(matcher.group(1)) : null;
} catch (NumberFormatException e) {
return null;
2021-08-10 10:55:30 +00:00
}
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} as a number rounded to the nearest integer or null if invalid. */
2021-08-14 09:55:00 +00:00
public static Integer parseRoundInt(Object tag) {
2021-08-10 10:55:30 +00:00
if (tag == null) {
return null;
}
try {
2021-08-14 09:55:00 +00:00
var matcher = TO_ROUND_INT_SUBSTRING_PATTERN.matcher(tag.toString());
2021-08-10 10:55:30 +00:00
return matcher.find() ? Math.round(Float.parseFloat(matcher.group(1))) : null;
} catch (NumberFormatException e) {
return null;
2021-06-14 11:04:03 +00:00
}
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} as an integer or null if invalid. */
2021-06-19 19:55:11 +00:00
public static Integer parseIntOrNull(Object tag) {
if (tag instanceof Number num) {
return num.intValue();
}
if (!(tag instanceof String)) {
return null;
}
try {
return Integer.parseInt(tag.toString());
} catch (NumberFormatException e) {
return null;
}
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} parsed as a boolean. */
2021-06-11 01:15:27 +00:00
public static boolean bool(Object tag) {
2021-08-02 00:26:22 +00:00
return Imposm3Parsers.bool(tag);
2021-06-11 01:15:27 +00:00
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} parsed as an integer where 1 is true, 0 is false. */
2021-06-11 01:15:27 +00:00
public static int boolInt(Object tag) {
2021-08-02 00:26:22 +00:00
return Imposm3Parsers.boolInt(tag);
2021-06-11 01:15:27 +00:00
}
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} parsed as an integer where 1 is true, 0 is false. */
2021-08-02 00:26:22 +00:00
public static int direction(Object input) {
return Imposm3Parsers.direction(input);
2021-06-11 01:15:27 +00:00
}
2021-09-10 00:46:20 +00:00
/**
* Returns {@code tag} for an OSM road based on the tags that are present. Bridges are above roads appear above
* tunnels and major roads appear above minor.
*/
2021-06-11 01:15:27 +00:00
public static int wayzorder(Map<String, Object> tags) {
2021-08-02 00:26:22 +00:00
return Imposm3Parsers.wayzorder(tags);
2021-04-14 11:20:19 +00:00
}
2021-06-23 01:46:42 +00:00
2021-09-10 00:46:20 +00:00
/** Returns {@code tag} as a double or null if invalid. */
2021-06-23 01:46:42 +00:00
public static Double parseDoubleOrNull(Object value) {
if (value instanceof Number num) {
return num.doubleValue();
}
if (value == null) {
return null;
}
try {
return Double.parseDouble(value.toString());
} catch (NumberFormatException e) {
return null;
}
}
2021-04-14 11:20:19 +00:00
}