2 * Copyright (C) 2016 - 2020 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.Comparables;
38 import org.mxchange.jcountry.model.data.Country;
39 import org.mxchange.jcountry.model.data.CountryData;
42 * A POJO for dialable fax numbers
44 * @author Roland Häder<roland@mxchange.org>
46 @Entity (name = "fax_numbers")
47 @Table (name = "fax_numbers")
50 @NamedQuery (name = "AllFaxNumbers", query = "SELECT f FROM fax_numbers AS f ORDER BY f.phoneId ASC")
53 @SuppressWarnings ("PersistenceUnitPresent")
54 public class FaxNumber implements DialableFaxNumber {
60 private static final long serialVersionUID = 17_687_687_191_375L;
63 * Area code (example: 2151 for Krefeld)
65 @Basic (optional = false)
66 @Column (name = "fax_area_code", length = 10, nullable = false)
67 private Integer faxAreaCode;
70 * Country instance ('s dial data)
72 @JoinColumn (name = "fax_country_id", nullable = false)
73 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
74 private Country faxCountry;
77 * Timestamp when this entry has been created
79 @Basic (optional = false)
80 @Temporal (TemporalType.TIMESTAMP)
81 @Column (name = "fax_entry_created", nullable = false, updatable = false)
82 private Date phoneEntryCreated;
85 * Timestamp when this entry has been created
87 @Temporal (TemporalType.TIMESTAMP)
88 @Column (name = "fax_entry_updated", insertable = false)
89 private Date phoneEntryUpdated;
95 @GeneratedValue (strategy = GenerationType.IDENTITY)
96 @Column (name = "fax_id", nullable = false, updatable = false)
100 * Fax number without prefix
102 @Basic (optional = false)
103 @Column (name = "fax_number", nullable = false)
104 private Long phoneNumber;
107 * Default constructor
109 public FaxNumber () {
113 * Constructor with country, area code and number
115 * @param faxCountry Country instance
116 * @param faxAreaCode Area code (without leading zeros)
117 * @param faxNumber Fax number (without area code and leading zero)
119 public FaxNumber (final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
120 // Invoke default constructor
123 // Are all parameter set?
124 if (null == faxAreaCode) {
126 throw new NullPointerException("faxAreaCode is null"); //NOI18N
127 } else if (faxAreaCode < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("faxAreaCode={0} is not valid.", faxAreaCode)); //NOI18N
130 } else if (null == faxCountry) {
132 throw new NullPointerException("faxCountry is null"); //NOI18N
133 } else if (null == faxCountry.getCountryId()) {
135 throw new NullPointerException("faxCountry.countryId is null"); //NOI18N
136 } else if (faxCountry.getCountryId() < 1) {
138 throw new IllegalArgumentException(MessageFormat.format("faxCountry.countryId={0} is not valid", faxCountry.getCountryId())); //NOI18N
139 } else if (null == faxNumber) {
141 throw new NullPointerException("faxNumber is null"); //NOI18N
142 } else if (faxNumber < 1) {
144 throw new IllegalArgumentException(MessageFormat.format("faxNumber={0} is not valid.", faxNumber)); //NOI18N
148 this.faxCountry = faxCountry;
149 this.faxAreaCode = faxAreaCode;
150 this.phoneNumber = faxNumber;
154 public int compareTo (final DialableFaxNumber otherNumber) {
155 // Is the parameter given?
156 if (null == otherNumber) {
158 throw new NullPointerException("otherNumber is null"); //NOI18N
159 } else if (otherNumber.equals(this)) {
165 final int[] comparators = {
167 this.getPhoneCountry().compareTo(otherNumber.getPhoneCountry()),
169 this.getPhoneAreaCode().compareTo(otherNumber.getPhoneAreaCode()),
171 this.getPhoneNumber().compareTo(otherNumber.getPhoneNumber())
175 final int comparison = Comparables.checkAll(comparators);
182 public boolean equals (final Object object) {
183 if (null == object) {
185 } else if (this.getClass() != object.getClass()) {
189 final DialableFaxNumber dialableFaxNumber = (DialableFaxNumber) object;
191 if (!Objects.equals(this.getPhoneNumber(), dialableFaxNumber.getPhoneNumber())) {
193 } else if (!Objects.equals(this.getPhoneId(), dialableFaxNumber.getPhoneId())) {
195 } else if (!Objects.equals(this.getPhoneAreaCode(), dialableFaxNumber.getPhoneAreaCode())) {
197 } else if (!Objects.equals(this.getPhoneCountry(), dialableFaxNumber.getPhoneCountry())) {
205 public Integer getPhoneAreaCode () {
206 return this.faxAreaCode;
210 public void setPhoneAreaCode (final Integer faxAreaCode) {
211 this.faxAreaCode = faxAreaCode;
215 public Country getPhoneCountry () {
216 return this.faxCountry;
220 public void setPhoneCountry (final Country faxCountry) {
221 this.faxCountry = faxCountry;
225 @SuppressWarnings ("ReturnOfDateField")
226 public Date getPhoneEntryCreated () {
227 return this.phoneEntryCreated;
231 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
232 public void setPhoneEntryCreated (final Date phoneEntryCreated) {
233 this.phoneEntryCreated = phoneEntryCreated;
237 @SuppressWarnings ("ReturnOfDateField")
238 public Date getPhoneEntryUpdated () {
239 return this.phoneEntryUpdated;
243 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
244 public void setPhoneEntryUpdated (final Date phoneEntryUpdated) {
245 this.phoneEntryUpdated = phoneEntryUpdated;
249 public Long getPhoneId () {
254 public void setPhoneId (final Long phoneId) {
255 this.phoneId = phoneId;
259 public Long getPhoneNumber () {
260 return this.phoneNumber;
264 public void setPhoneNumber (final Long phoneNumber) {
265 this.phoneNumber = phoneNumber;
269 public int hashCode () {
272 hash = 23 * hash + Objects.hashCode(this.getPhoneId());
273 hash = 23 * hash + Objects.hashCode(this.getPhoneNumber());
274 hash = 23 * hash + Objects.hashCode(this.getPhoneAreaCode());
275 hash = 23 * hash + Objects.hashCode(this.getPhoneCountry());