]> git.mxchange.org Git - jphone-core.git/blob - src/org/mxchange/jphone/model/phonenumbers/mobileprovider/CellphoneProvider.java
Updated copyright year
[jphone-core.git] / src / org / mxchange / jphone / model / phonenumbers / mobileprovider / CellphoneProvider.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.mobileprovider;
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.apache.commons.lang3.StringUtils;
38 import org.mxchange.jcoreutils.comparable.ComparableUtils;
39 import org.mxchange.jcoreutils.number.SafeNumberUtils;
40 import org.mxchange.jcountry.model.data.Country;
41 import org.mxchange.jcountry.model.data.CountryData;
42 import org.mxchange.jcountry.model.utils.CountryUtils;
43
44 /**
45  * A POJO for mobile providers
46  * <p>
47  * @author Roland Häder<roland@mxchange.org>
48  */
49 @Entity (name = "mobile_provider")
50 @Table (name = "mobile_provider")
51 @NamedQueries (
52                 @NamedQuery (name = "AllMobileProvider", query = "SELECT p FROM mobile_provider AS p ORDER BY p.providerId ASC")
53 )
54 @SuppressWarnings ("PersistenceUnitPresent")
55 public class CellphoneProvider implements MobileProvider {
56
57         /**
58          * Serial number
59          */
60         @Transient
61         private static final long serialVersionUID = 185_878_567_810_590L;
62
63         /**
64          * Country instance ('s dial data)
65          */
66         @JoinColumn (name = "provider_country_id", nullable = false)
67         @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
68         private Country providerCountry;
69
70         /**
71          * Provider dial prefix (example: 0177 for German E+)
72          */
73         @Basic (optional = false)
74         @Column (name = "provider_dial_prefix", length = 10, nullable = false)
75         private Long providerDialPrefix;
76
77         /**
78          * Timestamp when this entry has been created
79          */
80         @Basic (optional = false)
81         @Temporal (TemporalType.TIMESTAMP)
82         @Column (name = "provider_entry_created", nullable = false, updatable = false)
83         private Date providerEntryCreated;
84
85         /**
86          * Timestamp when this entry has been created
87          */
88         @Temporal (TemporalType.TIMESTAMP)
89         @Column (name = "provider_entry_updated", insertable = false)
90         private Date providerEntryUpdated;
91
92         /**
93          * Id number
94          */
95         @Id
96         @GeneratedValue (strategy = GenerationType.IDENTITY)
97         @Column (name = "provider_id", nullable = false, updatable = false)
98         private Long providerId;
99
100         /**
101          * Pattern for mail gateway
102          */
103         @Column (name = "provider_mail_pattern", length = 30)
104         private String providerMailPattern;
105
106         /**
107          * Name of the provider
108          */
109         @Basic (optional = false)
110         @Column (name = "provider_name", length = 30, nullable = false)
111         private String providerName;
112
113         /**
114          * Default constructor
115          */
116         public CellphoneProvider () {
117         }
118
119         /**
120          * Constructor with all required data
121          * <p>
122          * @param providerDialPrefix  Dial prefix
123          * @param providerName        Name
124          * @param providerCountry     An instance of a Country class
125          * @param providerMailPattern Pattern for email
126          */
127         public CellphoneProvider (final Long providerDialPrefix, final String providerName, final Country providerCountry, final String providerMailPattern) {
128                 // Invoke default constructor
129                 this();
130
131                 // Are all parameter set?
132                 if (null == providerDialPrefix) {
133                         // Throw NPE
134                         throw new NullPointerException("providerDialPrefix is null"); //NOI18N
135                 } else if (providerDialPrefix < 1) {
136                         // Throw IAE
137                         throw new IllegalArgumentException(MessageFormat.format("providerDialPrefix={0} is not valid.", providerDialPrefix)); //NOI18N
138                 } else if (null == providerName) {
139                         // Throw NPE
140                         throw new NullPointerException("providerName is null"); //NOI18N
141                 } else if (providerName.isEmpty()) {
142                         // Throw IAE
143                         throw new IllegalArgumentException("providerName is empty"); //NOI18N
144                 } else if (null == providerCountry) {
145                         // Throw NPE
146                         throw new NullPointerException("providerCountry is null"); //NOI18N
147                 } else if (null == providerCountry.getCountryId()) {
148                         // Throw NPE again
149                         throw new NullPointerException("providerCountry.countryId is null"); //NOI18N
150                 } else if (providerCountry.getCountryId() < 1) {
151                         // Throw IAE
152                         throw new IllegalArgumentException(MessageFormat.format("providerCountry.countryId={0} is not valid", providerCountry.getCountryId())); //NOI18N
153                 } else if (null == providerMailPattern) {
154                         // Throw NPE
155                         throw new NullPointerException("providerMailPattern is null"); //NOI18N
156                 } else if (providerMailPattern.isEmpty()) {
157                         // Throw IAE
158                         throw new IllegalArgumentException("providerMailPattern is empty"); //NOI18N
159                 }
160
161                 // Set all values
162                 this.providerDialPrefix = providerDialPrefix;
163                 this.providerName = providerName;
164                 this.providerCountry = providerCountry;
165                 this.providerMailPattern = providerMailPattern;
166         }
167
168         @Override
169         public int compareTo (final MobileProvider provider) {
170                 // Is the parameter given?
171                 if (null == provider) {
172                         // Throw NPE
173                         throw new NullPointerException("Parameter 'provider' is null"); //NOI18N
174                 } else if (Objects.equals(this, provider)) {
175                         // Same object
176                         return 0;
177                 }
178
179                 // Init comparisons
180                 final int[] comparators = {
181                         // First provider country
182                         CountryUtils.compare(this.getProviderCountry(), provider.getProviderCountry()),
183                         // ... provider name
184                         StringUtils.compare(this.getProviderName(), provider.getProviderName()),
185                         // ... dial prefix
186                         SafeNumberUtils.compare(this.getProviderDialPrefix(), provider.getProviderDialPrefix()),
187                         // ... mail pattern
188                         StringUtils.compare(this.getProviderMailPattern(), provider.getProviderMailPattern())
189                 };
190
191                 // Check all values
192                 final int comparison = ComparableUtils.checkAll(comparators);
193
194                 // Return value
195                 return comparison;
196         }
197
198         @Override
199         public boolean equals (final Object object) {
200                 if (null == object) {
201                         return false;
202                 } else if (this.getClass() != object.getClass()) {
203                         return false;
204                 }
205
206                 final MobileProvider mobileProvider = (MobileProvider) object;
207
208                 if (!Objects.equals(this.getProviderDialPrefix(), mobileProvider.getProviderDialPrefix())) {
209                         return false;
210                 } else if (!Objects.equals(this.getProviderId(), mobileProvider.getProviderId())) {
211                         return false;
212                 } else if (!Objects.equals(this.getProviderMailPattern(), mobileProvider.getProviderMailPattern())) {
213                         return false;
214                 } else if (!Objects.equals(this.getProviderName(), mobileProvider.getProviderName())) {
215                         return false;
216                 } else if (!Objects.equals(this.getProviderCountry(), mobileProvider.getProviderCountry())) {
217                         return false;
218                 }
219
220                 return true;
221         }
222
223         @Override
224         public Country getProviderCountry () {
225                 return this.providerCountry;
226         }
227
228         @Override
229         public void setProviderCountry (final Country providerCountry) {
230                 this.providerCountry = providerCountry;
231         }
232
233         @Override
234         public Long getProviderDialPrefix () {
235                 return this.providerDialPrefix;
236         }
237
238         @Override
239         public void setProviderDialPrefix (final Long providerDialPrefix) {
240                 this.providerDialPrefix = providerDialPrefix;
241         }
242
243         @Override
244         @SuppressWarnings ("ReturnOfDateField")
245         public Date getProviderEntryCreated () {
246                 return this.providerEntryCreated;
247         }
248
249         @Override
250         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
251         public void setProviderEntryCreated (final Date providerEntryCreated) {
252                 this.providerEntryCreated = providerEntryCreated;
253         }
254
255         @Override
256         @SuppressWarnings ("ReturnOfDateField")
257         public Date getProviderEntryUpdated () {
258                 return this.providerEntryUpdated;
259         }
260
261         @Override
262         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
263         public void setProviderEntryUpdated (final Date providerEntryUpdated) {
264                 this.providerEntryUpdated = providerEntryUpdated;
265         }
266
267         @Override
268         public Long getProviderId () {
269                 return this.providerId;
270         }
271
272         @Override
273         public void setProviderId (final Long providerId) {
274                 this.providerId = providerId;
275         }
276
277         @Override
278         public String getProviderMailPattern () {
279                 return this.providerMailPattern;
280         }
281
282         @Override
283         public void setProviderMailPattern (final String providerMailPattern) {
284                 this.providerMailPattern = providerMailPattern;
285         }
286
287         @Override
288         public String getProviderName () {
289                 return this.providerName;
290         }
291
292         @Override
293         public void setProviderName (final String providerName) {
294                 this.providerName = providerName;
295         }
296
297         @Override
298         public int hashCode () {
299                 int hash = 7;
300
301                 hash = 19 * hash + Objects.hashCode(this.getProviderDialPrefix());
302                 hash = 19 * hash + Objects.hashCode(this.getProviderId());
303                 hash = 19 * hash + Objects.hashCode(this.getProviderMailPattern());
304                 hash = 19 * hash + Objects.hashCode(this.getProviderName());
305                 hash = 19 * hash + Objects.hashCode(this.getProviderCountry());
306
307                 return hash;
308         }
309
310 }