2222import java .util .Map ;
2323
2424/**
25- * Implements the <a href="http://en.wikipedia.org/wiki/Geohash">Geohash</a>
26- * algorithm for hashing latitude and longitude points. Note: this implementation
27- * is only "stable" with 12-character hashes. Decoding "s" and re-hashing the
28- * result yields "t40000000000". Decoding and re-hashing "t40000000000" yields
29- * the same. 12 characters was chosen because this gives us precision up to
30- * one-millionth of a degree, like the rest of this library.
31- *
32- * @author Tyler Coles
25+ * Implements the <a href="http://en.wikipedia.org/wiki/Geohash">Geohash</a>
26+ * algorithm for hashing latitude and longitude points. Note: this
27+ * implementation is only "stable" with 12-character hashes. Decoding "s" and
28+ * re-hashing the result yields "t40000000000". Decoding and re-hashing
29+ * "t40000000000" yields the same. 12 characters was chosen because this gives
30+ * us precision up to one-millionth of a degree, like the rest of this library.
3331 */
3432public class Geohasher {
3533
3634 /**
37- * <p>Number of hash characters supported.</p>
38- * <p>Translates to binary bits per value by the formula:</p>
39- * <pre>BITS = ((PRECISION * 5) / 2) + PRECISION % 2.</pre>
40- * <p>BITS in turn translates to numerical precision
41- * by the formula:</p>
42- * <pre>LATITUDE_ERROR = 90.0 / (2 ^ (BITS + 1))
43- *LONGITUDE_ERROR = 180.0 / (2 ^ (BITS + 1))</pre>
35+ * <p>
36+ * Number of hash characters supported.
37+ * </p>
38+ * <p>
39+ * Translates to binary bits per value by the formula:
40+ * </p>
41+ *
42+ * <pre>
43+ * BITS = ((PRECISION * 5) / 2) + PRECISION % 2.
44+ * </pre>
45+ * <p>
46+ * BITS in turn translates to numerical precision by the formula:
47+ * </p>
48+ *
49+ * <pre>
50+ * LATITUDE_ERROR = 90.0 / (2 ^ (BITS + 1))
51+ *LONGITUDE_ERROR = 180.0 / (2 ^ (BITS + 1))
52+ * </pre>
4453 */
4554 public static final int PRECISION = 12 ;
4655 private static final int BITS = ((PRECISION * 5 ) / 2 ) + PRECISION % 2 ;
4756 private static final double MAX_LAT = 90.0 ;
4857 private static final double MAX_LNG = 180.0 ;
49- private static final char [] HASH_CHARS_ARRAY = new char []{'0' , '1' , '2' ,
50- '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
51- 'j' , 'k' , 'm' , 'n' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' ,
52- 'z' };
58+ private static final char [] HASH_CHARS_ARRAY = new char [] { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'b' ,
59+ 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'j' , 'k' , 'm' , 'n' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' };
5360 private static final Map <Character , Integer > HASH_CHARS_MAP ;
5461 protected static final BigDecimal [] LAT_BIT_VALUES ;
5562 protected static final BigDecimal [] LNG_BIT_VALUES ;
@@ -58,7 +65,7 @@ public class Geohasher {
5865 BigDecimal lngValue = new BigDecimal (MAX_LNG );
5966 LAT_BIT_VALUES = new BigDecimal [BITS ];
6067 LNG_BIT_VALUES = new BigDecimal [BITS ];
61-
68+
6269 BigDecimal TWO = new BigDecimal ("2" );
6370 for (int i = 0 ; i < BITS ; i ++) {
6471 latValue = latValue .divide (TWO );
@@ -76,8 +83,8 @@ public class Geohasher {
7683 /**
7784 * Decodes a geohash string to its LatLng equivalent.
7885 *
79- * @param hash the geohash string of any precision, although LatLng will
80- * still not become <em>more</em> precise than its settings.
86+ * @param hash the geohash string of any precision, although LatLng will still
87+ * not become <em>more</em> precise than its settings.
8188 * @return the decoded point.
8289 */
8390 public static LatLng decode (String hash ) {
@@ -110,8 +117,7 @@ protected static BitSet hashToBits(String hash) {
110117
111118 return bits ;
112119 } catch (NullPointerException e ) {
113- throw new IllegalArgumentException (
114- "Geohash string contains invalid characters." );
120+ throw new IllegalArgumentException ("Geohash string contains invalid characters." );
115121 }
116122 }
117123
@@ -122,7 +128,7 @@ protected static BitSet hashToBits(String hash) {
122128 * @return two bit sets: [0] = even bits, [1] = odd bits
123129 */
124130 protected static BitSet [] deInterleave (BitSet bits ) {
125- BitSet [] sets = new BitSet []{ new BitStore (), new BitStore ()};
131+ BitSet [] sets = new BitSet [] { new BitStore (), new BitStore () };
126132
127133 int n = bits .size ();
128134 for (int i = 0 ; i < n ; i ++) {
@@ -133,12 +139,12 @@ protected static BitSet[] deInterleave(BitSet bits) {
133139 }
134140
135141 /**
136- * Converts the set of bits representing a single value to double.
137- * The bit set passed into this function should already be de-interleaved.
142+ * Converts the set of bits representing a single value to double. The bit set
143+ * passed into this function should already be de-interleaved.
138144 *
139- * @param bits the bits for this value.
140- * @param bitValues the correct set of pre-computed bit-values to use
141- * for the particular value we are decoding: latitude or longitude.
145+ * @param bits the bits for this value.
146+ * @param bitValues the correct set of pre-computed bit-values to use for the
147+ * particular value we are decoding: latitude or longitude.
142148 * @return the value.
143149 */
144150 protected static double bitsToDouble (BitSet bits , BigDecimal [] bitValues ) {
@@ -153,11 +159,11 @@ protected static double bitsToDouble(BitSet bits, BigDecimal[] bitValues) {
153159 value = value .subtract (bitValues [n - i - 1 ]);
154160 }
155161 }
156-
162+
157163 BigDecimal lastDelta2x = bitValues [n - 1 ].multiply (new BigDecimal (2 ));
158164 BigDecimal roundingMin = lastValue .subtract (lastDelta2x );
159165 BigDecimal roundingMax = lastValue .add (lastDelta2x );
160-
166+
161167 BigDecimal rounded = value .setScale (6 , RoundingMode .HALF_UP );
162168 if (rounded .compareTo (roundingMin ) < 0 || rounded .compareTo (roundingMax ) > 0 ) {
163169 rounded = value .setScale (6 , RoundingMode .HALF_DOWN );
@@ -201,7 +207,7 @@ protected static String bitsToHash(BitSet bits) {
201207 * Interleaves two sets of bits.
202208 *
203209 * @param evenBits the bits to use for even bits. (0, 2, 4,...)
204- * @param oddBits the bits to use for odd bits. (1, 3, 5,...)
210+ * @param oddBits the bits to use for odd bits. (1, 3, 5,...)
205211 * @return the interleaved bits.
206212 */
207213 protected static BitSet interleave (BitSet evenBits , BitSet oddBits ) {
@@ -218,12 +224,12 @@ protected static BitSet interleave(BitSet evenBits, BitSet oddBits) {
218224 }
219225
220226 /**
221- * Converts a double value to its bit representation in the geohash
227+ * Converts a double value to its bit representation in the geohash
222228 * specification.
223229 *
224- * @param value the value to encode.
225- * @param maxRange the max range for the particular value we are
226- * encoding: latitude = 90.0, longitude = 180.0.
230+ * @param value the value to encode.
231+ * @param maxRange the max range for the particular value we are encoding:
232+ * latitude = 90.0, longitude = 180.0.
227233 * @return the bit set for this value.
228234 */
229235 protected static BitSet doubleToBits (double value , double maxRange ) {
@@ -247,13 +253,11 @@ protected static BitSet doubleToBits(double value, double maxRange) {
247253 }
248254
249255 /**
250- * Specialization of BitSet to <em>actually</em> keep track of
251- * the number of bits that are being usefully employed, regardless
252- * of whether or not they are 1 or 0. This requires that you call set
253- * for 0's <em>and</em> 1's. Not all features are implemented, but setting,
254- * getting, and size work fine, which is all I need for this class.
255- *
256- * @author Tyler Coles
256+ * Specialization of BitSet to <em>actually</em> keep track of the number of
257+ * bits that are being usefully employed, regardless of whether or not they are
258+ * 1 or 0. This requires that you call set for 0's <em>and</em> 1's. Not all
259+ * features are implemented, but setting, getting, and size work fine, which is
260+ * all I need for this class.
257261 */
258262 protected static class BitStore extends BitSet {
259263
@@ -271,7 +275,7 @@ public String toString() {
271275 s = (get (i ) ? "1" : "0" ) + s ;
272276 return s ;
273277 }
274-
278+
275279 @ Override
276280 public void set (int bitIndex ) {
277281 super .set (bitIndex );
0 commit comments