Skip to content

Commit fe6a1c0

Browse files
committed
Merge branch 'jstastny-arrays-to-objects'
2 parents 0e1afde + 87c8eba commit fe6a1c0

File tree

4 files changed

+264
-119
lines changed

4 files changed

+264
-119
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package de.meggsimum.w3w;
2+
3+
public class Coordinates {
4+
private double latitude;
5+
private double longitude;
6+
7+
public Coordinates() {
8+
}
9+
10+
public Coordinates(double latitude, double longitude) {
11+
this.latitude = latitude;
12+
this.longitude = longitude;
13+
}
14+
15+
public double getLatitude() {
16+
return latitude;
17+
}
18+
19+
public double getLongitude() {
20+
return longitude;
21+
}
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) return true;
26+
if (!(o instanceof Coordinates)) return false;
27+
28+
Coordinates that = (Coordinates) o;
29+
30+
if (Double.compare(that.latitude, latitude) != 0) return false;
31+
return Double.compare(that.longitude, longitude) == 0;
32+
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
int result;
38+
long temp;
39+
temp = Double.doubleToLongBits(latitude);
40+
result = (int) (temp ^ (temp >>> 32));
41+
temp = Double.doubleToLongBits(longitude);
42+
result = 31 * result + (int) (temp ^ (temp >>> 32));
43+
return result;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "Coordinates{" +
49+
"latitude=" + latitude +
50+
", longitude=" + longitude +
51+
'}';
52+
}
53+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package de.meggsimum.w3w;
2+
3+
public class ThreeWords {
4+
private String first;
5+
private String second;
6+
private String third;
7+
8+
public ThreeWords() {
9+
}
10+
11+
public ThreeWords(String first, String second, String third) {
12+
this.first = first;
13+
this.second = second;
14+
this.third = third;
15+
}
16+
17+
public String getFirst() {
18+
return first;
19+
}
20+
21+
public void setFirst(String first) {
22+
this.first = first;
23+
}
24+
25+
public String getSecond() {
26+
return second;
27+
}
28+
29+
public void setSecond(String second) {
30+
this.second = second;
31+
}
32+
33+
public String getThird() {
34+
return third;
35+
}
36+
37+
public void setThird(String third) {
38+
this.third = third;
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) return true;
44+
if (!(o instanceof ThreeWords)) return false;
45+
46+
ThreeWords that = (ThreeWords) o;
47+
48+
if (!first.equals(that.first)) return false;
49+
if (!second.equals(that.second)) return false;
50+
return third.equals(that.third);
51+
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
int result = first.hashCode();
57+
result = 31 * result + second.hashCode();
58+
result = 31 * result + third.hashCode();
59+
return result;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "ThreeWords{" +
65+
"first='" + first + '\'' +
66+
", second='" + second + '\'' +
67+
", third='" + third + '\'' +
68+
'}';
69+
}
70+
}

src/main/java/de/meggsimum/w3w/What3Words.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ public What3Words(String apiKey, String language) {
5757
this.language = language;
5858
}
5959

60+
public Coordinates wordsToPosition(ThreeWords threeWords, String language) throws IOException, What3WordsException {
61+
double[] doubleCoordinates = wordsToPosition(new String[]{threeWords.getFirst(), threeWords.getSecond(), threeWords.getThird()});
62+
return new Coordinates(doubleCoordinates[0], doubleCoordinates[1]);
63+
}
64+
65+
public Coordinates wordsToPosition(ThreeWords threeWords) throws IOException, What3WordsException {
66+
return wordsToPosition(threeWords, this.language);
67+
}
68+
6069
/**
6170
* Converts 3 words into a position specifying default language.
6271
*
@@ -90,7 +99,7 @@ public double[] wordsToPosition(String[] words, String language) throws IOExcept
9099
try {
91100
// parse the coordinates out of the JSON
92101
JSONObject json = new JSONObject(response);
93-
if (json.has("position") == true) {
102+
if (json.has("position")) {
94103
JSONArray jsonCoords = (JSONArray) json.get("position");
95104
double[] coords = new double[2];
96105
coords[0] = jsonCoords.getDouble(0);
@@ -114,6 +123,15 @@ public double[] wordsToPosition(String[] words, String language) throws IOExcept
114123

115124
}
116125

126+
public ThreeWords positionToWords(Coordinates coordinates, String language) throws IOException, What3WordsException {
127+
String[] words = positionToWords(new double[]{coordinates.getLatitude(), coordinates.getLongitude()}, language);
128+
return new ThreeWords(words[0], words[1], words[2]);
129+
}
130+
131+
public ThreeWords positionToWords(Coordinates coordinates) throws IOException, What3WordsException {
132+
return positionToWords(coordinates, this.language);
133+
}
134+
117135
/**
118136
* Converts a position into a "w3w-address" with default language.
119137
*

0 commit comments

Comments
 (0)