]> 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          * Id number
85          */
86         @Id
87         @GeneratedValue (strategy = GenerationType.IDENTITY)
88         @Column (name = "provider_id", nullable = false, updatable = false)
89         private Long providerId;
90
91         /**
92          * Pattern for mail gateway
93          */
94         @Column (name = "provider_mail_pattern", length = 30)
95         private String providerMailPattern;
96
97         /**
98          * Name of the provider
99          */
100         @Basic (optional = false)
101         @Column (name = "provider_name", length = 30, nullable = false)
102         private String providerName;
103
104         /**
105          * Default constructor
106          */
107         public CellphoneProvider () {
108         }
109
110         /**
111          * Constructor with all required data
112          * <p>
113          * @param providerDialPrefix  Dial prefix
114          * @param providerName        Name
115          * @param providerCountry     An instance of a Country class
116          * @param providerMailPattern Pattern for email
117          */
118         public CellphoneProvider (final Long providerDialPrefix, final String providerName, final Country providerCountry, final String providerMailPattern) {
119                 // Invoke default constructor
120                 this();
121
122                 // Are all parameter set?
123                 if (null == providerDialPrefix) {
124                         // Throw NPE
125                         throw new NullPointerException("providerDialPrefix is null"); //NOI18N
126                 } else if (providerDialPrefix < 1) {
127                         // Throw IAE
128                         throw new IllegalArgumentException(MessageFormat.format("providerDialPrefix={0} is not valid.", providerDialPrefix)); //NOI18N
129                 } else if (null == providerName) {
130                         // Throw NPE
131                         throw new NullPointerException("providerName is null"); //NOI18N
132                 } else if (providerName.isEmpty()) {
133                         // Throw IAE
134                         throw new IllegalArgumentException("providerName is empty"); //NOI18N
135                 } else if (null == providerCountry) {
136                         // Throw NPE
137                         throw new NullPointerException("providerCountry is null"); //NOI18N
138                 } else if (null == providerCountry.getCountryId()) {
139                         // Throw NPE again
140                         throw new NullPointerException("providerCountry.countryId is null"); //NOI18N
141                 } else if (providerCountry.getCountryId() < 1) {
142                         // Throw IAE
143                         throw new IllegalArgumentException(MessageFormat.format("providerCountry.countryId={0} is not valid", providerCountry.getCountryId())); //NOI18N
144                 } else if (null == providerMailPattern) {
145                         // Throw NPE
146                         throw new NullPointerException("providerMailPattern is null"); //NOI18N
147                 } else if (providerMailPattern.isEmpty()) {
148                         // Throw IAE
149                         throw new IllegalArgumentException("providerMailPattern is empty"); //NOI18N
150                 }
151
152                 // Set all values
153                 this.providerDialPrefix = providerDialPrefix;
154                 this.providerName = providerName;
155                 this.providerCountry = providerCountry;
156                 this.providerMailPattern = providerMailPattern;
157         }
158
159         @Override
160         public int compareTo (final MobileProvider otherProvider) {
161                 // Is the parameter given?
162                 if (null == otherProvider) {
163                         // Throw NPE
164                         throw new NullPointerException("otherProvider is null"); //NOI18N
165                 } else if (otherProvider.equals(this)) {
166                         // Same object
167                         return 0;
168                 }
169
170                 // Init comparisons
171                 final int[] comparators = {
172                         // First provider
173                         this.getProviderCountry().compareTo(otherProvider.getProviderCountry()),
174                         // Next is name
175                         StringUtils.compare(this.getProviderName(), otherProvider.getProviderName()),
176                         // Last is dial prefix
177                         this.getProviderDialPrefix().compareTo(otherProvider.getProviderDialPrefix())
178                 };
179
180                 // Check all values
181                 final int comparison = Comparables.checkAll(comparators);
182
183                 // Return value
184                 return comparison;
185         }
186
187         @Override
188         public boolean equals (final Object object) {
189                 if (null == object) {
190                         return false;
191                 } else if (this.getClass() != object.getClass()) {
192                         return false;
193                 }
194
195                 final MobileProvider mobileProvider = (MobileProvider) object;
196
197                 if (!Objects.equals(this.getProviderDialPrefix(), mobileProvider.getProviderDialPrefix())) {
198                         return false;
199                 } else if (!Objects.equals(this.getProviderName(), mobileProvider.getProviderName())) {
200                         return false;
201                 } else if (!Objects.equals(this.getProviderCountry(), mobileProvider.getProviderCountry())) {
202                         return false;
203                 }
204
205                 return true;
206         }
207
208         @Override
209         public Country getProviderCountry () {
210                 return this.providerCountry;
211         }
212
213         @Override
214         public void setProviderCountry (final Country providerCountry) {
215                 this.providerCountry = providerCountry;
216         }
217
218         @Override
219         public Long getProviderDialPrefix () {
220                 return this.providerDialPrefix;
221         }
222
223         @Override
224         public void setProviderDialPrefix (final Long providerDialPrefix) {
225                 this.providerDialPrefix = providerDialPrefix;
226         }
227
228         @Override
229         @SuppressWarnings ("ReturnOfDateField")
230         public Date getProviderEntryCreated () {
231                 return this.providerEntryCreated;
232         }
233
234         @Override
235         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
236         public void setProviderEntryCreated (final Date providerEntryCreated) {
237                 this.providerEntryCreated = providerEntryCreated;
238         }
239
240         @Override
241         public Long getProviderId () {
242                 return this.providerId;
243         }
244
245         @Override
246         public void setProviderId (final Long providerId) {
247                 this.providerId = providerId;
248         }
249
250         @Override
251         public String getProviderMailPattern () {
252                 return this.providerMailPattern;
253         }
254
255         @Override
256         public void setProviderMailPattern (final String providerMailPattern) {
257                 this.providerMailPattern = providerMailPattern;
258         }
259
260         @Override
261         public String getProviderName () {
262                 return this.providerName;
263         }
264
265         @Override
266         public void setProviderName (final String providerName) {
267                 this.providerName = providerName;
268         }
269
270         @Override
271         public int hashCode () {
272                 int hash = 7;
273
274                 hash = 19 * hash + Objects.hashCode(this.getProviderDialPrefix());
275                 hash = 19 * hash + Objects.hashCode(this.getProviderName());
276                 hash = 19 * hash + Objects.hashCode(this.getProviderCountry());
277
278                 return hash;
279         }
280
281 }