]> git.mxchange.org Git - jphone-core.git/blob - src/org/mxchange/jphone/model/phonenumbers/fax/FaxNumber.java
Updated copyright year
[jphone-core.git] / src / org / mxchange / jphone / model / phonenumbers / fax / FaxNumber.java
1 /*
2  * Copyright (C) 2016 - 2024 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.jphone.model.phonenumbers.fax;
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.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;
41
42 /**
43  * A POJO for dialable fax numbers
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Entity (name = "fax_numbers")
48 @Table (name = "fax_numbers")
49 @NamedQueries (
50                 {
51                         @NamedQuery (name = "AllFaxNumbers", query = "SELECT f FROM fax_numbers AS f ORDER BY f.phoneId ASC")
52                 }
53 )
54 @SuppressWarnings ("PersistenceUnitPresent")
55 public class FaxNumber implements DialableFaxNumber {
56
57         /**
58          * Serial number
59          */
60         @Transient
61         private static final long serialVersionUID = 17_687_687_191_375L;
62
63         /**
64          * Area code (example: 2151 for Krefeld)
65          */
66         @Basic (optional = false)
67         @Column (name = "fax_area_code", length = 10, nullable = false)
68         private Integer faxAreaCode;
69
70         /**
71          * Country instance ('s dial data)
72          */
73         @JoinColumn (name = "fax_country_id", nullable = false)
74         @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
75         private Country faxCountry;
76
77         /**
78          * Timestamp when this entry has been created
79          */
80         @Basic (optional = false)
81         @Temporal (TemporalType.TIMESTAMP)
82         @Column (name = "fax_entry_created", nullable = false, updatable = false)
83         private Date phoneEntryCreated;
84
85         /**
86          * Timestamp when this entry has been created
87          */
88         @Temporal (TemporalType.TIMESTAMP)
89         @Column (name = "fax_entry_updated", insertable = false)
90         private Date phoneEntryUpdated;
91
92         /**
93          * Id number
94          */
95         @Id
96         @GeneratedValue (strategy = GenerationType.IDENTITY)
97         @Column (name = "fax_id", nullable = false, updatable = false)
98         private Long phoneId;
99
100         /**
101          * Fax number without prefix
102          */
103         @Basic (optional = false)
104         @Column (name = "fax_number", nullable = false)
105         private Long phoneNumber;
106
107         /**
108          * Default constructor
109          */
110         public FaxNumber () {
111         }
112
113         /**
114          * Constructor with country, area code and number
115          * <p>
116          * @param faxCountry  Country instance
117          * @param faxAreaCode Area code (without leading zeros)
118          * @param faxNumber   Fax number (without area code and leading zero)
119          */
120         public FaxNumber (final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
121                 // Invoke default constructor
122                 this();
123
124                 // Are all parameter set?
125                 if (null == faxAreaCode) {
126                         // Throw NPE
127                         throw new NullPointerException("faxAreaCode is null"); //NOI18N
128                 } else if (faxAreaCode < 1) {
129                         // Throw IAE
130                         throw new IllegalArgumentException(MessageFormat.format("faxAreaCode={0} is not valid.", faxAreaCode)); //NOI18N
131                 } else if (null == faxCountry) {
132                         // Throw NPE
133                         throw new NullPointerException("faxCountry is null"); //NOI18N
134                 } else if (null == faxCountry.getCountryId()) {
135                         // Throw NPE again
136                         throw new NullPointerException("faxCountry.countryId is null"); //NOI18N
137                 } else if (faxCountry.getCountryId() < 1) {
138                         // Throw IAE
139                         throw new IllegalArgumentException(MessageFormat.format("faxCountry.countryId={0} is not valid", faxCountry.getCountryId())); //NOI18N
140                 } else if (null == faxNumber) {
141                         // Throw NPE
142                         throw new NullPointerException("faxNumber is null"); //NOI18N
143                 } else if (faxNumber < 1) {
144                         // Throw IAE
145                         throw new IllegalArgumentException(MessageFormat.format("faxNumber={0} is not valid.", faxNumber)); //NOI18N
146                 }
147
148                 // Set all values
149                 this.faxCountry = faxCountry;
150                 this.faxAreaCode = faxAreaCode;
151                 this.phoneNumber = faxNumber;
152         }
153
154         @Override
155         public int compareTo (final DialableFaxNumber faxNumber) {
156                 // Is the parameter given?
157                 if (null == faxNumber) {
158                         // Throw NPE
159                         throw new NullPointerException("faxNumber is null"); //NOI18N
160                 } else if (Objects.equals(this, faxNumber)) {
161                         // Same object
162                         return 0;
163                 }
164
165                 // Init comparisons
166                 final int[] comparators = {
167                         // First country
168                         CountryUtils.compare(this.getPhoneCountry(), faxNumber.getPhoneCountry()),
169                         // Next area code
170                         this.getPhoneAreaCode().compareTo(faxNumber.getPhoneAreaCode()),
171                         // Last number
172                         this.getPhoneNumber().compareTo(faxNumber.getPhoneNumber())
173                 };
174
175                 // Check all values
176                 final int comparison = ComparableUtils.checkAll(comparators);
177
178                 // Return value
179                 return comparison;
180         }
181
182         @Override
183         public boolean equals (final Object object) {
184                 if (null == object) {
185                         return false;
186                 } else if (this.getClass() != object.getClass()) {
187                         return false;
188                 }
189
190                 final DialableFaxNumber dialableFaxNumber = (DialableFaxNumber) object;
191
192                 if (!Objects.equals(this.getPhoneNumber(), dialableFaxNumber.getPhoneNumber())) {
193                         return false;
194                 } else if (!Objects.equals(this.getPhoneId(), dialableFaxNumber.getPhoneId())) {
195                         return false;
196                 } else if (!Objects.equals(this.getPhoneAreaCode(), dialableFaxNumber.getPhoneAreaCode())) {
197                         return false;
198                 } else if (!Objects.equals(this.getPhoneCountry(), dialableFaxNumber.getPhoneCountry())) {
199                         return false;
200                 }
201
202                 return true;
203         }
204
205         @Override
206         public Integer getPhoneAreaCode () {
207                 return this.faxAreaCode;
208         }
209
210         @Override
211         public void setPhoneAreaCode (final Integer faxAreaCode) {
212                 this.faxAreaCode = faxAreaCode;
213         }
214
215         @Override
216         public Country getPhoneCountry () {
217                 return this.faxCountry;
218         }
219
220         @Override
221         public void setPhoneCountry (final Country faxCountry) {
222                 this.faxCountry = faxCountry;
223         }
224
225         @Override
226         @SuppressWarnings ("ReturnOfDateField")
227         public Date getPhoneEntryCreated () {
228                 return this.phoneEntryCreated;
229         }
230
231         @Override
232         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
233         public void setPhoneEntryCreated (final Date phoneEntryCreated) {
234                 this.phoneEntryCreated = phoneEntryCreated;
235         }
236
237         @Override
238         @SuppressWarnings ("ReturnOfDateField")
239         public Date getPhoneEntryUpdated () {
240                 return this.phoneEntryUpdated;
241         }
242
243         @Override
244         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
245         public void setPhoneEntryUpdated (final Date phoneEntryUpdated) {
246                 this.phoneEntryUpdated = phoneEntryUpdated;
247         }
248
249         @Override
250         public Long getPhoneId () {
251                 return this.phoneId;
252         }
253
254         @Override
255         public void setPhoneId (final Long phoneId) {
256                 this.phoneId = phoneId;
257         }
258
259         @Override
260         public Long getPhoneNumber () {
261                 return this.phoneNumber;
262         }
263
264         @Override
265         public void setPhoneNumber (final Long phoneNumber) {
266                 this.phoneNumber = phoneNumber;
267         }
268
269         @Override
270         public int hashCode () {
271                 int hash = 3;
272
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());
277
278                 return hash;
279         }
280
281 }