Skip to content

Commit 25b5143

Browse files
author
Kevin
committed
Merge branch 'master' of git://github.com/kungfoo/geohash-java
Conflicts: src/main/java/ch/hsr/geohash/GeoHash.java src/test/java/ch/hsr/geohash/GeoHashTest.java
1 parent 8c29b23 commit 25b5143

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

‎src/main/java/ch/hsr/geohash/GeoHash.java‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414

15+
@SuppressWarnings({"JavaDoc"})
1516
public final class GeoHash implements Comparable<GeoHash>, Serializable {
1617
private static final long serialVersionUID = -8553214249630252175L;
1718
private static final int[] BITS = { 16, 8, 4, 2, 1 };
@@ -167,10 +168,7 @@ private static void setBoundingBox(GeoHash hash, double[] latitudeRange, double[
167168
}
168169

169170
public GeoHash next(int step) {
170-
int insignificantBits = 64 - significantBits;
171-
long unshiftedVal = bits >> insignificantBits;
172-
unshiftedVal += step;
173-
return fromLongValue(unshiftedVal << insignificantBits, significantBits);
171+
return fromOrd(ord() + step, significantBits);
174172
}
175173

176174
public GeoHash next() {
@@ -181,6 +179,15 @@ public GeoHash prev() {
181179
return next(-1);
182180
}
183181

182+
public long ord() {
183+
int insignificantBits = 64 - significantBits;
184+
return bits >> insignificantBits;
185+
}
186+
187+
public static GeoHash fromOrd(long ord, int significantBits) {
188+
int insignificantBits = 64 - significantBits;
189+
return fromLongValue(ord << insignificantBits, significantBits);
190+
}
184191
/**
185192
* Counts the number of geohashes contained between the two (ie how many times next() is called to increment from one to two)
186193
* This value depends on the number of significant bits.
@@ -192,10 +199,7 @@ public GeoHash prev() {
192199
public static long stepsBetween(GeoHash one, GeoHash two) {
193200
if (one.significantBits() != two.significantBits())
194201
throw new IllegalArgumentException("It is only valid to compare the number of steps between two hashes if they have the same number of significant bits");
195-
int insignificantBits = 64 - one.significantBits();
196-
long unshiftedVal1 = one.bits >> insignificantBits;
197-
long unshiftedVal2 = two.bits >> insignificantBits;
198-
return unshiftedVal2 - unshiftedVal1;
202+
return two.ord() - one.ord();
199203
}
200204

201205
private void divideRangeEncode(double value, double[] range) {

0 commit comments

Comments
 (0)