]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Country.java
8d88a191b2b591805f8039ef49cb2a783c696187
[gigi.git] / src / org / cacert / gigi / dbObjects / Country.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Random;
10 import java.util.RandomAccess;
11
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;
16
17 /**
18  * Represents a country containing its ISO 3166-1-Code and its English name.
19  */
20 public class Country {
21
22     public enum CountryCodeType {
23         CODE_2_CHARS(2), //
24         CODE_3_CHARS(3); //
25
26         private final int len;
27
28         private CountryCodeType(int len) {
29             this.len = len;
30         }
31
32         public int getLen() {
33             return len;
34         }
35     }
36
37     /**
38      * Id of the database entry.
39      */
40     private final int id;
41
42     /**
43      * English name of the country.
44      */
45     private final String country;
46
47     /**
48      * ISO 3166-1 alpha-2 code of the country.
49      */
50     private final String countryCode2;
51
52     /**
53      * ISO 3166-1 alpha-3 code of the country.
54      */
55     private final String countryCode3;
56
57     /**
58      * A unmodifiable {@link RandomAccess}-List of all Countries.
59      */
60     private static final List<Country> countries;
61
62     /**
63      * An unmodifiable index of all 2- and 3-letter country codes.
64      */
65     private static final Map<String, Country> byString;
66
67     static {
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();
72             while (rs.next()) {
73                 Country e = new Country(rs);
74                 ccd.put(e.countryCode2, e);
75                 ccd.put(e.countryCode3, e);
76                 cs.add(e);
77             }
78         }
79         countries = Collections.unmodifiableList(new ArrayList<>(cs));
80         byString = Collections.unmodifiableMap(ccd);
81     }
82
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");
88     }
89
90     public int getId() {
91         return id;
92     }
93
94     public String getName() {
95         return country;
96     }
97
98     /**
99      * Returns the default (ISO 3166-1 alpha-2) country code of this country.
100      * 
101      * @return the country code
102      */
103     public String getCode() {
104         return countryCode2;
105     }
106
107     /**
108      * Gets the specified type of country code for this country.
109      * 
110      * @param type
111      *            the type of the code
112      * @return the corresponding code
113      */
114     public String getCode(CountryCodeType type) {
115         switch (type) {
116         case CODE_2_CHARS:
117             return countryCode2;
118         case CODE_3_CHARS:
119             return countryCode3;
120         default:
121             throw new IllegalArgumentException("Enum switch was non-exhaustive");
122         }
123     }
124
125     /**
126      * Gets an unmodifiable, {@link RandomAccess}-List of all countries.
127      * 
128      * @return the list.
129      */
130     public static List<Country> getCountries() {
131         return countries;
132     }
133
134     /**
135      * Checks a country code for its validity and conformance to the given type.
136      * 
137      * @param countrycode
138      *            the code to check
139      * @param cType
140      *            the type it should have
141      * @throws GigiApiException
142      *             if the code was wrong
143      */
144     public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
145         getCountryByCode(countrycode, cType);
146     }
147
148     /**
149      * Fetches the {@link Country} object for the given country code.
150      * 
151      * @param countrycode
152      *            the code to fetch the county for
153      * @param cType
154      *            the type of the code
155      * @return the specified country
156      * @throws GigiApiException
157      *             if the code was wrong.
158      */
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())));
162         }
163         Country i = byString.get(countrycode);
164         if (i == null) {
165             throw new GigiApiException("Country Code was wrong.");
166         }
167         return i;
168     }
169
170     public static Country getRandomCountry() {
171         List<Country> cc = Country.getCountries();
172         int rnd = new Random().nextInt(cc.size());
173         return cc.get(rnd);
174     }
175 }