diff --git a/geo/src/main/java/com/github/davidmoten/geo/LatLong.java b/geo/src/main/java/com/github/davidmoten/geo/LatLong.java index 0f39209..09801fb 100644 --- a/geo/src/main/java/com/github/davidmoten/geo/LatLong.java +++ b/geo/src/main/java/com/github/davidmoten/geo/LatLong.java @@ -61,4 +61,21 @@ public String toString() { return builder.toString(); } + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final LatLong latLong = (LatLong) o; + return Double.compare(lat, latLong.lat) == 0 && Double.compare(lon, latLong.lon) == 0; + } + + @Override + public int hashCode() { + return 31 * Double.hashCode(lat) + Double.hashCode(lon); + } + } diff --git a/geo/src/test/java/com/github/davidmoten/geo/LatLongTest.java b/geo/src/test/java/com/github/davidmoten/geo/LatLongTest.java index 4e7ede6..6aa3b5f 100644 --- a/geo/src/test/java/com/github/davidmoten/geo/LatLongTest.java +++ b/geo/src/test/java/com/github/davidmoten/geo/LatLongTest.java @@ -1,9 +1,9 @@ package com.github.davidmoten.geo; -import static org.junit.Assert.assertEquals; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Unit tests for {@link LatLong}. * @@ -18,4 +18,15 @@ public void testToString() { new LatLong(10, 20).toString()); } + @Test + public void testHashCode() { + float lat = 20.05f; + float lon = -15.5f; + LatLong a = new LatLong(lat, lon); + LatLong b = new LatLong(lat, lon); + + assertEquals(a.hashCode(), b.hashCode()); + assertEquals(a, b); + } + }