@@ -646,7 +646,7 @@ static Territory fromNumber(final int number) throws UnknownTerritoryException {
646646 }
647647
648648 /**
649- * Get a territory from a mapcode territory abbreviation. Note that the provided abbreviation is NOT an
649+ * Get a territory from a mapcode territory abbreviation (or a territory name) . Note that the provided abbreviation is NOT an
650650 * ISO code: it's a mapcode prefix. As local mapcodes for subdivisions have been optimized to prefer to use 2-character
651651 * subdivisions codes in local codes, subdivisions are preferred over countries in this case.
652652 *
@@ -658,7 +658,7 @@ static Territory fromNumber(final int number) throws UnknownTerritoryException {
658658 *
659659 * Brazilian mapcodes, on the other hand, would be specified as "BRA BDHP.JK39-1D", using the ISO 3 letter code.
660660 *
661- * @param alphaCode Territory, alphanumeric code.
661+ * @param alphaCode Territory name or alphanumeric code.
662662 * @return Territory.
663663 * @throws UnknownTerritoryException Thrown if incorrect numeric or alphanumeric code.
664664 */
@@ -828,7 +828,7 @@ private Territory(
828828 int min = Integer .MAX_VALUE ;
829829 int max = Integer .MIN_VALUE ;
830830 final Set <Integer > territoryCodes = new HashSet <Integer >();
831- final Set <String > aliasesSet = new HashSet <String >();
831+ final Set <String > namesSet = new HashSet <String >();
832832
833833 for (final Territory territory : Territory .values ()) {
834834 final int territoryNumber = territory .getNumber ();
@@ -849,14 +849,33 @@ private Territory(
849849 if ((territory .parentTerritory != null ) && !parentList .contains (territory .parentTerritory )) {
850850 parentList .add (territory .parentTerritory );
851851 }
852+
853+ // Add territory codes and alias codes.
854+ if (namesSet .contains (territory .toString ())) {
855+ throw new ExceptionInInitializerError (errorPrefix + "non-unique territory: " + territory .toString ());
856+ }
857+ namesSet .add (territory .toString ());
852858 addNameWithParentVariants (territory .toString (), territory );
853859 for (final String alias : territory .aliases ) {
854- if (aliasesSet .contains (alias )) {
860+ if (namesSet .contains (alias )) {
855861 throw new ExceptionInInitializerError (errorPrefix + "non-unique alias: " + alias );
856862 }
857- aliasesSet .add (alias );
863+ namesSet .add (alias );
858864 addNameWithParentVariants (alias , territory );
859865 }
866+
867+ // Add territory fullnames and aliases as well. Skip special case: territory name == territory code (e.g. USA).
868+ if (namesSet .contains (territory .fullName .toUpperCase ()) && !territory .toString ().equals (territory .fullName .toUpperCase ())) {
869+ throw new ExceptionInInitializerError (errorPrefix + "non-unique fullName: " + territory .fullName .toUpperCase ());
870+ }
871+ addNameWithParentVariants (territory .fullName .toUpperCase (), territory );
872+ for (final String fullNameAlias : territory .fullNameAliases ) {
873+ if (namesSet .contains (fullNameAlias .toUpperCase ())) {
874+ throw new ExceptionInInitializerError (errorPrefix + "non-unique fullName alias: " + fullNameAlias );
875+ }
876+ namesSet .add (fullNameAlias .toUpperCase ());
877+ addNameWithParentVariants (fullNameAlias .toUpperCase (), territory );
878+ }
860879 min = Math .min (min , territory .number );
861880 max = Math .max (max , territory .number );
862881 }
0 commit comments