Skip to content

Commit f854f7c

Browse files
author
Tyler Coles
committed
Code cleanup pass.
Organized and removed unused imports. Re-formatted all java files. Format tooling from the VS Code Language Support for Java(TM) by Red Hat extension. Will consider Maven-based options in future.
1 parent e05fc02 commit f854f7c

20 files changed

+772
-480
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
.vscode/
1+
.vscode/*
2+
!.vscode/java-formatter.xml
3+
24
target/

.vscode/java-formatter.xml

Lines changed: 382 additions & 0 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
</licenses>
2222

2323
<properties>
24-
<java.version>1.8</java.version>
24+
<maven.compiler.source>1.8</maven.compiler.source>
25+
<maven.compiler.target>1.8</maven.compiler.target>
2526
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2627
<!-- <buildNumber>is generated automatically (below)</buildNumber> -->
2728
</properties>
@@ -34,8 +35,8 @@
3435
<artifactId>maven-compiler-plugin</artifactId>
3536
<version>3.10.1</version>
3637
<configuration>
37-
<source>${java.version}</source>
38-
<target>${java.version}</target>
38+
<source>${maven.compiler.source}</source>
39+
<target>${maven.compiler.target}</target>
3940
</configuration>
4041
</plugin>
4142

src/main/java/com/javadocmd/simplelatlng/Geohasher.java

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,41 @@
2222
import 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
*/
3432
public 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);

src/main/java/com/javadocmd/simplelatlng/LatLng.java

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
import com.javadocmd.simplelatlng.util.LatLngConfig;
2222

2323
/**
24-
* <p>A representation of a single point in latitude and longitude.
25-
* All data is handled in degrees and will be normalized if possible
26-
* to the +/- 90 latitude, +/- 180 longitude region.</p>
27-
*
28-
* <p>Note that attempting to create a LatLng with invalid values
29-
* (NaN, negative infinity, positive infinity) will throw
30-
* IllegalArgumentExceptions.</p>
31-
*
32-
* @author Tyler Coles
24+
* <p>
25+
* A representation of a single point in latitude and longitude. All data is
26+
* handled in degrees and will be normalized if possible to the +/- 90 latitude,
27+
* +/- 180 longitude region.
28+
* </p>
29+
* <p>
30+
* Note that attempting to create a LatLng with invalid values (NaN, negative
31+
* infinity, positive infinity) will throw IllegalArgumentExceptions.
32+
* </p>
3333
*/
3434
public class LatLng implements Serializable {
3535

@@ -47,19 +47,18 @@ public static LatLng random() {
4747
/**
4848
* Creates a random latitude and longitude. (Not inclusive of (-90, 0))
4949
*
50-
* @param r the random number generator to use, if you want to be
51-
* specific or are creating many LatLngs at once.
50+
* @param r the random number generator to use, if you want to be specific or
51+
* are creating many LatLngs at once.
5252
* @return the random LatLng.
5353
*/
5454
public static LatLng random(Random r) {
55-
return new LatLng((r.nextDouble() * -180.0) + 90.0,
56-
(r.nextDouble() * -360.0) + 180.0);
55+
return new LatLng((r.nextDouble() * -180.0) + 90.0, (r.nextDouble() * -360.0) + 180.0);
5756
}
5857

5958
/**
60-
* Tests whether two angles fall within the tolerance
61-
* allowed in {@link com.javadocmd.simplelatlng.util.LatLngConfig}. Ignores
62-
* NaN and infinite values, returning false in either case.
59+
* Tests whether two angles fall within the tolerance allowed in
60+
* {@link com.javadocmd.simplelatlng.util.LatLngConfig}. Ignores NaN and
61+
* infinite values, returning false in either case.
6362
*
6463
* @param degree1 one degree angle.
6564
* @param degree2 another degree angle.
@@ -70,8 +69,7 @@ public static boolean degreesEqual(double degree1, double degree2) {
7069
return false;
7170
if (Double.isInfinite(degree1) || Double.isInfinite(degree2))
7271
return false;
73-
return LatLngConfig.doubleToLong(degree1) == LatLngConfig
74-
.doubleToLong(degree2);
72+
return LatLngConfig.doubleToLong(degree1) == LatLngConfig.doubleToLong(degree2);
7573
}
7674

7775
private long latitude;
@@ -80,7 +78,7 @@ public static boolean degreesEqual(double degree1, double degree2) {
8078
/**
8179
* Creates a LatLng point.
8280
*
83-
* @param latitude the latitude in degrees.
81+
* @param latitude the latitude in degrees.
8482
* @param longitude the longitude in degrees.
8583
*/
8684
public LatLng(double latitude, double longitude) {
@@ -97,8 +95,8 @@ public double getLatitude() {
9795
}
9896

9997
/**
100-
* Get the internal long representation of this point's latitude
101-
* in degrees. Intended for library use only.
98+
* Get the internal long representation of this point's latitude in degrees.
99+
* Intended for library use only.
102100
*
103101
* @return the internal representation of latitude in degrees.
104102
*/
@@ -116,8 +114,8 @@ public double getLongitude() {
116114
}
117115

118116
/**
119-
* Get the internal long representation of this point's longitude
120-
* in degrees. Intended for library use only.
117+
* Get the internal long representation of this point's longitude in degrees.
118+
* Intended for library use only.
121119
*
122120
* @return the internal representation of longitude in degrees.
123121
*/
@@ -128,7 +126,7 @@ public long getLongitudeInternal() {
128126
/**
129127
* Sets the latitude and longitude for this point.
130128
*
131-
* @param latitude the latitude in degrees.
129+
* @param latitude the latitude in degrees.
132130
* @param longitude the longitude in degrees.
133131
*/
134132
public void setLatitudeLongitude(double latitude, double longitude) {
@@ -162,12 +160,13 @@ private void setLongitude(double longitude) {
162160
}
163161

164162
/**
165-
* @return true if this LatLng represents a polar coordinate (+/- 90 degrees latitude).
163+
* @return true if this LatLng represents a polar coordinate (+/- 90 degrees
164+
* latitude).
166165
*/
167166
public boolean isPolar() {
168167
return this.latitude == 90000000L || this.latitude == -90000000L;
169168
}
170-
169+
171170
@Override
172171
public boolean equals(Object obj) {
173172
if (obj == this)
@@ -193,8 +192,7 @@ public int hashCode() {
193192

194193
@Override
195194
public String toString() {
196-
return String.format("(%s,%s)",
197-
LatLngConfig.getDegreeFormat().format(LatLngConfig.longToDouble(this.latitude)),
195+
return String.format("(%s,%s)", LatLngConfig.getDegreeFormat().format(LatLngConfig.longToDouble(this.latitude)),
198196
LatLngConfig.getDegreeFormat().format(LatLngConfig.longToDouble(this.longitude)));
199197
}
200198
}

0 commit comments

Comments
 (0)