Skip to content

Commit

Permalink
Support user-assigned country codes. (See issue TakahikoKawasaki#25.)
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Mahar committed Jul 14, 2015
1 parent 79ebefc commit 77d3d6f
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 29 deletions.
81 changes: 58 additions & 23 deletions src/main/java/com/neovisionaries/i18n/CountryCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
*
* @author Takahiko Kawasaki
*/
public enum CountryCode
public enum CountryCode implements CountryCodeInterface
{

/**
Expand Down Expand Up @@ -2150,14 +2150,20 @@ public enum Assignment
}


private static final Map<String, CountryCode> alpha3Map = new HashMap<String, CountryCode>();
private static final Map<Integer, CountryCode> numericMap = new HashMap<Integer, CountryCode>();

private static final Map<String, CountryCodeInterface> alpha2Map = new HashMap<String, CountryCodeInterface>();
private static final Map<String, CountryCodeInterface> alpha3Map = new HashMap<String, CountryCodeInterface>();
private static final Map<Integer, CountryCodeInterface> numericMap = new HashMap<Integer, CountryCodeInterface>();
private static final List<CountryCodeInterface> standardAndUserAssignedCodes = new ArrayList<CountryCodeInterface>();

static
{
for (CountryCode cc : values())
{
if (cc.getAlpha2() != null)
{
alpha2Map.put(cc.getAlpha2(), cc);
}

if (cc.getAlpha3() != null)
{
alpha3Map.put(cc.getAlpha3(), cc);
Expand All @@ -2167,6 +2173,8 @@ public enum Assignment
{
numericMap.put(cc.getNumeric(), cc);
}

standardAndUserAssignedCodes.add(cc);
}
}

Expand All @@ -2191,6 +2199,8 @@ private CountryCode(String name, String alpha3, int numeric, Assignment assignme
*
* @return
* The country name.
*
* @see com.neovisionaries.i18n.CountryCodeInterface#getName()
*/
public String getName()
{
Expand All @@ -2207,6 +2217,8 @@ public String getName()
* >ISO 3166-1 alpha-2</a> code.
* {@link CountryCode#UNDEFINED} returns {@code "UNDEFINED"}
* which is not an official ISO 3166-1 alpha-2 code.
*
* @see com.neovisionaries.i18n.CountryCodeInterface#getAlpha2()
*/
public String getAlpha2()
{
Expand All @@ -2224,6 +2236,8 @@ public String getAlpha2()
* Some country codes reserved exceptionally (such as {@link #EU})
* returns {@code null}.
* {@link CountryCode#UNDEFINED} returns {@code null}, too.
*
* @see com.neovisionaries.i18n.CountryCodeInterface#getAlpha3()
*/
public String getAlpha3()
{
Expand All @@ -2241,6 +2255,8 @@ public String getAlpha3()
* Country codes reserved exceptionally (such as {@link #EU})
* returns {@code -1}.
* {@link CountryCode#UNDEFINED} returns {@code -1}, too.
*
* @see com.neovisionaries.i18n.CountryCodeInterface#getNumeric()
*/
public int getNumeric()
{
Expand All @@ -2256,6 +2272,8 @@ public int getNumeric()
*
* @see <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Decoding_table"
* >Decoding table of ISO 3166-1 alpha-2 codes</a>
*
* @see com.neovisionaries.i18n.CountryCodeInterface#getAssignment()
*/
public Assignment getAssignment()
{
Expand Down Expand Up @@ -2338,6 +2356,8 @@ public Assignment getAssignment()
*
* @return
* A {@code Locale} instance that matches this {@code CountryCode}.
*
* @see com.neovisionaries.i18n.CountryCodeInterface#toLocale()
*/
public Locale toLocale()
{
Expand Down Expand Up @@ -2377,6 +2397,8 @@ public Locale toLocale()
* @since 1.4
*
* @see Currency#getInstance(Locale)
*
* @see com.neovisionaries.i18n.CountryCodeInterface#getCurrency()
*/
public Currency getCurrency()
{
Expand Down Expand Up @@ -2416,7 +2438,7 @@ public Currency getCurrency()
*
* @see #getByCode(String, boolean)
*/
public static CountryCode getByCode(String code)
public static CountryCodeInterface getByCode(String code)
{
return getByCode(code, true);
}
Expand All @@ -2443,7 +2465,7 @@ public static CountryCode getByCode(String code)
*
* @see #getByCode(String, boolean)
*/
public static CountryCode getByCodeIgnoreCase(String code)
public static CountryCodeInterface getByCodeIgnoreCase(String code)
{
return getByCode(code, false);
}
Expand Down Expand Up @@ -2471,7 +2493,7 @@ public static CountryCode getByCodeIgnoreCase(String code)
* @return
* A {@code CountryCode} instance, or {@code null} if not found.
*/
public static CountryCode getByCode(String code, boolean caseSensitive)
public static CountryCodeInterface getByCode(String code, boolean caseSensitive)
{
if (code == null)
{
Expand Down Expand Up @@ -2518,7 +2540,7 @@ public static CountryCode getByCode(String code, boolean caseSensitive)
*
* @see Locale#getCountry()
*/
public static CountryCode getByLocale(Locale locale)
public static CountryCodeInterface getByLocale(Locale locale)
{
if (locale == null)
{
Expand Down Expand Up @@ -2571,20 +2593,13 @@ static String canonicalize(String code, boolean caseSensitive)
}


private static CountryCode getByAlpha2Code(String code)
private static CountryCodeInterface getByAlpha2Code(String code)
{
try
{
return Enum.valueOf(CountryCode.class, code);
}
catch (IllegalArgumentException e)
{
return null;
}
return alpha2Map.get(code);
}


private static CountryCode getByAlpha3Code(String code)
private static CountryCodeInterface getByAlpha3Code(String code)
{
return alpha3Map.get(code);
}
Expand All @@ -2603,7 +2618,7 @@ private static CountryCode getByAlpha3Code(String code)
* A {@code CountryCode} instance, or {@code null} if not found.
* If 0 or a negative value is given, {@code null} is returned.
*/
public static CountryCode getByCode(int code)
public static CountryCodeInterface getByCode(int code)
{
if (code <= 0)
{
Expand Down Expand Up @@ -2637,7 +2652,7 @@ public static CountryCode getByCode(int code)
*
* @since 1.11
*/
public static List<CountryCode> findByName(String regex)
public static List<CountryCodeInterface> findByName(String regex)
{
if (regex == null)
{
Expand Down Expand Up @@ -2688,16 +2703,16 @@ public static List<CountryCode> findByName(String regex)
*
* @since 1.11
*/
public static List<CountryCode> findByName(Pattern pattern)
public static List<CountryCodeInterface> findByName(Pattern pattern)
{
if (pattern == null)
{
throw new IllegalArgumentException("pattern is null.");
}

List<CountryCode> list = new ArrayList<CountryCode>();
List<CountryCodeInterface> list = new ArrayList<CountryCodeInterface>();

for (CountryCode entry : values())
for (CountryCodeInterface entry : standardAndUserAssignedCodes)
{
// If the name matches the given pattern.
if (pattern.matcher(entry.getName()).matches())
Expand All @@ -2708,4 +2723,24 @@ public static List<CountryCode> findByName(Pattern pattern)

return list;
}

public static void addUserAssigned(CountryCodeInterface code)
{
standardAndUserAssignedCodes.add(code);

if (!alpha3Map.containsKey(code.getAlpha3()))
{
alpha3Map.put(code.getAlpha3(), code);
}

if (!alpha2Map.containsKey(code.getAlpha2()))
{
alpha2Map.put(code.getAlpha2(), code);
}

if (!numericMap.containsKey(code.getNumeric()))
{
numericMap.put(code.getNumeric(), code);
}
}
}
102 changes: 102 additions & 0 deletions src/main/java/com/neovisionaries/i18n/CountryCodeInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.neovisionaries.i18n;

import java.util.Currency;
import java.util.Locale;

import com.neovisionaries.i18n.CountryCode.Assignment;

public interface CountryCodeInterface {

/**
* Get the country name.
*
* @return The country name.
*/
String getName();

/**
* Get the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2"
* >ISO 3166-1 alpha-2</a> code.
*
* @return The standard <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2"
* >ISO 3166-1 alpha-2</a> code or one which is
* <a href="https://en.wikipedia.org/wiki/ISO_3166-1#Reserved_and_user-assigned_code_elements">reserved
* or user-assigned</a>. {@link CountryCode#UNDEFINED} returns
* {@code "UNDEFINED"} which is not an official ISO 3166-1 alpha-2 code.
*/
String getAlpha2();

/**
* Get the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3"
* >ISO 3166-1 alpha-3</a> code.
*
* @return The standard <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3"
* >ISO 3166-1 alpha-3</a> code or one which is
* <a href="https://en.wikipedia.org/wiki/ISO_3166-1#Reserved_and_user-assigned_code_elements">reserved
* or user-assigned</a>. Some country codes reserved exceptionally (such as
* {@link #EU}) returns {@code null}. {@link CountryCode#UNDEFINED} returns
* {@code null}, too.
*/
String getAlpha3();

/**
* Get the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_numeric"
* >ISO 3166-1 numeric</a> code.
*
* @return The standard <a href="http://en.wikipedia.org/wiki/ISO_3166-1_numeric"
* >ISO 3166-1 numeric</a> code or one which is
* <a href="https://en.wikipedia.org/wiki/ISO_3166-1#Reserved_and_user-assigned_code_elements">reserved
* or user-assigned</a>. Country codes reserved exceptionally (such as
* {@link #EU}) returns {@code -1}. {@link CountryCode#UNDEFINED} returns
* {@code -1}, too.
*/
int getNumeric();

/**
* Get the assignment state of this country code in ISO 3166-1.
*
* @return The assignment state.
*
* @see <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Decoding_table"
* >Decoding table of ISO 3166-1 alpha-2 codes</a>
*/
Assignment getAssignment();

/**
* Convert this {@code CountryCode} instance to a {@link Locale} instance.
*
* @return A {@code Locale} instance that matches this {@code CountryCode}.
*/
Locale toLocale();

/**
* Get the currency.
*
* <p>
* This method is an alias of {@link Currency}{@code .}{@link
* Currency#getInstance(Locale) getInstance}{@code (}{@link
* #toLocale()}{@code )}. The only difference is that this method returns
* {@code null} when {@code Currency.getInstance(Locale)} throws
* {@code IllegalArgumentException}.
* </p>
*
* <p>
* This method returns {@code null} when the territory represented by this
* {@code CountryCode} instance does not have a currency. {@link #AQ}
* (Antarctica) is one example.
* </p>
*
* <p>
* In addition, this method returns {@code null} also when the ISO 3166 code
* represented by this {@code CountryCode} instance is not supported by the
* implementation of {@link
* Currency#getInstance(Locale)}. At the time of this writing, {@link #SS}
* (South Sudan) is one example.
* </p>
*
* @return A {@code Currency} instance. In some cases, null is returned.
*
* @see Currency#getInstance(Locale)
*/
Currency getCurrency();
}
4 changes: 2 additions & 2 deletions src/main/java/com/neovisionaries/i18n/CurrencyCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,7 @@ public static List<CurrencyCode> getByCountry(String country, boolean caseSensit
* {@code CurrencyCode} instance whose country list contains
* the specified country, the size of the returned list is zero.
*/
public static List<CurrencyCode> getByCountry(CountryCode country)
public static List<CurrencyCode> getByCountry(CountryCodeInterface country)
{
List<CurrencyCode> list = new ArrayList<CurrencyCode>();

Expand All @@ -2602,7 +2602,7 @@ public static List<CurrencyCode> getByCountry(CountryCode country)

for (CurrencyCode currency : values())
{
for (CountryCode cc : currency.countryList)
for (CountryCodeInterface cc : currency.countryList)
{
if (cc == country)
{
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/neovisionaries/i18n/LocaleCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ public LanguageCode getLanguage()
* The country code. This method may return null.
* For example, {@link #en LocaleCode.en}.getCountry() returns null.
*/
public CountryCode getCountry()
public CountryCodeInterface getCountry()
{
return country;
}
Expand Down Expand Up @@ -1643,7 +1643,7 @@ public static List<LocaleCode> getByCountry(String country, boolean caseSensitiv
*
* @since 1.3
*/
public static List<LocaleCode> getByCountry(CountryCode country)
public static List<LocaleCode> getByCountry(CountryCodeInterface country)
{
List<LocaleCode> list = new ArrayList<LocaleCode>();

Expand Down
Loading

0 comments on commit 77d3d6f

Please sign in to comment.