Skip to content

Commit 3a7f9ae

Browse files
author
unknown
committed
Accommodate API changes
1 parent 751a676 commit 3a7f9ae

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

src/com/mapcode/example/MapCodeExample.java

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,98 @@
44
******************************************************************************/
55
package com.mapcode.example;
66

7-
import java.util.ArrayList;
7+
import java.util.List;
88

99
import com.mapcode.MapcodeDecoder;
1010
import com.mapcode.MapcodeEncoder;
11-
import com.mapcode.MapcodeParentTerritory;
12-
import com.mapcode.MapcodeTerritory;
13-
import com.mapcode.MapcodeTerritory.CodeFormat;
14-
import com.mapcode.Point;
11+
import com.mapcode.common.ParentTerritory;
12+
import com.mapcode.common.Territory;
13+
import com.mapcode.common.Territory.CodeFormat;
14+
import com.mapcode.common.Point;
15+
import com.mapcode.common.UnknownTerritoryNameException;
1516

1617
public class MapCodeExample {
1718
public static void main(String[] args) {
1819

1920
// example 1 - disambiguate MN in several conditions
2021
System.out.println("Example 1:");
2122
String iso = "MN";
22-
System.out.println("ISO code " + iso + " if we don't care about context: "
23-
+ MapcodeTerritory.fromString(iso).toString());
24-
System.out.println("ISO code " + iso + " in USA context : "
25-
+ MapcodeTerritory.fromString(iso, MapcodeParentTerritory.USA).toString());
26-
System.out.println("ISO code " + iso + " in IND context : "
27-
+ MapcodeTerritory.fromString(iso, MapcodeParentTerritory.IND).toString());
28-
System.out.println("ISO code " + iso + " in RUS context : "
29-
+ MapcodeTerritory.fromString(iso, MapcodeParentTerritory.RUS).toString());
23+
try {
24+
System.out.println("ISO code " + iso + " if we don't care about context: "
25+
+ Territory.fromString(iso).toString());
26+
System.out.println("ISO code " + iso + " in USA context : "
27+
+ Territory.fromString(iso, ParentTerritory.USA).toString());
28+
System.out.println("ISO code " + iso + " in IND context : "
29+
+ Territory.fromString(iso, ParentTerritory.IND).toString());
30+
System.out.println("ISO code " + iso + " in RUS context : "
31+
+ Territory.fromString(iso, ParentTerritory.RUS).toString());
32+
} catch (UnknownTerritoryNameException e) {
33+
// TODO Auto-generated catch block
34+
e.printStackTrace();
35+
}
36+
3037
System.out.println();
3138

32-
// example 2 - get the Mapcode Territory for a territory abbreviation 'isocode',
39+
// example 2 - get the Mapcode Territory for a territory abbreviation
40+
// 'isocode',
3341
// e.g. entered by the user
3442

3543
String isocode = "NLD"; // abbreviation for The Netherlands
36-
MapcodeTerritory mapcodeTerritory = MapcodeTerritory.fromString(isocode);
44+
Territory mapcodeTerritory = Territory.AAA;
45+
try {
46+
mapcodeTerritory = Territory.fromString(isocode);
47+
} catch (UnknownTerritoryNameException e) {
48+
// TODO Auto-generated catch block
49+
e.printStackTrace();
50+
}
3751

3852
// example 3 - print the full English name of the territory.
3953
System.out.println("Example 2/3:");
40-
System.out.println("Territory " + isocode +": " + mapcodeTerritory.getFullName() );
54+
System.out.println("Territory " + isocode + ": " + mapcodeTerritory.getFullName());
4155
System.out.println();
42-
56+
4357
// example 4 - decode some mapcodes in this territory
4458
System.out.println("Example 4 (Decode examples):");
4559
String mapcode = "49.4V";
46-
Point p = MapcodeDecoder.master_decode(mapcode, mapcodeTerritory);
60+
Point p = MapcodeDecoder.decode(mapcode, mapcodeTerritory);
4761
if (p.isDefined()) {
48-
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " represents latitude " + p.getLatitudeDegrees() + ", longitude " + p.getLongitudeDegrees());
62+
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString()
63+
+ " represents latitude " + p.getLatDeg() + ", longitude " + p.getLonDeg());
4964
} else {
5065
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " is invalid");
5166
}
5267

5368
mapcode = "49.4A";
54-
p = MapcodeDecoder.master_decode(mapcode, mapcodeTerritory);
69+
p = MapcodeDecoder.decode(mapcode, mapcodeTerritory);
5570
if (p.isDefined()) {
56-
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " represents latitude " + p.getLatitudeDegrees() + ", longitude " + p.getLongitudeDegrees());
71+
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString()
72+
+ " represents latitude " + p.getLatDeg() + ", longitude " + p.getLonDeg());
5773
} else {
5874
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " is invalid");
5975
}
6076
System.out.println();
61-
77+
6278
// example 5 - encode a coordinate in this territory
6379
System.out.println("Example 5 (Encode examples):");
6480
double lat = 52.376514;
6581
double lon = 4.908542;
66-
82+
6783
// get all results, exclude "world"
68-
ArrayList<String> s = MapcodeEncoder.master_encode(lat, lon, mapcodeTerritory, false, false, false);
69-
System.out.println("latitude "+ lat + ", longitude " + lon + " has "+ s.size() +" possible mapcodes in " + mapcodeTerritory.getFullName());
84+
List<String> s = MapcodeEncoder.encode(lat, lon, mapcodeTerritory, false, false, false);
85+
System.out.println("latitude " + lat + ", longitude " + lon + " has " + s.size() + " possible mapcodes in "
86+
+ mapcodeTerritory.getFullName());
7087
int i = 1;
71-
for (String result: s) {
88+
for (String result : s) {
7289
String[] parts = result.split("/");
7390
String result_mapcode = parts[0];
7491
int result_ccode = Integer.parseInt(parts[1]); // the ccode
7592

76-
// let's give non-earth mapcodes an explicit, non-ambiguous territory prefix
93+
// let's give non-earth mapcodes an explicit, non-ambiguous
94+
// territory prefix
7795
String result_prefix = "";
7896
if (result_mapcode.length() != 10) {
79-
result_prefix = MapcodeTerritory.fromTerritoryCode(result_ccode).toString(CodeFormat.MINIMAL_UNAMBIGUOUS) + " ";
97+
result_prefix = Territory.fromTerritoryCode(result_ccode).toString(CodeFormat.MINIMAL_UNAMBIGUOUS)
98+
+ " ";
8099
}
81100
System.out.println(" alternative " + String.valueOf(i++) + ": \"" + result_prefix + result_mapcode + "\"");
82101
}
@@ -86,20 +105,22 @@ public static void main(String[] args) {
86105
System.out.println("Example 6:");
87106
lat = 26.87016;
88107
lon = 75.847;
89-
System.out.println("All possible mapcodes in the world for latitude "+ lat +", longitude " + lon);
108+
System.out.println("All possible mapcodes in the world for latitude " + lat + ", longitude " + lon);
90109

91-
s = MapcodeEncoder.master_encode(lat, lon, null, false, false, true);
110+
s = MapcodeEncoder.encode(lat, lon, null, false, false, true);
92111

93-
i=1;
94-
for (String result: s) {
112+
i = 1;
113+
for (String result : s) {
95114
String[] parts = result.split("/");
96115
String result_mapcode = parts[0];
97116
int result_ccode = Integer.parseInt(parts[1]); // the ccode
98117

99-
// let's give non-earth mapcodes an explicit, non-ambiguous territory prefix
118+
// let's give non-earth mapcodes an explicit, non-ambiguous
119+
// territory prefix
100120
String result_prefix = "";
101121
if (result_mapcode.length() != 10) {
102-
result_prefix = MapcodeTerritory.fromTerritoryCode(result_ccode).toString(CodeFormat.MINIMAL_UNAMBIGUOUS) + " ";
122+
result_prefix = Territory.fromTerritoryCode(result_ccode).toString(CodeFormat.MINIMAL_UNAMBIGUOUS)
123+
+ " ";
103124
}
104125
System.out.println(" alternative " + String.valueOf(i++) + ": \"" + result_prefix + result_mapcode + "\"");
105126
}

0 commit comments

Comments
 (0)