1 package org.cacert.gigi.dbObjects;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.LinkedList;
9 import java.util.RandomAccess;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.database.GigiResultSet;
14 import org.cacert.gigi.output.template.SprintfCommand;
17 * Represents a country containing its ISO 3166-1-Code and its English name.
19 public class Country {
21 public enum CountryCodeType {
25 private final int len;
27 private CountryCodeType(int len) {
37 * Id of the database entry.
42 * English name of the country.
44 private final String country;
47 * ISO 3166-1 alpha-2 code of the country.
49 private final String countryCode2;
52 * ISO 3166-1 alpha-3 code of the country.
54 private final String countryCode3;
57 * A unmodifiable {@link RandomAccess}-List of all Countries.
59 private static final List<Country> countries;
62 * An unmodifiable index of all 2- and 3-letter country codes.
64 private static final Map<String, Country> byString;
67 LinkedList<Country> cs = new LinkedList<>();
68 HashMap<String, Country> ccd = new HashMap<>();
69 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `english` as country, `code2`, `code3` FROM `countryIsoCode`", true)) {
70 GigiResultSet rs = ps.executeQuery();
72 Country e = new Country(rs);
73 ccd.put(e.countryCode2, e);
74 ccd.put(e.countryCode3, e);
78 countries = Collections.unmodifiableList(new ArrayList<>(cs));
79 byString = Collections.unmodifiableMap(ccd);
82 private Country(GigiResultSet rs) {
83 this.id = rs.getInt("id");
84 this.country = rs.getString("country");
85 this.countryCode2 = rs.getString("code2");
86 this.countryCode3 = rs.getString("code3");
93 public String getName() {
98 * Returns the default (ISO 3166-1 alpha-2) country code of this country.
100 * @return the country code
102 public String getCode() {
107 * Gets the specified type of country code for this country.
110 * the type of the code
111 * @return the corresponding code
113 public String getCode(CountryCodeType type) {
120 throw new IllegalArgumentException("Enum switch was non-exhaustive");
125 * Gets an unmodifiable, {@link RandomAccess}-List of all countries.
129 public static List<Country> getCountries() {
134 * Checks a country code for its validity and conformance to the given type.
139 * the type it should have
140 * @throws GigiApiException
141 * if the code was wrong
143 public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
144 getCountryByCode(countrycode, cType);
148 * Fetches the {@link Country} object for the given country code.
151 * the code to fetch the county for
153 * the type of the code
154 * @return the specified country
155 * @throws GigiApiException
156 * if the code was wrong.
158 public static Country getCountryByCode(String countrycode, CountryCodeType cType) throws GigiApiException {
159 if (countrycode.length() != cType.getLen()) {
160 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
162 Country i = byString.get(countrycode);
164 throw new GigiApiException("Country Code was wrong.");