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;
38 * A POJO for country data
40 * @author Roland Häder<roland@mxchange.org>
42 @Entity (name = "country_data")
43 @Table (name = "country_data")
46 @NamedQuery (name = "AllCountries", query = "SELECT c FROM country_data AS c ORDER BY c.countryId ASC")
49 @SuppressWarnings ("PersistenceUnitPresent")
50 public class CountryData implements Country {
56 private static final long serialVersionUID = 14_853_982_718_509L;
59 * Dial prefix to be dialed before an abroad number is being dialed. In
60 * Germany this is "+" or 00.
62 @Basic (optional = false)
63 @Column (name = "country_abroad_dial_prefix", length = 10)
64 private String countryAbroadDialPrefix;
67 * 2-characters country code, all upper-case (example: DE for Germany, PH
70 @Basic (optional = false)
71 @Column (name = "country_code", length = 2, nullable = false, unique = true)
72 private String countryCode;
75 * TImestamp when this entry has been created
77 @Basic (optional = false)
78 @Temporal (TemporalType.TIMESTAMP)
79 @Column (name = "country_entry_created", nullable = false, updatable = false)
80 private Date countryEntryCreated;
83 * TImestamp when this entry has been updated
85 @Temporal (TemporalType.TIMESTAMP)
86 @Column (name = "country_entry_updated", insertable = false)
87 private Date countryEntryUpdated;
90 * Dial prefix to be dialed before the area/city number is dialed. In
93 @Basic (optional = false)
94 @Column (name = "country_external_dial_prefix", length = 10)
95 private String countryExternalDialPrefix;
98 * Key to i18n key (to have translated country names)
100 @Basic (optional = false)
101 @Column (name = "country_i18n_key", length = 100, nullable = false)
102 private String countryI18nKey;
108 @GeneratedValue (strategy = GenerationType.IDENTITY)
109 @Column (name = "country_id", nullable = false, updatable = false)
110 private Long countryId;
113 * Is the local dialing prefix required or optional for calling numbers in
116 @Basic (optional = false)
117 @Column (name = "country_is_local_prefix_required", nullable = false)
118 private Boolean countryIsLocalPrefixRequired;
121 * 2-digit country's phone code (example: 49 for Germany, 63 for
124 @Basic (optional = false)
125 @Column (name = "country_phone_code", length = 2, nullable = false, updatable = false)
126 private Short countryPhoneCode;
129 * Default constructor, required for the JPA
131 public CountryData () {
135 * Constructor with all required fields
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
143 * @param countryPhoneCode Phone code for country
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
149 // Are all parameter set?
150 if (null == countryAbroadDialPrefix) {
152 throw new NullPointerException("countryAbroadDialPrefix is null"); //NOI18N
153 } else if (countryAbroadDialPrefix.isEmpty()) {
155 throw new IllegalArgumentException("countryAbroadDialPrefix is empty"); //NOI18N
156 } else if (null == countryCode) {
158 throw new NullPointerException("countryCode is null"); //NOI18N
159 } else if (countryCode.isEmpty()) {
161 throw new IllegalArgumentException("countryCode is empty"); //NOI18N
162 } else if (null == countryExternalDialPrefix) {
164 throw new NullPointerException("countryExternalDialPrefix is null"); //NOI18N
165 } else if (countryExternalDialPrefix.isEmpty()) {
167 throw new IllegalArgumentException("countryExternalDialPrefix is empty"); //NOI18N
168 } else if (null == countryI18nKey) {
170 throw new NullPointerException("countryI18nKey is null"); //NOI18N
171 } else if (countryI18nKey.isEmpty()) {
173 throw new IllegalArgumentException("countryI18nKey is empty"); //NOI18N
174 } else if (null == countryIsLocalPrefixRequired) {
176 throw new NullPointerException("countryIsLocalPrefixRequired is null"); //NOI18N
177 } else if (null == countryPhoneCode) {
179 throw new NullPointerException("countryPhoneCode is null"); //NOI18N
180 } else if (countryPhoneCode < 1) {
182 throw new IllegalArgumentException(MessageFormat.format("countryPhoneCode={0} is not valid, must be at least 1.", countryPhoneCode)); //NOI18N
186 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
187 this.countryCode = countryCode;
188 this.countryExternalDialPrefix = countryExternalDialPrefix;
189 this.countryI18nKey = countryI18nKey;
190 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
191 this.countryPhoneCode = countryPhoneCode;
195 public int compareTo (final Country country) {
196 // Check parameter on null-reference and equality to this
197 if (null == country) {
199 throw new NullPointerException("country is null"); //NOI18N
200 } else if (country.equals(this)) {
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()),
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())
220 final int comparison = ComparableUtils.checkAll(comparators);
227 public boolean equals (final Object object) {
228 if (this == object) {
230 } else if (null == object) {
232 } else if (this.getClass() != object.getClass()) {
236 // @todo Maybe a bit unsafe cast?
237 final Country country = (Country) object;
239 if (!Objects.equals(this.getCountryAbroadDialPrefix(), country.getCountryAbroadDialPrefix())) {
241 } else if (!Objects.equals(this.getCountryCode(), country.getCountryCode())) {
243 } else if (!Objects.equals(this.getCountryExternalDialPrefix(), country.getCountryExternalDialPrefix())) {
245 } else if (!Objects.equals(this.getCountryI18nKey(), country.getCountryI18nKey())) {
247 } else if (!Objects.equals(this.getCountryId(), country.getCountryId())) {
249 } else if (!Objects.equals(this.getCountryIsLocalPrefixRequired(), country.getCountryIsLocalPrefixRequired())) {
251 } else if (!Objects.equals(this.getCountryPhoneCode(), country.getCountryPhoneCode())) {
259 public String getCountryAbroadDialPrefix () {
260 return this.countryAbroadDialPrefix;
264 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
265 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
269 public String getCountryCode () {
270 return this.countryCode;
274 public void setCountryCode (final String countryCode) {
275 this.countryCode = countryCode;
279 @SuppressWarnings ("ReturnOfDateField")
280 public Date getCountryEntryCreated () {
281 return this.countryEntryCreated;
285 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
286 public void setCountryEntryCreated (final Date countryEntryCreated) {
287 this.countryEntryCreated = countryEntryCreated;
291 @SuppressWarnings ("ReturnOfDateField")
292 public Date getCountryEntryUpdated () {
293 return this.countryEntryUpdated;
297 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
298 public void setCountryEntryUpdated (final Date countryEntryUpdated) {
299 this.countryEntryUpdated = countryEntryUpdated;
303 public String getCountryExternalDialPrefix () {
304 return this.countryExternalDialPrefix;
308 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
309 this.countryExternalDialPrefix = countryExternalDialPrefix;
313 public String getCountryI18nKey () {
314 return this.countryI18nKey;
318 public void setCountryI18nKey (final String countryI18nKey) {
319 this.countryI18nKey = countryI18nKey;
323 public Long getCountryId () {
324 return this.countryId;
328 public void setCountryId (final Long countryId) {
329 this.countryId = countryId;
333 public Boolean getCountryIsLocalPrefixRequired () {
334 return this.countryIsLocalPrefixRequired;
338 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
339 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
343 public Short getCountryPhoneCode () {
344 return this.countryPhoneCode;
348 public void setCountryPhoneCode (final Short countryPhoneCode) {
349 this.countryPhoneCode = countryPhoneCode;
353 public int hashCode () {
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());