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.jphone.model.phonenumbers.fax;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37 import org.mxchange.jcoreutils.comparable.ComparableUtils;
38 import org.mxchange.jcountry.model.data.Country;
39 import org.mxchange.jcountry.model.data.CountryData;
40 import org.mxchange.jcountry.model.utils.CountryUtils;
43 * A POJO for dialable fax numbers
45 * @author Roland Häder<roland@mxchange.org>
47 @Entity (name = "fax_numbers")
48 @Table (name = "fax_numbers")
51 @NamedQuery (name = "AllFaxNumbers", query = "SELECT f FROM fax_numbers AS f ORDER BY f.phoneId ASC")
54 @SuppressWarnings ("PersistenceUnitPresent")
55 public class FaxNumber implements DialableFaxNumber {
61 private static final long serialVersionUID = 17_687_687_191_375L;
64 * Area code (example: 2151 for Krefeld)
66 @Basic (optional = false)
67 @Column (name = "fax_area_code", length = 10, nullable = false)
68 private Integer faxAreaCode;
71 * Country instance ('s dial data)
73 @JoinColumn (name = "fax_country_id", nullable = false)
74 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
75 private Country faxCountry;
78 * Timestamp when this entry has been created
80 @Basic (optional = false)
81 @Temporal (TemporalType.TIMESTAMP)
82 @Column (name = "fax_entry_created", nullable = false, updatable = false)
83 private Date phoneEntryCreated;
86 * Timestamp when this entry has been created
88 @Temporal (TemporalType.TIMESTAMP)
89 @Column (name = "fax_entry_updated", insertable = false)
90 private Date phoneEntryUpdated;
96 @GeneratedValue (strategy = GenerationType.IDENTITY)
97 @Column (name = "fax_id", nullable = false, updatable = false)
101 * Fax number without prefix
103 @Basic (optional = false)
104 @Column (name = "fax_number", nullable = false)
105 private Long phoneNumber;
108 * Default constructor
110 public FaxNumber () {
114 * Constructor with country, area code and number
116 * @param faxCountry Country instance
117 * @param faxAreaCode Area code (without leading zeros)
118 * @param faxNumber Fax number (without area code and leading zero)
120 public FaxNumber (final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
121 // Invoke default constructor
124 // Are all parameter set?
125 if (null == faxAreaCode) {
127 throw new NullPointerException("faxAreaCode is null"); //NOI18N
128 } else if (faxAreaCode < 1) {
130 throw new IllegalArgumentException(MessageFormat.format("faxAreaCode={0} is not valid.", faxAreaCode)); //NOI18N
131 } else if (null == faxCountry) {
133 throw new NullPointerException("faxCountry is null"); //NOI18N
134 } else if (null == faxCountry.getCountryId()) {
136 throw new NullPointerException("faxCountry.countryId is null"); //NOI18N
137 } else if (faxCountry.getCountryId() < 1) {
139 throw new IllegalArgumentException(MessageFormat.format("faxCountry.countryId={0} is not valid", faxCountry.getCountryId())); //NOI18N
140 } else if (null == faxNumber) {
142 throw new NullPointerException("faxNumber is null"); //NOI18N
143 } else if (faxNumber < 1) {
145 throw new IllegalArgumentException(MessageFormat.format("faxNumber={0} is not valid.", faxNumber)); //NOI18N
149 this.faxCountry = faxCountry;
150 this.faxAreaCode = faxAreaCode;
151 this.phoneNumber = faxNumber;
155 public int compareTo (final DialableFaxNumber faxNumber) {
156 // Is the parameter given?
157 if (null == faxNumber) {
159 throw new NullPointerException("faxNumber is null"); //NOI18N
160 } else if (Objects.equals(this, faxNumber)) {
166 final int[] comparators = {
168 CountryUtils.compare(this.getPhoneCountry(), faxNumber.getPhoneCountry()),
170 this.getPhoneAreaCode().compareTo(faxNumber.getPhoneAreaCode()),
172 this.getPhoneNumber().compareTo(faxNumber.getPhoneNumber())
176 final int comparison = ComparableUtils.checkAll(comparators);
183 public boolean equals (final Object object) {
184 if (null == object) {
186 } else if (this.getClass() != object.getClass()) {
190 final DialableFaxNumber dialableFaxNumber = (DialableFaxNumber) object;
192 if (!Objects.equals(this.getPhoneNumber(), dialableFaxNumber.getPhoneNumber())) {
194 } else if (!Objects.equals(this.getPhoneId(), dialableFaxNumber.getPhoneId())) {
196 } else if (!Objects.equals(this.getPhoneAreaCode(), dialableFaxNumber.getPhoneAreaCode())) {
198 } else if (!Objects.equals(this.getPhoneCountry(), dialableFaxNumber.getPhoneCountry())) {
206 public Integer getPhoneAreaCode () {
207 return this.faxAreaCode;
211 public void setPhoneAreaCode (final Integer faxAreaCode) {
212 this.faxAreaCode = faxAreaCode;
216 public Country getPhoneCountry () {
217 return this.faxCountry;
221 public void setPhoneCountry (final Country faxCountry) {
222 this.faxCountry = faxCountry;
226 @SuppressWarnings ("ReturnOfDateField")
227 public Date getPhoneEntryCreated () {
228 return this.phoneEntryCreated;
232 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
233 public void setPhoneEntryCreated (final Date phoneEntryCreated) {
234 this.phoneEntryCreated = phoneEntryCreated;
238 @SuppressWarnings ("ReturnOfDateField")
239 public Date getPhoneEntryUpdated () {
240 return this.phoneEntryUpdated;
244 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
245 public void setPhoneEntryUpdated (final Date phoneEntryUpdated) {
246 this.phoneEntryUpdated = phoneEntryUpdated;
250 public Long getPhoneId () {
255 public void setPhoneId (final Long phoneId) {
256 this.phoneId = phoneId;
260 public Long getPhoneNumber () {
261 return this.phoneNumber;
265 public void setPhoneNumber (final Long phoneNumber) {
266 this.phoneNumber = phoneNumber;
270 public int hashCode () {
273 hash = 23 * hash + Objects.hashCode(this.getPhoneId());
274 hash = 23 * hash + Objects.hashCode(this.getPhoneNumber());
275 hash = 23 * hash + Objects.hashCode(this.getPhoneAreaCode());
276 hash = 23 * hash + Objects.hashCode(this.getPhoneCountry());