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.Random;
10 import java.util.RandomAccess;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.database.GigiPreparedStatement;
14 import org.cacert.gigi.database.GigiResultSet;
15 import org.cacert.gigi.output.template.SprintfCommand;
18 * Represents a country containing its ISO 3166-1-Code and its English name.
20 public class Country {
22 public enum CountryCodeType {
26 private final int len;
28 private CountryCodeType(int len) {
38 * Id of the database entry.
43 * English name of the country.
45 private final String country;
48 * ISO 3166-1 alpha-2 code of the country.
50 private final String countryCode2;
53 * ISO 3166-1 alpha-3 code of the country.
55 private final String countryCode3;
58 * A unmodifiable {@link RandomAccess}-List of all Countries.
60 private static final List<Country> countries;
63 * An unmodifiable index of all 2- and 3-letter country codes.
65 private static final Map<String, Country> byString;
68 LinkedList<Country> cs = new LinkedList<>();
69 HashMap<String, Country> ccd = new HashMap<>();
70 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `english` as country, `code2`, `code3` FROM `countryIsoCode`", true)) {
71 GigiResultSet rs = ps.executeQuery();
73 Country e = new Country(rs);
74 ccd.put(e.countryCode2, e);
75 ccd.put(e.countryCode3, e);
79 countries = Collections.unmodifiableList(new ArrayList<>(cs));
80 byString = Collections.unmodifiableMap(ccd);
83 private Country(GigiResultSet rs) {
84 this.id = rs.getInt("id");
85 this.country = rs.getString("country");
86 this.countryCode2 = rs.getString("code2");
87 this.countryCode3 = rs.getString("code3");
94 public String getName() {
99 * Returns the default (ISO 3166-1 alpha-2) country code of this country.
101 * @return the country code
103 public String getCode() {
108 * Gets the specified type of country code for this country.
111 * the type of the code
112 * @return the corresponding code
114 public String getCode(CountryCodeType type) {
121 throw new IllegalArgumentException("Enum switch was non-exhaustive");
126 * Gets an unmodifiable, {@link RandomAccess}-List of all countries.
130 public static List<Country> getCountries() {
135 * Checks a country code for its validity and conformance to the given type.
140 * the type it should have
141 * @throws GigiApiException
142 * if the code was wrong
144 public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
145 getCountryByCode(countrycode, cType);
149 * Fetches the {@link Country} object for the given country code.
152 * the code to fetch the county for
154 * the type of the code
155 * @return the specified country
156 * @throws GigiApiException
157 * if the code was wrong.
159 public static Country getCountryByCode(String countrycode, CountryCodeType cType) throws GigiApiException {
160 if (countrycode.length() != cType.getLen()) {
161 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
163 Country i = byString.get(countrycode);
165 throw new GigiApiException("Country Code was wrong.");
170 public static Country getRandomCountry() {
171 List<Country> cc = Country.getCountries();
172 int rnd = new Random().nextInt(cc.size());