fmpp.ISOCountryCode.fmt Maven / Gradle / Ivy
Show all versions of cdt Show documentation
<@pp.changeOutputFile name="org/jadira/cdt/country/ISOCountryCode.java" />
/*
* Copyright 2012 Christopher Pheby
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jadira.cdt.country;
import java.util.HashMap;
import java.util.Map;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import org.jadira.cdt.country.CountryCode;
/**
* Enumeration of ISO ISO 3166-1
* country codes.
*
* The Enum names of this enumeration themselves are represented by
* ISO 3166-1 alpha-2
* codes.
* Instance methods return country name (getName()),
* ISO 3166-1 alpha-3 codes ({@link ISOCountryCode#getAlpha3Code()}),
* ISO 3166-1 numericCode codes ({@link ISOCountryCode#getNumericCode()}),
* and E.164 the dialling region code ({@link ISOCountryCode#getDiallingRegionCode()})
* Static factory methods can retrieve ISOCountryCode instances that
* corresponds to a given code.
*
*
* To update this class, populate using countrynames.txt, downloadable from OpenGeoCode.
* For the purpose of FMPP, all but one line of header must be removed.
*
*/
public enum ISOCountryCode implements CountryCode {
<#list countryCode as countryCode>
${countryCode.alpha2Code}("${countryCode.englishShortnameReading}", "${countryCode.alpha3Code}", ${countryCode.numericCode}),
#list>;
private static final Map alpha3CodeMap = new HashMap();
private static final Map numericCodeMap = new HashMap();
private static final PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance();
static {
for (ISOCountryCode next : values()) {
alpha3CodeMap.put(next.getAlpha3Code(), next);
numericCodeMap.put(next.getNumericCode(), next);
}
}
private final String countryName;
private final String alpha3Code;
private final Integer numericCode;
private ISOCountryCode(String countryName, String alpha3Code, Integer numericCode) {
this.countryName = countryName;
this.alpha3Code = alpha3Code;
this.numericCode = numericCode;
}
/**
* Get the country name.
* @return The country name.
*/
public String getCountryName() {
return countryName;
}
/**
* Get the ISO 3166-1 alpha-2 code.
* @return The ISO 3166-1 alpha-2 code.
*/
public String getAlpha2Code() {
return name();
}
/**
* Get the ISO 3166-1 alpha-3 code.
* @return The ISO 3166-1 alpha-3 code.
*/
public String getAlpha3Code() {
return alpha3Code;
}
/**
* Get the ISO 3166-1 numericCode code.
* @return The ISO 3166-1 numericCode code.
*/
public Integer getNumericCode() {
return numericCode;
}
/**
* Gets the International Direct Dial prefix for the country
* @return
*/
public Integer getDiallingRegionCode() {
return PHONE_NUMBER_UTIL.getCountryCodeForRegion(this.getAlpha2Code());
}
/**
* Get a CountryCode that corresponds to a given ISO 3166-1 alpha-2 or
* alpha-3 code.
* @param code An ISO 3166-1 alpha-2 or
* alpha-3 code.
* @return A ISOCountryCode instance, or null if not found.
*/
public static ISOCountryCode getByCode(String code) {
if (code == null) {
return null;
}
if (code.length() == 2) {
return getByAlpha2Code(code);
} else if (code.length() == 3) {
return getByAlpha3Code(code);
} else {
return null;
}
}
/**
* Get a CountryCode that corresponds to a given ISO 3166-1 alpha-2 code.
* @param code An ISO 3166-1 alpha-2.
* @return A ISOCountryCode instance, or null if not found.
*/
public static ISOCountryCode getByAlpha2Code(String code) {
try {
return Enum.valueOf(ISOCountryCode.class, code);
} catch (IllegalArgumentException e) {
return null;
}
}
/**
* Get a CountryCode that corresponds to a given ISO 3166-1 alpha-3 code.
* @param code An ISO 3166-1 alpha-3 code.
* @return A ISOCountryCode instance, or null if not found.
*/
public static ISOCountryCode getByAlpha3Code(String code) {
return alpha3CodeMap.get(code);
}
/**
* Get a CountryCode that corresponds to a given ISO 3166-1
* numericCode code.
* @param code An ISO 3166-1 numericCode code.
* @return A CountryCode instance, or null if not found.
*/
public static ISOCountryCode getByNumericCode(Integer code) {
return numericCodeMap.get(code);
}
/**
* Get a CountryCode that corresponds to a given Dialling Region code.
* @param code An E.164 Dialling Region Code code.
* @return A CountryCode instance, or null if not found.
*/
public static ISOCountryCode getByDiallingRegionCode(Integer code) {
return getByCode(PHONE_NUMBER_UTIL.getRegionCodeForCountryCode(code));
}
}