Skip to content

Commit

Permalink
Merge pull request #59 from satorg/geohash-with-bit-precision
Browse files Browse the repository at this point in the history
add validation for negative numberOfBits in GeoHash.withBitPrecision
  • Loading branch information
kungfoo committed Apr 4, 2022
2 parents 7ed8536 + 2cb753d commit 6e9a2ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/ch/hsr/geohash/GeoHash.java
Expand Up @@ -61,6 +61,9 @@ public static GeoHash withCharacterPrecision(double latitude, double longitude,
* at the same time defines this hash's bounding box.
*/
public static GeoHash withBitPrecision(double latitude, double longitude, int numberOfBits) {
if (numberOfBits < 0) {
throw new IllegalArgumentException("A Geohash can't be of negative bits long!");
}
if (numberOfBits > MAX_BIT_PRECISION) {
throw new IllegalArgumentException("A Geohash can only be " + MAX_BIT_PRECISION + " bits long!");
}
Expand Down
18 changes: 17 additions & 1 deletion src/test/java/ch/hsr/geohash/GeoHashTest.java
Expand Up @@ -116,9 +116,25 @@ private void assertWithin(GeoHash hash, GeoHash bbox) {
assertTrue(hash + " should be within " + bbox, hash.within(bbox));
}

@Test
public void itShouldCreateAHashWithMinimumPrecisionOf0Bits() {
GeoHash geoHash = GeoHash.withBitPrecision(10.0, 10.0, 0);
assertEquals(0, geoHash.significantBits());
assertEquals(0, geoHash.longValue());
assertEquals(new WGS84Point(10.0, 10.0), geoHash.getOriginatingPoint());
}

@Test
public void itShouldCreateAHashWithMaximumPrecisionOf64Bits() {
GeoHash.withBitPrecision(10.0, 10.0, 64);
GeoHash geoHash = GeoHash.withBitPrecision(10.0, 10.0, 64);
assertEquals(64, geoHash.significantBits());
assertEquals(0xc07e07e07e07e07eL, geoHash.longValue());
assertEquals(new WGS84Point(10.0, 10.0), geoHash.getOriginatingPoint());
}

@Test(expected = IllegalArgumentException.class)
public void itShouldThrowWhenTheBitPrecisionIsLessThan0Bits() {
GeoHash.withBitPrecision(46.0, 8.0, -1);
}

@Test(expected = IllegalArgumentException.class)
Expand Down

0 comments on commit 6e9a2ba

Please sign in to comment.