]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
Cleanup:
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / utils / ContactUtils.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jcontacts.contact.utils;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21 import org.mxchange.jcontacts.contact.Contact;
22 import org.mxchange.jcountry.data.Country;
23 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
24 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
25 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
26 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
27 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
28 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
29 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
30
31 /**
32  * Utilities for contacts
33  * <p>
34  * @author Roland Haeder<roland@mxchange.org>
35  */
36 public class ContactUtils implements Serializable {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 26_785_734_719_670L;
42
43         /**
44          * Checks whether both contacts are same, but ignoring id number. If you
45          * want to include id number in comparison, better use Objects.equals() as
46          * the equal() method is implemented and checks all fields.
47          * <p>
48          * @param contact Contact one
49          * @param other Contact two
50          * <p>
51          * @return Whether both are the same
52          */
53         public static boolean isSameContact (final Contact contact, final Contact other) {
54                 // Both should not be null
55                 if (null == contact) {
56                         // First contact is null
57                         throw new NullPointerException("contact is null"); //NOI18N
58                 } else if (null == other) {
59                         // Secondcontact is null
60                         throw new NullPointerException("other is null"); //NOI18N
61                 }
62
63                 // Check all data fields, except id number
64                 return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
65                                 (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
66                                 (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
67                                 (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
68                                 (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
69                                 (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
70                                 (Objects.equals(contact.getContactGender(), other.getContactGender())) &&
71                                 (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
72                                 (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
73                                 (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
74                                 (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())));
75         }
76
77         /**
78          * Updates cellphone data in contact instance. This method also removes the
79          * cellphone instance if no provider is selected. A bean (mostly EJB) should
80          * then make sure that the cellphone entry is being unlinked from contact
81          * instance or being removed, if no longer used.
82          * <p>
83          * @param contact Contact instance to update
84          * @param cellphoneProvider New cellphone provider (or old)
85          * @param cellphoneNumber New cellphone number (or old)
86          * <p>
87          * @return Whether the cellphone has been unlinked in contact object
88          */
89         public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {
90                 // At least contact must be valid
91                 if (null == contact) {
92                         // Throw NPE
93                         throw new NullPointerException("contact is null"); //NOI18N
94                 }
95
96                 // Default is not unlinked
97                 boolean isUnlinked = false;
98
99                 // Is there a cellphone number?
100                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
101                         // Is provider null?
102                         if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {
103                                 // Remove instance
104                                 contact.setContactCellphoneNumber(null);
105
106                                 // Mark as unlinked
107                                 isUnlinked = true;
108                         } else {
109                                 // Yes, then update as well
110                                 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);
111                                 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);
112                         }
113                 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {
114                         // Create new instance
115                         DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);
116
117                         // Set it in contact
118                         contact.setContactCellphoneNumber(cellphone);
119                 }
120
121                 // Return status
122                 return isUnlinked;
123         }
124
125         /**
126          * Updates land-line data in contact instance. This method also removes the
127          * land-line instance if no country is selected. A bean (mostly EJB) should
128          * then make sure that the land-line entry is being unlinked from contact
129          * instance or being removed, if no longer used.
130          * <p>
131          * @param contact Contact instance being updated
132          * @param faxCountry Updated fax number or null
133          * @param faxAreaCode Updated fax area code or null
134          * @param faxNumber Updated fax number
135          * <p>
136          * @return Whether the fax number has been unlinked in contact object
137          */
138         public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
139                 // At least contact must be valid
140                 if (null == contact) {
141                         // Throw NPE
142                         throw new NullPointerException("contact is null"); //NOI18N
143                 }
144
145                 // Default is not unlinked
146                 boolean isUnlinked = false;
147
148                 // Is there a fax instance?
149                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
150                         // Found existing fax number, remove it?
151                         if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
152                                 // Remove existing instance
153                                 contact.setContactFaxNumber(null);
154
155                                 // Mark it as being removed
156                                 isUnlinked = true;
157                         } else {
158                                 // Set all data
159                                 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
160                                 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
161                                 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
162                         }
163                 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
164                         // Set new land-line number
165                         DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
166
167                         // Set it in contact
168                         contact.setContactFaxNumber(fax);
169                 }
170
171                 // Return status
172                 return isUnlinked;
173         }
174
175         /**
176          * Updates land-line data in contact instance. This method also removes the
177          * land-line instance if no country is selected. A bean (mostly EJB) should
178          * then make sure that the land-line entry is being unlinked from contact
179          * instance or being removed, if no longer used.
180          * <p>
181          * @param contact Contact instance being updated
182          * @param phoneCountry New phone country or old or null
183          * @param phoneAreaCode New phone's area code (or old)
184          * @param phoneNumber New phone number (or old)
185          * <p>
186          * @return Whether the land-line number has been unlinked in contact object
187          */
188         public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
189                 // At least contact must be valid
190                 if (null == contact) {
191                         // Throw NPE
192                         throw new NullPointerException("contact is null"); //NOI18N
193                 }
194
195                 // Default is not unlinked
196                 boolean isUnlinked = false;
197
198                 // Is there a land-line instance?
199                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
200                         // Found existing land-line number, remove it?
201                         if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
202                                 // Remove existing instance
203                                 contact.setContactLandLineNumber(null);
204
205                                 // Mark it as being removed
206                                 isUnlinked = true;
207                         } else {
208                                 // Set all data
209                                 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
210                                 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
211                                 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
212                         }
213                 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
214                         // Set new land-line number
215                         DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
216
217                         // Set it in contact
218                         contact.setContactLandLineNumber(landLine);
219                 }
220
221                 // Return status
222                 return isUnlinked;
223         }
224
225         /**
226          * Private constructor for utilities
227          */
228         private ContactUtils () {
229         }
230
231 }