]> git.mxchange.org Git - jphone-core.git/blob - src/org/mxchange/jphone/model/phonenumbers/mobileprovider/CellphoneProvider.java
e58d2d70055f11cfd687ed0fb20816efdd529db3
[jphone-core.git] / src / org / mxchange / jphone / model / phonenumbers / mobileprovider / CellphoneProvider.java
1 /*
2  * Copyright (C) 2016 - 2022 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.Comparables;
39 import org.mxchange.jcoreutils.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 (provider.equals(this)) {
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 = Comparables.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.getProviderName(), mobileProvider.getProviderName())) {
211                         return false;
212                 } else if (!Objects.equals(this.getProviderCountry(), mobileProvider.getProviderCountry())) {
213                         return false;
214                 }
215
216                 return true;
217         }
218
219         @Override
220         public Country getProviderCountry () {
221                 return this.providerCountry;
222         }
223
224         @Override
225         public void setProviderCountry (final Country providerCountry) {
226                 this.providerCountry = providerCountry;
227         }
228
229         @Override
230         public Long getProviderDialPrefix () {
231                 return this.providerDialPrefix;
232         }
233
234         @Override
235         public void setProviderDialPrefix (final Long providerDialPrefix) {
236                 this.providerDialPrefix = providerDialPrefix;
237         }
238
239         @Override
240         @SuppressWarnings ("ReturnOfDateField")
241         public Date getProviderEntryCreated () {
242                 return this.providerEntryCreated;
243         }
244
245         @Override
246         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
247         public void setProviderEntryCreated (final Date providerEntryCreated) {
248                 this.providerEntryCreated = providerEntryCreated;
249         }
250
251         @Override
252         @SuppressWarnings ("ReturnOfDateField")
253         public Date getProviderEntryUpdated () {
254                 return this.providerEntryUpdated;
255         }
256
257         @Override
258         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
259         public void setProviderEntryUpdated (final Date providerEntryUpdated) {
260                 this.providerEntryUpdated = providerEntryUpdated;
261         }
262
263         @Override
264         public Long getProviderId () {
265                 return this.providerId;
266         }
267
268         @Override
269         public void setProviderId (final Long providerId) {
270                 this.providerId = providerId;
271         }
272
273         @Override
274         public String getProviderMailPattern () {
275                 return this.providerMailPattern;
276         }
277
278         @Override
279         public void setProviderMailPattern (final String providerMailPattern) {
280                 this.providerMailPattern = providerMailPattern;
281         }
282
283         @Override
284         public String getProviderName () {
285                 return this.providerName;
286         }
287
288         @Override
289         public void setProviderName (final String providerName) {
290                 this.providerName = providerName;
291         }
292
293         @Override
294         public int hashCode () {
295                 int hash = 7;
296
297                 hash = 19 * hash + Objects.hashCode(this.getProviderDialPrefix());
298                 hash = 19 * hash + Objects.hashCode(this.getProviderName());
299                 hash = 19 * hash + Objects.hashCode(this.getProviderCountry());
300
301                 return hash;
302         }
303
304 }