]> git.mxchange.org Git - jcountry-core.git/blob - src/org/mxchange/jcountry/model/data/CountryData.java
c0dbebe5cc56af986cc3cbcb8cee0c0d829f8834
[jcountry-core.git] / src / org / mxchange / jcountry / model / data / CountryData.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcountry.model.data;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.NamedQueries;
29 import javax.persistence.NamedQuery;
30 import javax.persistence.Table;
31 import javax.persistence.Temporal;
32 import javax.persistence.TemporalType;
33 import javax.persistence.Transient;
34 import org.apache.commons.lang3.StringUtils;
35 import org.mxchange.jcoreutils.comparable.ComparableUtils;
36
37 /**
38  * A POJO for country data
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Entity (name = "country_data")
43 @Table (name = "country_data")
44 @NamedQueries (
45                 {
46                         @NamedQuery (name = "AllCountries", query = "SELECT c FROM country_data AS c ORDER BY c.countryId ASC")
47                 }
48 )
49 @SuppressWarnings ("PersistenceUnitPresent")
50 public class CountryData implements Country {
51
52         /**
53          * Serial number
54          */
55         @Transient
56         private static final long serialVersionUID = 14_853_982_718_509L;
57
58         /**
59          * Dial prefix to be dialed before an abroad number is being dialed. In
60          * Germany this is "+" or 00.
61          */
62         @Basic (optional = false)
63         @Column (name = "country_abroad_dial_prefix", length = 10)
64         private String countryAbroadDialPrefix;
65
66         /**
67          * 2-characters country code, all upper-case (example: DE for Germany, PH
68          * for Philippines)
69          */
70         @Basic (optional = false)
71         @Column (name = "country_code", length = 2, nullable = false, unique = true)
72         private String countryCode;
73
74         /**
75          * TImestamp when this entry has been created
76          */
77         @Basic (optional = false)
78         @Temporal (TemporalType.TIMESTAMP)
79         @Column (name = "country_entry_created", nullable = false, updatable = false)
80         private Date countryEntryCreated;
81
82         /**
83          * TImestamp when this entry has been updated
84          */
85         @Temporal (TemporalType.TIMESTAMP)
86         @Column (name = "country_entry_updated", insertable = false)
87         private Date countryEntryUpdated;
88
89         /**
90          * Dial prefix to be dialed before the area/city number is dialed. In
91          * Germany, this is 0.
92          */
93         @Basic (optional = false)
94         @Column (name = "country_external_dial_prefix", length = 10)
95         private String countryExternalDialPrefix;
96
97         /**
98          * Key to i18n key (to have translated country names)
99          */
100         @Basic (optional = false)
101         @Column (name = "country_i18n_key", length = 100, nullable = false)
102         private String countryI18nKey;
103
104         /**
105          * Id number
106          */
107         @Id
108         @GeneratedValue (strategy = GenerationType.IDENTITY)
109         @Column (name = "country_id", nullable = false, updatable = false)
110         private Long countryId;
111
112         /**
113          * Is the local dialing prefix required or optional for calling numbers in
114          * same area?
115          */
116         @Basic (optional = false)
117         @Column (name = "country_is_local_prefix_required", nullable = false)
118         private Boolean countryIsLocalPrefixRequired;
119
120         /**
121          * 2-digit country's phone code (example: 49 for Germany, 63 for
122          * Philippines)
123          */
124         @Basic (optional = false)
125         @Column (name = "country_phone_code", length = 2, nullable = false, updatable = false)
126         private Short countryPhoneCode;
127
128         /**
129          * Default constructor, required for the JPA
130          */
131         public CountryData () {
132         }
133
134         /**
135          * Constructor with all required fields
136          * <p>
137          * @param countryAbroadDialPrefix      Abroad dial prefix
138          * @param countryCode                  2-digit country code
139          * @param countryExternalDialPrefix    External dial prefix
140          * @param countryI18nKey               I18n key
141          * @param countryIsLocalPrefixRequired Whether local prefix is required to
142          *                                     dial
143          * @param countryPhoneCode             Phone code for country
144          */
145         public CountryData (final String countryAbroadDialPrefix, final String countryCode, final String countryExternalDialPrefix, final String countryI18nKey, final Boolean countryIsLocalPrefixRequired, final Short countryPhoneCode) {
146                 // Invoke default constructor first
147                 this();
148
149                 // Are all parameter set?
150                 if (null == countryAbroadDialPrefix) {
151                         // Throw NPE
152                         throw new NullPointerException("countryAbroadDialPrefix is null"); //NOI18N
153                 } else if (countryAbroadDialPrefix.isEmpty()) {
154                         // Throw IAE
155                         throw new IllegalArgumentException("countryAbroadDialPrefix is empty"); //NOI18N
156                 } else if (null == countryCode) {
157                         // Throw NPE
158                         throw new NullPointerException("countryCode is null"); //NOI18N
159                 } else if (countryCode.isEmpty()) {
160                         // Throw IAE
161                         throw new IllegalArgumentException("countryCode is empty"); //NOI18N
162                 } else if (null == countryExternalDialPrefix) {
163                         // Throw NPE
164                         throw new NullPointerException("countryExternalDialPrefix is null"); //NOI18N
165                 } else if (countryExternalDialPrefix.isEmpty()) {
166                         // Throw IAE
167                         throw new IllegalArgumentException("countryExternalDialPrefix is empty"); //NOI18N
168                 } else if (null == countryI18nKey) {
169                         // Throw NPE
170                         throw new NullPointerException("countryI18nKey is null"); //NOI18N
171                 } else if (countryI18nKey.isEmpty()) {
172                         // Throw IAE
173                         throw new IllegalArgumentException("countryI18nKey is empty"); //NOI18N
174                 } else if (null == countryIsLocalPrefixRequired) {
175                         // Throw NPE
176                         throw new NullPointerException("countryIsLocalPrefixRequired is null"); //NOI18N
177                 } else if (null == countryPhoneCode) {
178                         // Throw NPE
179                         throw new NullPointerException("countryPhoneCode is null"); //NOI18N
180                 } else if (countryPhoneCode < 1) {
181                         // Throw IAE
182                         throw new IllegalArgumentException(MessageFormat.format("countryPhoneCode={0} is not valid, must be at least 1.", countryPhoneCode)); //NOI18N
183                 }
184
185                 // Set all fields
186                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
187                 this.countryCode = countryCode;
188                 this.countryExternalDialPrefix = countryExternalDialPrefix;
189                 this.countryI18nKey = countryI18nKey;
190                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
191                 this.countryPhoneCode = countryPhoneCode;
192         }
193
194         @Override
195         public int compareTo (final Country country) {
196                 // Check parameter on null-reference and equality to this
197                 if (null == country) {
198                         // Should not happen
199                         throw new NullPointerException("country is null"); //NOI18N
200                 } else if (country.equals(this)) {
201                         // Same object
202                         return 0;
203                 }
204
205                 // Init comparators
206                 final int comparators[] = {
207                         // First check country code, clear indication ...
208                         StringUtils.compare(this.getCountryCode(), country.getCountryCode()),
209                         // ... and phone code, too
210                         this.getCountryPhoneCode().compareTo(country.getCountryPhoneCode()),
211                         // ... then i18n key
212                         StringUtils.compare(this.getCountryI18nKey(), country.getCountryI18nKey()),
213                         // ... abroad dial prefix
214                         StringUtils.compare(this.getCountryAbroadDialPrefix(), country.getCountryAbroadDialPrefix()),
215                         // ... external dial prefix
216                         StringUtils.compare(this.getCountryExternalDialPrefix(), country.getCountryExternalDialPrefix())
217                 };
218
219                 // Check all values
220                 final int comparison = ComparableUtils.checkAll(comparators);
221
222                 // Return value
223                 return comparison;
224         }
225
226         @Override
227         public boolean equals (final Object object) {
228                 if (this == object) {
229                         return true;
230                 } else if (null == object) {
231                         return false;
232                 } else if (this.getClass() != object.getClass()) {
233                         return false;
234                 }
235
236                 // @todo Maybe a bit unsafe cast?
237                 final Country country = (Country) object;
238
239                 if (!Objects.equals(this.getCountryAbroadDialPrefix(), country.getCountryAbroadDialPrefix())) {
240                         return false;
241                 } else if (!Objects.equals(this.getCountryCode(), country.getCountryCode())) {
242                         return false;
243                 } else if (!Objects.equals(this.getCountryExternalDialPrefix(), country.getCountryExternalDialPrefix())) {
244                         return false;
245                 } else if (!Objects.equals(this.getCountryI18nKey(), country.getCountryI18nKey())) {
246                         return false;
247                 } else if (!Objects.equals(this.getCountryId(), country.getCountryId())) {
248                         return false;
249                 } else if (!Objects.equals(this.getCountryIsLocalPrefixRequired(), country.getCountryIsLocalPrefixRequired())) {
250                         return false;
251                 } else if (!Objects.equals(this.getCountryPhoneCode(), country.getCountryPhoneCode())) {
252                         return false;
253                 }
254
255                 return true;
256         }
257
258         @Override
259         public String getCountryAbroadDialPrefix () {
260                 return this.countryAbroadDialPrefix;
261         }
262
263         @Override
264         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
265                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
266         }
267
268         @Override
269         public String getCountryCode () {
270                 return this.countryCode;
271         }
272
273         @Override
274         public void setCountryCode (final String countryCode) {
275                 this.countryCode = countryCode;
276         }
277
278         @Override
279         @SuppressWarnings ("ReturnOfDateField")
280         public Date getCountryEntryCreated () {
281                 return this.countryEntryCreated;
282         }
283
284         @Override
285         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
286         public void setCountryEntryCreated (final Date countryEntryCreated) {
287                 this.countryEntryCreated = countryEntryCreated;
288         }
289
290         @Override
291         @SuppressWarnings ("ReturnOfDateField")
292         public Date getCountryEntryUpdated () {
293                 return this.countryEntryUpdated;
294         }
295
296         @Override
297         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
298         public void setCountryEntryUpdated (final Date countryEntryUpdated) {
299                 this.countryEntryUpdated = countryEntryUpdated;
300         }
301
302         @Override
303         public String getCountryExternalDialPrefix () {
304                 return this.countryExternalDialPrefix;
305         }
306
307         @Override
308         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
309                 this.countryExternalDialPrefix = countryExternalDialPrefix;
310         }
311
312         @Override
313         public String getCountryI18nKey () {
314                 return this.countryI18nKey;
315         }
316
317         @Override
318         public void setCountryI18nKey (final String countryI18nKey) {
319                 this.countryI18nKey = countryI18nKey;
320         }
321
322         @Override
323         public Long getCountryId () {
324                 return this.countryId;
325         }
326
327         @Override
328         public void setCountryId (final Long countryId) {
329                 this.countryId = countryId;
330         }
331
332         @Override
333         public Boolean getCountryIsLocalPrefixRequired () {
334                 return this.countryIsLocalPrefixRequired;
335         }
336
337         @Override
338         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
339                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
340         }
341
342         @Override
343         public Short getCountryPhoneCode () {
344                 return this.countryPhoneCode;
345         }
346
347         @Override
348         public void setCountryPhoneCode (final Short countryPhoneCode) {
349                 this.countryPhoneCode = countryPhoneCode;
350         }
351
352         @Override
353         public int hashCode () {
354                 int hash = 7;
355
356                 hash = 41 * hash + Objects.hashCode(this.getCountryAbroadDialPrefix());
357                 hash = 41 * hash + Objects.hashCode(this.getCountryCode());
358                 hash = 41 * hash + Objects.hashCode(this.getCountryExternalDialPrefix());
359                 hash = 41 * hash + Objects.hashCode(this.getCountryI18nKey());
360                 hash = 41 * hash + Objects.hashCode(this.getCountryId());
361                 hash = 41 * hash + Objects.hashCode(this.getCountryIsLocalPrefixRequired());
362                 hash = 41 * hash + Objects.hashCode(this.getCountryPhoneCode());
363
364                 return hash;
365         }
366
367 }