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