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.landline;
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 phone numbers
44 * @author Roland Häder<roland@mxchange.org>
46 @Entity (name = "landline_numbers")
47 @Table (name = "landline_numbers")
50 @NamedQuery (name = "AllLandLineNumbers", query = "SELECT p FROM landline_numbers AS p ORDER BY p.phoneId ASC")
53 @SuppressWarnings ("PersistenceUnitPresent")
54 public class LandLineNumber implements DialableLandLineNumber {
60 private static final long serialVersionUID = 18_563_748_781_956L;
63 * Area code (example: 2151 for Krefeld)
65 @Basic (optional = false)
66 @Column (name = "landline_area_code", length = 10, nullable = false)
67 private Integer phoneAreaCode;
70 * Connection to table "country_data"
72 @JoinColumn (name = "landline_country_id", nullable = false, updatable = false)
73 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
74 private Country phoneCountry;
77 * Timestamp when this entry has been created
79 @Basic (optional = false)
80 @Temporal (TemporalType.TIMESTAMP)
81 @Column (name = "landline_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 = "landline_entry_updated", insertable = false)
89 private Date phoneEntryUpdated;
95 @GeneratedValue (strategy = GenerationType.IDENTITY)
96 @Column (name = "landline_id", length = 10, nullable = false, updatable = false)
100 * Phone number with out dial prefix
102 @Basic (optional = false)
103 @Column (name = "landline_number", nullable = false)
104 private Long phoneNumber;
107 * Default constructor
109 public LandLineNumber () {
113 * Constructor with country instance, area code and number
115 * @param landLineCountry An instance of a Country class
116 * @param landLineAreaCode Land-line area code
117 * @param landLineNumber Land-line number
119 public LandLineNumber (final Country landLineCountry, final Integer landLineAreaCode, final Long landLineNumber) {
120 // Invoke default constructor
123 // Are all parameter set?
124 if (null == landLineAreaCode) {
126 throw new NullPointerException("landLineAreaCode is null"); //NOI18N
127 } else if (landLineAreaCode < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("landLineAreaCode={0} is not valid.", landLineAreaCode)); //NOI18N
130 } else if (null == landLineCountry) {
132 throw new NullPointerException("landLineCountry is null"); //NOI18N
133 } else if (null == landLineCountry.getCountryId()) {
135 throw new NullPointerException("landLineCountry.countryId is null"); //NOI18N
136 } else if (landLineCountry.getCountryId() < 1) {
138 throw new IllegalArgumentException(MessageFormat.format("landLineCountry.countryId={0} is not valid", landLineCountry.getCountryId())); //NOI18N
139 } else if (null == landLineNumber) {
141 throw new NullPointerException("landLineNumber is null"); //NOI18N
142 } else if (landLineNumber < 1) {
144 throw new IllegalArgumentException(MessageFormat.format("landLineNumber={0} is not valid.", landLineNumber)); //NOI18N
148 this.phoneCountry = landLineCountry;
149 this.phoneAreaCode = landLineAreaCode;
150 this.phoneNumber = landLineNumber;
154 public int compareTo (final DialableLandLineNumber 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 DialableLandLineNumber landLineNumber = (DialableLandLineNumber) object;
191 if (!Objects.equals(this.getPhoneId(), landLineNumber.getPhoneId())) {
193 } else if (!Objects.equals(this.getPhoneNumber(), landLineNumber.getPhoneNumber())) {
195 } else if (!Objects.equals(this.getPhoneAreaCode(), landLineNumber.getPhoneAreaCode())) {
197 } else if (!Objects.equals(this.getPhoneCountry(), landLineNumber.getPhoneCountry())) {
205 public Integer getPhoneAreaCode () {
206 return this.phoneAreaCode;
210 public void setPhoneAreaCode (final Integer phoneAreaCode) {
211 this.phoneAreaCode = phoneAreaCode;
215 public Country getPhoneCountry () {
216 return this.phoneCountry;
220 public void setPhoneCountry (final Country phoneCountry) {
221 this.phoneCountry = phoneCountry;
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 = 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());