2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcountry.model.data;
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;
39 * A POJO for country data
41 * @author Roland Häder<roland@mxchange.org>
43 @Entity (name = "country_data")
44 @Table (name = "country_data")
47 @NamedQuery (name = "AllCountries", query = "SELECT c FROM country_data AS c ORDER BY c.countryId ASC")
50 @SuppressWarnings ("PersistenceUnitPresent")
51 public class CountryData implements Country {
57 private static final long serialVersionUID = 14_853_982_718_509L;
60 * Dial prefix to be dialed before an abroad number is being dialed. In
61 * Germany this is "+" or 00.
63 @Basic (optional = false)
64 @Column (name = "country_abroad_dial_prefix", length = 10)
65 private String countryAbroadDialPrefix;
68 * 2-characters country code, all upper-case (example: DE for Germany, PH
71 @Basic (optional = false)
72 @Column (name = "country_code", length = 2, nullable = false, unique = true)
73 private String countryCode;
76 * TImestamp when this entry has been created
78 @Basic (optional = false)
79 @Temporal (TemporalType.TIMESTAMP)
80 @Column (name = "country_entry_created", nullable = false, updatable = false)
81 private Date countryEntryCreated;
84 * TImestamp when this entry has been updated
86 @Temporal (TemporalType.TIMESTAMP)
87 @Column (name = "country_entry_updated", insertable = false)
88 private Date countryEntryUpdated;
91 * Dial prefix to be dialed before the area/city number is dialed. In
94 @Basic (optional = false)
95 @Column (name = "country_external_dial_prefix", length = 10)
96 private String countryExternalDialPrefix;
99 * Key to i18n key (to have translated country names)
101 @Basic (optional = false)
102 @Column (name = "country_i18n_key", length = 100, nullable = false)
103 private String countryI18nKey;
109 @GeneratedValue (strategy = GenerationType.IDENTITY)
110 @Column (name = "country_id", nullable = false, updatable = false)
111 private Long countryId;
114 * Is the local dialing prefix required or optional for calling numbers in
117 @Basic (optional = false)
118 @Column (name = "country_is_local_prefix_required", nullable = false)
119 private Boolean countryIsLocalPrefixRequired;
122 * 2-digit country's phone code (example: 49 for Germany, 63 for
125 @Basic (optional = false)
126 @Column (name = "country_phone_code", length = 2, nullable = false, updatable = false)
127 private Short countryPhoneCode;
130 * Default constructor, required for the JPA
132 public CountryData () {
136 * Constructor with all required fields
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
144 * @param countryPhoneCode Phone code for country
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
150 // Are all parameter set?
151 if (null == countryAbroadDialPrefix) {
153 throw new NullPointerException("countryAbroadDialPrefix is null"); //NOI18N
154 } else if (countryAbroadDialPrefix.isEmpty()) {
156 throw new IllegalArgumentException("countryAbroadDialPrefix is empty"); //NOI18N
157 } else if (null == countryCode) {
159 throw new NullPointerException("countryCode is null"); //NOI18N
160 } else if (countryCode.isEmpty()) {
162 throw new IllegalArgumentException("countryCode is empty"); //NOI18N
163 } else if (null == countryExternalDialPrefix) {
165 throw new NullPointerException("countryExternalDialPrefix is null"); //NOI18N
166 } else if (countryExternalDialPrefix.isEmpty()) {
168 throw new IllegalArgumentException("countryExternalDialPrefix is empty"); //NOI18N
169 } else if (null == countryI18nKey) {
171 throw new NullPointerException("countryI18nKey is null"); //NOI18N
172 } else if (countryI18nKey.isEmpty()) {
174 throw new IllegalArgumentException("countryI18nKey is empty"); //NOI18N
175 } else if (null == countryIsLocalPrefixRequired) {
177 throw new NullPointerException("countryIsLocalPrefixRequired is null"); //NOI18N
178 } else if (null == countryPhoneCode) {
180 throw new NullPointerException("countryPhoneCode is null"); //NOI18N
181 } else if (countryPhoneCode < 1) {
183 throw new IllegalArgumentException(MessageFormat.format("countryPhoneCode={0} is not valid, must be at least 1.", countryPhoneCode)); //NOI18N
187 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
188 this.countryCode = countryCode;
189 this.countryExternalDialPrefix = countryExternalDialPrefix;
190 this.countryI18nKey = countryI18nKey;
191 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
192 this.countryPhoneCode = countryPhoneCode;
196 public int compareTo (final Country country) {
197 // Check parameter on null-reference and equality to this
198 if (null == country) {
200 throw new NullPointerException("country is null"); //NOI18N
201 } else if (country.equals(this)) {
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()),
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())
223 final int comparison = ComparableUtils.checkAll(comparators);
230 public boolean equals (final Object object) {
231 if (this == object) {
233 } else if (null == object) {
235 } else if (this.getClass() != object.getClass()) {
239 // Cast to Country interface
240 final Country country = (Country) object;
242 if (!Objects.equals(this.getCountryAbroadDialPrefix(), country.getCountryAbroadDialPrefix())) {
244 } else if (!Objects.equals(this.getCountryCode(), country.getCountryCode())) {
246 } else if (!Objects.equals(this.getCountryExternalDialPrefix(), country.getCountryExternalDialPrefix())) {
248 } else if (!Objects.equals(this.getCountryI18nKey(), country.getCountryI18nKey())) {
250 } else if (!Objects.equals(this.getCountryId(), country.getCountryId())) {
252 } else if (!Objects.equals(this.getCountryIsLocalPrefixRequired(), country.getCountryIsLocalPrefixRequired())) {
254 } else if (!Objects.equals(this.getCountryPhoneCode(), country.getCountryPhoneCode())) {
262 public String getCountryAbroadDialPrefix () {
263 return this.countryAbroadDialPrefix;
267 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
268 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
272 public String getCountryCode () {
273 return this.countryCode;
277 public void setCountryCode (final String countryCode) {
278 this.countryCode = countryCode;
282 @SuppressWarnings ("ReturnOfDateField")
283 public Date getCountryEntryCreated () {
284 return this.countryEntryCreated;
288 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
289 public void setCountryEntryCreated (final Date countryEntryCreated) {
290 this.countryEntryCreated = countryEntryCreated;
294 @SuppressWarnings ("ReturnOfDateField")
295 public Date getCountryEntryUpdated () {
296 return this.countryEntryUpdated;
300 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
301 public void setCountryEntryUpdated (final Date countryEntryUpdated) {
302 this.countryEntryUpdated = countryEntryUpdated;
306 public String getCountryExternalDialPrefix () {
307 return this.countryExternalDialPrefix;
311 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
312 this.countryExternalDialPrefix = countryExternalDialPrefix;
316 public String getCountryI18nKey () {
317 return this.countryI18nKey;
321 public void setCountryI18nKey (final String countryI18nKey) {
322 this.countryI18nKey = countryI18nKey;
326 public Long getCountryId () {
327 return this.countryId;
331 public void setCountryId (final Long countryId) {
332 this.countryId = countryId;
336 public Boolean getCountryIsLocalPrefixRequired () {
337 return this.countryIsLocalPrefixRequired;
341 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
342 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
346 public Short getCountryPhoneCode () {
347 return this.countryPhoneCode;
351 public void setCountryPhoneCode (final Short countryPhoneCode) {
352 this.countryPhoneCode = countryPhoneCode;
356 public int hashCode () {
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());