From b10cfd0e1334551f73ae3f0eae133efac5e48fec Mon Sep 17 00:00:00 2001 From: Paul Picazo Date: Wed, 30 Oct 2024 01:14:27 -0700 Subject: [PATCH] --- src/main/java/org/smerty/jham/Angle.java | 6 +++++- src/main/java/org/smerty/jham/Latitude.java | 3 +++ src/main/java/org/smerty/jham/Longitude.java | 4 +++- src/test/java/org/smerty/jham/AngleTest.java | 6 +++--- src/test/java/org/smerty/jham/LatitudeTest.java | 6 +++--- src/test/java/org/smerty/jham/LocationTest.java | 12 ++++++------ src/test/java/org/smerty/jham/LongitudeTest.java | 6 +++--- 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/smerty/jham/Angle.java b/src/main/java/org/smerty/jham/Angle.java index d51db37..4086821 100644 --- a/src/main/java/org/smerty/jham/Angle.java +++ b/src/main/java/org/smerty/jham/Angle.java @@ -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 diff --git a/src/main/java/org/smerty/jham/Latitude.java b/src/main/java/org/smerty/jham/Latitude.java index 4e790f3..95e4c00 100644 --- a/src/main/java/org/smerty/jham/Latitude.java +++ b/src/main/java/org/smerty/jham/Latitude.java @@ -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); diff --git a/src/main/java/org/smerty/jham/Longitude.java b/src/main/java/org/smerty/jham/Longitude.java index 35e620c..63ba859 100644 --- a/src/main/java/org/smerty/jham/Longitude.java +++ b/src/main/java/org/smerty/jham/Longitude.java @@ -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; } - } diff --git a/src/test/java/org/smerty/jham/AngleTest.java b/src/test/java/org/smerty/jham/AngleTest.java index 79dc19e..9692a81 100644 --- a/src/test/java/org/smerty/jham/AngleTest.java +++ b/src/test/java/org/smerty/jham/AngleTest.java @@ -103,13 +103,13 @@ public void testEqualsHashCode() { @Test public void testInvalidInputs() { Angle angleNaN = new Angle(Double.NaN); - assertTrue(Double.isNaN(angleNaN.getRadians())); + assertTrue(angleNaN == null); Angle anglePosInf = new Angle(Double.POSITIVE_INFINITY); - assertTrue(Double.isInfinite(anglePosInf.getRadians())); + assertTrue(anglePosInf == null); Angle angleNegInf = new Angle(Double.NEGATIVE_INFINITY); - assertTrue(Double.isInfinite(angleNegInf.getRadians())); + assertTrue(angleNegInf == null); } @Test diff --git a/src/test/java/org/smerty/jham/LatitudeTest.java b/src/test/java/org/smerty/jham/LatitudeTest.java index 152c566..419d17f 100644 --- a/src/test/java/org/smerty/jham/LatitudeTest.java +++ b/src/test/java/org/smerty/jham/LatitudeTest.java @@ -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 diff --git a/src/test/java/org/smerty/jham/LocationTest.java b/src/test/java/org/smerty/jham/LocationTest.java index f5f34a5..17111ae 100644 --- a/src/test/java/org/smerty/jham/LocationTest.java +++ b/src/test/java/org/smerty/jham/LocationTest.java @@ -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); } diff --git a/src/test/java/org/smerty/jham/LongitudeTest.java b/src/test/java/org/smerty/jham/LongitudeTest.java index 2040aee..d7e8ed1 100644 --- a/src/test/java/org/smerty/jham/LongitudeTest.java +++ b/src/test/java/org/smerty/jham/LongitudeTest.java @@ -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