Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid input handling in Angle, Latitude, Longitude, and Location classes #22

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/org/smerty/jham/Angle.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public Angle() {
* angle in radians
*/
public Angle(final double radiansIn) {
this.radians = new BigDecimal(radiansIn);
if (Double.isNaN(radiansIn) || Double.isInfinite(radiansIn)) {
this.radians = null;
} else {
this.radians = new BigDecimal(radiansIn);
}
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/smerty/jham/Latitude.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public int hashCode() {
* @return latitude object
*/
public static Latitude fromDegrees(final double degrees) {
if (Double.isNaN(degrees) || Double.isInfinite(degrees)) {
return null;
}
Latitude latitude = new Latitude();
latitude.radians = Angle.degreesToRadians(new BigDecimal(degrees),
MathContext.DECIMAL128);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/smerty/jham/Longitude.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public int hashCode() {
* @return longitude object
*/
public static Longitude fromDegrees(final double degrees) {
if (Double.isNaN(degrees) || Double.isInfinite(degrees)) {
return null;
}
Longitude longitude = new Longitude();
longitude.radians = Angle.degreesToRadians(new BigDecimal(degrees),
MathContext.DECIMAL128);
return longitude;
}


}
6 changes: 3 additions & 3 deletions src/test/java/org/smerty/jham/AngleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public void testEqualsHashCode() {
@Test
public void testInvalidInputs() {
Angle angleNaN = new Angle(Double.NaN);
assertTrue(Double.isNaN(angleNaN.getRadians()));
assertTrue(angleNaN.getRadians() == null);

Angle anglePosInf = new Angle(Double.POSITIVE_INFINITY);
assertTrue(Double.isInfinite(anglePosInf.getRadians()));
assertTrue(anglePosInf.getRadians() == null);

Angle angleNegInf = new Angle(Double.NEGATIVE_INFINITY);
assertTrue(Double.isInfinite(angleNegInf.getRadians()));
assertTrue(angleNegInf.getRadians() == null);
}

@Test
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/smerty/jham/LatitudeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public void testEquals() {
@Test
public void testInvalidInputs() {
Latitude latNaN = Latitude.fromDegrees(Double.NaN);
assertTrue(Double.isNaN(latNaN.toDegrees()));
assertTrue(latNaN == null);

Latitude latPosInf = Latitude.fromDegrees(Double.POSITIVE_INFINITY);
assertTrue(Double.isInfinite(latPosInf.toDegrees()));
assertTrue(latPosInf == null);

Latitude latNegInf = Latitude.fromDegrees(Double.NEGATIVE_INFINITY);
assertTrue(Double.isInfinite(latNegInf.toDegrees()));
assertTrue(latNegInf == null);
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/smerty/jham/LocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,24 @@ public void testEquals() {
public void testInvalidInputs() {
try {
Location locNaN = new Location(Double.NaN, Double.NaN);
assertTrue(Double.isNaN(locNaN.getLatitude().toDegrees()));
assertTrue(Double.isNaN(locNaN.getLongitude().toDegrees()));
assertTrue(locNaN.getLatitude() == null);
assertTrue(locNaN.getLongitude() == null);
} catch (Exception e) {
assertTrue(false);
}

try {
Location locPosInf = new Location(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
assertTrue(Double.isInfinite(locPosInf.getLatitude().toDegrees()));
assertTrue(Double.isInfinite(locPosInf.getLongitude().toDegrees()));
assertTrue(locPosInf.getLatitude() == null);
assertTrue(locPosInf.getLongitude() == null);
} catch (Exception e) {
assertTrue(false);
}

try {
Location locNegInf = new Location(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
assertTrue(Double.isInfinite(locNegInf.getLatitude().toDegrees()));
assertTrue(Double.isInfinite(locNegInf.getLongitude().toDegrees()));
assertTrue(locNegInf.getLatitude() == null);
assertTrue(locNegInf.getLongitude() == null);
} catch (Exception e) {
assertTrue(false);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/smerty/jham/LongitudeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public void testEquals() {
@Test
public void testInvalidInputs() {
Longitude lonNaN = Longitude.fromDegrees(Double.NaN);
assertTrue(Double.isNaN(lonNaN.toDegrees()));
assertTrue(lonNaN == null);

Longitude lonPosInf = Longitude.fromDegrees(Double.POSITIVE_INFINITY);
assertTrue(Double.isInfinite(lonPosInf.toDegrees()));
assertTrue(lonPosInf == null);

Longitude lonNegInf = Longitude.fromDegrees(Double.NEGATIVE_INFINITY);
assertTrue(Double.isInfinite(lonNegInf.toDegrees()));
assertTrue(lonNegInf == null);
}

@Test
Expand Down
Loading