Migrated from old fork

This commit is contained in:
SGWebFreelancer 2024-01-14 18:01:41 +08:00
parent 797f1e1737
commit 44300ba227
2 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,79 @@
package com.baeldung.geocoordinatevalidator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GeoCoordinateValidator {
public static final String DD_COORDINATE_REGEX = "^(-?\\d+\\.\\d+)(\\s*,\\s*)?(-?\\d+\\.\\d+)$";
public static final String DMS_COORDINATE_REGEX = "^(\\d{1,3})°(\\d{1,2})'(\\d{1,2}(\\.\\d+)?)?\"?([NSns])(\\s*,\\s*)?(\\d{1,3})°(\\d{1,2})'(\\d{1,2}(\\.\\d+)?)?\"?([WEwe])$";
public static final String MGRS_COORDINATE_REGEX = "^\\d{1,2}[^IO]{3}(\\d{10}|\\d{8}|\\d{6}|\\d{4}|\\d{2})$";
public static boolean isValidDDFormatWithRegex(String coordinateString) {
return Pattern.compile(DD_COORDINATE_REGEX).matcher(coordinateString).matches();
}
public static boolean isValidDMSFormatWithRegex(String coordinateString) {
return Pattern.compile(DMS_COORDINATE_REGEX).matcher(coordinateString).matches();
}
public static boolean isValidMGRSFormatWithRegex(String coordinateString) {
return Pattern.compile(MGRS_COORDINATE_REGEX).matcher(coordinateString).matches();
}
public static boolean isValidDDFormatWithCustomValidation(String coordinateString) {
try {
String[] parts = coordinateString.split(",");
if (parts.length != 2) {
return false;
}
double latitude = Double.parseDouble(parts[0].trim());
double longitude = Double.parseDouble(parts[1].trim());
return !(latitude < -90) && !(latitude > 90) && !(longitude < -180) && !(longitude > 180);
} catch (NumberFormatException e) {
return false;
}
}
public static boolean isValidDMSFormatWithCustomValidation(String coordinateString) {
try {
String[] dmsParts = coordinateString.split("[°',]");
if (dmsParts.length > 6) {
return false;
}
int degreesLatitude = Integer.parseInt(dmsParts[0].trim());
int minutesLatitude = Integer.parseInt(dmsParts[1].trim());
String[] secondPartsLatitude = dmsParts[2].split("\"");
double secondsLatitude = secondPartsLatitude.length > 1 ? Double.parseDouble(secondPartsLatitude[0].trim()) : 0.0;
String hemisphereLatitude = secondPartsLatitude.length > 1 ? secondPartsLatitude[1] : dmsParts[2];
int degreesLongitude = Integer.parseInt(dmsParts[3].trim());
int minutesLongitude = Integer.parseInt(dmsParts[4].trim());
String[] secondPartsLongitude = dmsParts[5].split("\"");
double secondsLongitude = secondPartsLongitude.length > 1 ? Double.parseDouble(secondPartsLongitude[0].trim()) : 0.0;
String hemisphereLongitude = secondPartsLongitude.length > 1 ? secondPartsLongitude[1] : dmsParts[5];
if (isInvalidLatitude(degreesLatitude, minutesLatitude, secondsLatitude, hemisphereLatitude) ||
isInvalidLongitude(degreesLongitude, minutesLongitude, secondsLongitude, hemisphereLongitude)) {
return false;
}
return true;
} catch (NumberFormatException e) {
return false;
}
}
private static boolean isInvalidLatitude(int degrees, int minutes, double seconds, String hemisphere) {
return degrees < 0 || degrees > 90 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60 ||
(!hemisphere.equalsIgnoreCase("N") && !hemisphere.equalsIgnoreCase("S"));
}
private static boolean isInvalidLongitude(int degrees, int minutes, double seconds, String hemisphere) {
return degrees < 0 || degrees > 180 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60 ||
(!hemisphere.equalsIgnoreCase("E") && !hemisphere.equalsIgnoreCase("W"));
}
}

View File

@ -0,0 +1,91 @@
package com.baeldung.geocoordinatevalidator;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class GeoCoordinateValidatorUnitTest {
@Test
public void givenValidDDCoordinates_whenValidatingWithRegex_thenReturnsTrue() {
assertTrue(GeoCoordinateValidator.isValidDDFormatWithRegex("34.0522 , -118.2437"));
assertTrue(GeoCoordinateValidator.isValidDDFormatWithRegex("-34.0522, 118.2437"));
assertTrue(GeoCoordinateValidator.isValidDDFormatWithRegex("-90.0, 180.0"));
assertTrue(GeoCoordinateValidator.isValidDDFormatWithRegex("90.0, -180.0"));
}
@Test
public void givenInvalidDDCoordinates_whenValidatingWithRegex_thenReturnsFalse() {
assertFalse(GeoCoordinateValidator.isValidDDFormatWithRegex("invalid"));
assertFalse(GeoCoordinateValidator.isValidDDFormatWithRegex("90degrees, 180degrees"));
assertFalse(GeoCoordinateValidator.isValidDDFormatWithRegex("90.000001, 0"));
}
@Test
public void givenValidDMSCoordinates_whenValidatingWithRegex_thenReturnsTrue() {
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithRegex("45°30'15.5\"S , 123°45'59.999\"W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithRegex("45°30'N, 123°45'23.2\"W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithRegex("45°30'23.2\"N, 123°45'W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithRegex("45°30'N, 123°45'W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithRegex("34°12'34\"N, 118°14'37\"W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithRegex("34°12'34\"s, 118°14'37\"e"));
}
@Test
public void givenInvalidDMSCoordinates_whenValidatingWithRegex_thenReturnsFalse() {
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithRegex("invalid"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithRegex("91degress12'34\"W, 118degress14'37\"W"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithRegex("1000°12'34\"N, 118°14'37\"W"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithRegex("34°12'34\"W, 118°14'37\"N"));
}
@Test
public void givenValidMGRSCoordinates_whenValidatingWithRegex_thenReturnsTrue() {
assertTrue(GeoCoordinateValidator.isValidMGRSFormatWithRegex("33TWN1234567890"));
assertTrue(GeoCoordinateValidator.isValidMGRSFormatWithRegex("33TWN12346789"));
assertTrue(GeoCoordinateValidator.isValidMGRSFormatWithRegex("33TWN123678"));
assertTrue(GeoCoordinateValidator.isValidMGRSFormatWithRegex("33TWN1267"));
}
@Test
public void givenInvalidMGRSCoordinates_whenValidatingWithRegex_thenReturnsFalse() {
assertFalse(GeoCoordinateValidator.isValidMGRSFormatWithRegex("invalid"));
assertFalse(GeoCoordinateValidator.isValidMGRSFormatWithRegex("33TIO1234567890"));
assertFalse(GeoCoordinateValidator.isValidMGRSFormatWithRegex("1000TWN1234567890"));
}
@Test
public void givenValidDDCoordinates_whenValidatingWithCustomValidation_thenReturnsTrue() {
assertTrue(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("34.0522, -118.2437"));
assertTrue(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("-34.0522, 118.2437"));
assertTrue(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("-90.0, 180.0"));
assertTrue(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("90.0, -180.0"));
}
@Test
public void givenInvalidDDCoordinates_whenValidatingWithCustomValidation_thenReturnsFalse() {
assertFalse(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("90degrees, 180degrees"));
assertFalse(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("invalid"));
assertFalse(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("91.0, -118.2437"));
assertFalse(GeoCoordinateValidator.isValidDDFormatWithCustomValidation("34.0522, -181.0"));
}
@Test
public void givenValidDMSCoordinates_whenValidatingWithCustomValidation_thenReturnsTrue() {
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("34°12'34\"N, 118°14'37\"W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("34°12'34\"s, 118°14'37\"e"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("45°30'N, 123°45'23.2\"W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("45°30'23.2\"N, 123°45'W"));
assertTrue(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("45°30'N, 123°45'W"));
}
@Test
public void givenInvalidDMSCoordinates_whenValidatingWithCustomValidation_thenReturnsFalse() {
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("invalid"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("91°12'34\"N, 118°14'37\"W"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("34°60'34\"N, 118°14'37\"W"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("34°12'60\"N, 118°14'37\"W"));
assertFalse(GeoCoordinateValidator.isValidDMSFormatWithCustomValidation("34°12'34\"N, 181°14'37\"W"));
}
}