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