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