2020import org .slf4j .LoggerFactory ;
2121
2222import javax .annotation .Nonnull ;
23+ import javax .annotation .Nullable ;
2324import java .util .List ;
2425import java .util .regex .Matcher ;
2526
@@ -65,20 +66,10 @@ private MapcodeCodec() {
6566 * @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
6667 */
6768 @ Nonnull
68- public static List <Mapcode > encode (
69- final double latDeg ,
70- final double lonDeg ) throws IllegalArgumentException {
71- checkRange ("latDeg" , latDeg , Point .LAT_DEG_MIN , Point .LAT_DEG_MAX );
72- checkRange ("lonDeg" , lonDeg , Point .LON_DEG_MIN , Point .LON_DEG_MAX );
73-
74- // Call mapcode encoder.
75- @ Nonnull final List <Mapcode > results = Encoder .encode (latDeg , lonDeg , null , false , false , true );
76- assert results != null ;
77- assert results .size () >= 1 ;
78- return results ;
69+ public static List <Mapcode > encode (final double latDeg , final double lonDeg ) throws IllegalArgumentException {
70+ return encode (latDeg , lonDeg , null );
7971 }
8072
81- // Convenience method.
8273 @ Nonnull
8374 public static List <Mapcode > encode (@ Nonnull final Point point ) throws IllegalArgumentException {
8475 checkNonnull ("point" , point );
@@ -97,33 +88,26 @@ public static List<Mapcode> encode(@Nonnull final Point point) throws IllegalArg
9788 *
9889 * @param latDeg Latitude, accepted range: -90..90.
9990 * @param lonDeg Longitude, accepted range: -180..180.
100- * @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. Cannot
101- * be null.
91+ * @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. May be null.
10292 * @return List of mapcode information records, see {@link Mapcode}. This list is empty if no
10393 * Mapcode can be generated for this territory matching the lat/lon.
10494 * @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
10595 */
10696 @ Nonnull
107- public static List <Mapcode > encode (
108- final double latDeg ,
109- final double lonDeg ,
110- @ Nonnull final Territory restrictToTerritory ) throws IllegalArgumentException {
97+ public static List <Mapcode > encode (final double latDeg , final double lonDeg ,
98+ @ Nullable final Territory restrictToTerritory ) throws IllegalArgumentException {
11199 checkRange ("latDeg" , latDeg , Point .LAT_DEG_MIN , Point .LAT_DEG_MAX );
112100 checkRange ("lonDeg" , lonDeg , Point .LON_DEG_MIN , Point .LON_DEG_MAX );
113- checkNonnull ("restrictToTerritory" , restrictToTerritory );
114101
115102 // Call Mapcode encoder.
116- @ Nonnull final List <Mapcode > results =
117- Encoder .encode (latDeg , lonDeg , restrictToTerritory , false , false , false );
103+ final List <Mapcode > results = Encoder .encode (latDeg , lonDeg , restrictToTerritory , false , false , (restrictToTerritory == null ));
118104 assert results != null ;
119105 return results ;
120106 }
121107
122- // Convenience method.
123108 @ Nonnull
124- public static List <Mapcode > encode (
125- @ Nonnull final Point point ,
126- @ Nonnull final Territory restrictToTerritory ) throws IllegalArgumentException {
109+ public static List <Mapcode > encode (@ Nonnull final Point point ,
110+ @ Nullable final Territory restrictToTerritory ) throws IllegalArgumentException {
127111 checkNonnull ("point" , point );
128112 return encode (point .getLatDeg (), point .getLonDeg (), restrictToTerritory );
129113 }
@@ -138,20 +122,14 @@ public static List<Mapcode> encode(
138122 * @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
139123 */
140124 @ Nonnull
141- public static Mapcode encodeToShortest (
142- final double latDeg ,
143- final double lonDeg ) throws IllegalArgumentException {
144- checkRange ("latDeg" , latDeg , Point .LAT_DEG_MIN , Point .LAT_DEG_MAX );
145- checkRange ("lonDeg" , lonDeg , Point .LON_DEG_MIN , Point .LON_DEG_MAX );
146-
147- // Call mapcode encoder.
148- @ Nonnull final List <Mapcode > results = Encoder .encode (latDeg , lonDeg , null , false , true , true );
149- assert results != null ;
150- assert results .size () == 1 ;
151- return results .get (0 );
125+ public static Mapcode encodeToShortest (final double latDeg , final double lonDeg ) throws IllegalArgumentException {
126+ try {
127+ return encodeToShortest (latDeg , lonDeg , null );
128+ } catch (final UnknownMapcodeException e ) {
129+ throw new IllegalStateException ("Encoding should never fail for + " + latDeg + ", " + lonDeg , e );
130+ }
152131 }
153132
154- // Convenience method.
155133 @ Nonnull
156134 public static Mapcode encodeToShortest (@ Nonnull final Point point ) throws IllegalArgumentException {
157135 checkNonnull ("point" , point );
@@ -163,24 +141,20 @@ public static Mapcode encodeToShortest(@Nonnull final Point point) throws Illega
163141 *
164142 * @param latDeg Latitude, accepted range: -90..90.
165143 * @param lonDeg Longitude, accepted range: -180..180.
166- * @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. Cannot
167- * be null.
144+ * @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. May be null.
168145 * @return Shortest mapcode, see {@link Mapcode}.
169146 * @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
170147 * @throws UnknownMapcodeException Thrown if no mapcode was found for the lat/lon matching the territory.
171148 */
172149 @ Nonnull
173- public static Mapcode encodeToShortest (
174- final double latDeg ,
175- final double lonDeg ,
176- @ Nonnull final Territory restrictToTerritory ) throws IllegalArgumentException , UnknownMapcodeException {
150+ public static Mapcode encodeToShortest (final double latDeg , final double lonDeg ,
151+ @ Nullable final Territory restrictToTerritory ) throws IllegalArgumentException , UnknownMapcodeException {
177152 checkRange ("latDeg" , latDeg , Point .LAT_DEG_MIN , Point .LAT_DEG_MAX );
178153 checkRange ("lonDeg" , lonDeg , Point .LON_DEG_MIN , Point .LON_DEG_MAX );
179- checkNonnull ("restrictToTerritory" , restrictToTerritory );
180154
181155 // Call mapcode encoder.
182156 @ Nonnull final List <Mapcode > results =
183- Encoder .encode (latDeg , lonDeg , restrictToTerritory , false , true , false );
157+ Encoder .encode (latDeg , lonDeg , restrictToTerritory , false , true , ( restrictToTerritory == null ) );
184158 assert results != null ;
185159 assert results .size () <= 1 ;
186160 if (results .isEmpty ()) {
@@ -190,11 +164,9 @@ public static Mapcode encodeToShortest(
190164 return results .get (0 );
191165 }
192166
193- // Convenience method.
194167 @ Nonnull
195- public static Mapcode encodeToShortest (
196- @ Nonnull final Point point ,
197- @ Nonnull final Territory restrictToTerritory ) throws IllegalArgumentException , UnknownMapcodeException {
168+ public static Mapcode encodeToShortest (@ Nonnull final Point point ,
169+ @ Nullable final Territory restrictToTerritory ) throws IllegalArgumentException , UnknownMapcodeException {
198170 checkNonnull ("point" , point );
199171 return encodeToShortest (point .getLatDeg (), point .getLonDeg (), restrictToTerritory );
200172 }
@@ -208,9 +180,7 @@ public static Mapcode encodeToShortest(
208180 * @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
209181 */
210182 @ Nonnull
211- public static Mapcode encodeToInternational (
212- final double latDeg ,
213- final double lonDeg ) throws IllegalArgumentException {
183+ public static Mapcode encodeToInternational (final double latDeg , final double lonDeg ) throws IllegalArgumentException {
214184 checkRange ("latDeg" , latDeg , Point .LAT_DEG_MIN , Point .LAT_DEG_MAX );
215185 checkRange ("lonDeg" , lonDeg , Point .LON_DEG_MIN , Point .LON_DEG_MAX );
216186
@@ -221,7 +191,6 @@ public static Mapcode encodeToInternational(
221191 return results .get (results .size () - 1 );
222192 }
223193
224- // Convenience method.
225194 @ Nonnull
226195 public static Mapcode encodeToInternational (@ Nonnull final Point point ) throws IllegalArgumentException {
227196 checkNonnull ("point" , point );
@@ -263,28 +232,27 @@ public static Point decode(@Nonnull final String mapcode) throws UnknownMapcodeE
263232 * Note that if a territory-code is supplied in the string, it takes preferences over the parameter.
264233 *
265234 * @param mapcode Mapcode.
266- * @param defaultTerritoryContext Default territory context for disambiguation purposes.
235+ * @param defaultTerritoryContext Default territory context for disambiguation purposes. May be null.
267236 * @return Point corresponding to mapcode. Latitude range: -90..90, longitude range: -180..180.
268237 * @throws UnknownMapcodeException Thrown if the mapcode has the right syntax, but cannot be decoded into a point.
269238 * @throws IllegalArgumentException Thrown if arguments are null, or if the syntax of the mapcode is incorrect.
270239 */
271240 @ Nonnull
272241 public static Point decode (
273242 @ Nonnull final String mapcode ,
274- @ Nonnull final Territory defaultTerritoryContext ) throws UnknownMapcodeException , IllegalArgumentException {
243+ @ Nullable final Territory defaultTerritoryContext ) throws UnknownMapcodeException , IllegalArgumentException {
275244 checkNonnull ("mapcode" , mapcode );
276- checkNonnull ("territoryContext" , defaultTerritoryContext );
277245
278246 // Clean up mapcode.
279- String mapcodeClean = mapcode .trim ().toUpperCase ();
247+ String mapcodeClean = Mapcode . convertStringToPlainAscii ( mapcode .trim ().toUpperCase () );
280248
281249 // Determine territory from mapcode.
282250 final Territory territory ;
283251 final Matcher matcherTerritory = Mapcode .PATTERN_TERRITORY .matcher (mapcodeClean );
284252 if (!matcherTerritory .find ()) {
285253
286254 // No territory code was supplied in the string, use specified territory context parameter.
287- territory = defaultTerritoryContext ;
255+ territory = ( defaultTerritoryContext != null ) ? defaultTerritoryContext : Territory . AAA ;
288256 } else {
289257
290258 // Use the territory code from the string.
@@ -296,7 +264,7 @@ public static Point decode(
296264 }
297265
298266 // Cut off the territory part.
299- mapcodeClean = mapcode .substring (matcherTerritory .end ()).trim ();
267+ mapcodeClean = mapcodeClean .substring (matcherTerritory .end ()).trim ();
300268 }
301269
302270 if (!Mapcode .isValidMapcodeFormat (mapcodeClean )) {
0 commit comments