Skip to content

Commit 9161476

Browse files
authored
Merge pull request #93 from bocops/master
Java: fixing issue #89 and adding tests
2 parents e1bca09 + 88d36e0 commit 9161476

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

java/com/google/openlocationcode/OpenLocationCode.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public double getEastLongitude() {
149149
/**
150150
* Creates Open Location Code object for the provided code.
151151
* @param code A valid OLC code. Can be a full code or a shortened code.
152-
* @throws IlegalArgumentException when the passed code is not valid.
152+
* @throws IllegalArgumentException when the passed code is not valid.
153153
* @constructor
154154
*/
155155
public OpenLocationCode(String code) throws IllegalArgumentException {
@@ -432,11 +432,14 @@ public OpenLocationCode recover(double referenceLatitude, double referenceLongit
432432
double recoveredLatitude = recoveredCodeArea.getCenterLatitude();
433433
double recoveredLongitude = recoveredCodeArea.getCenterLongitude();
434434

435-
// Move the recovered latitude by one precision up or down if it is too far from the reference.
435+
// Move the recovered latitude by one precision up or down if it is too far from the reference,
436+
// unless doing so would lead to an invalid latitude.
436437
double latitudeDiff = recoveredLatitude - referenceLatitude;
437-
if (latitudeDiff > prefixPrecision / 2) {
438+
if (latitudeDiff > prefixPrecision / 2
439+
&& recoveredLatitude - prefixPrecision > -LATITUDE_MAX.intValue()) {
438440
recoveredLatitude -= prefixPrecision;
439-
} else if (latitudeDiff < -prefixPrecision / 2) {
441+
} else if (latitudeDiff < -prefixPrecision / 2
442+
&& recoveredLatitude + prefixPrecision < LATITUDE_MAX.intValue()) {
440443
recoveredLatitude += prefixPrecision;
441444
}
442445

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.google.openlocationcode.tests;
2+
3+
import com.google.openlocationcode.OpenLocationCode;
4+
5+
import junit.framework.Assert;
6+
import org.junit.Test;
7+
8+
/** Test recovery near the poles. */
9+
public class RecoverTest {
10+
11+
@Test
12+
public void testRecoveryNearSouthPole() {
13+
OpenLocationCode olc = new OpenLocationCode("XXXXXX+XX");
14+
Assert.assertEquals("2CXXXXXX+XX",olc.recover(-81.0,0.0).getCode());
15+
}
16+
17+
@Test
18+
public void testRecoveryNearNorthPole() {
19+
OpenLocationCode olc = new OpenLocationCode("2222+22");
20+
Assert.assertEquals("CFX22222+22", olc.recover(89.6, 0.0).getCode());
21+
}
22+
}

0 commit comments

Comments
 (0)