]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/model/utils/ContactUtils.java
Updated copyright year
[jcontacts-core.git] / src / org / mxchange / jcontacts / model / utils / ContactUtils.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.jcontacts.model.utils;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21 import org.mxchange.jcontacts.model.contact.Contact;
22 import org.mxchange.jcountry.model.data.Country;
23 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
24 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
25 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
26 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
27 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
28 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
29 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
30
31 /**
32  * Utilities for contacts
33  * <p>
34  * @author Roland Häder<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          * Compares both contact instances. This method returns -1 if second
45          * instance is null.
46          * <p>
47          * @param contact1 Contact instance 1
48          * @param contact2 Contact instance 2
49          * <p>
50          * @return Comparison value
51          */
52         public static int compare (final Contact contact1, final Contact contact2) {
53                 // Check equality, then at least first must be given
54                 if (Objects.equals(contact1, contact2)) {
55                         // Both are same
56                         return 0;
57                 } else if (null == contact1) {
58                         // First is null
59                         return -1;
60                 } else if (null == contact2) {
61                         // Second is null
62                         return 1;
63                 }
64
65                 // Invoke compareTo() method
66                 return contact1.compareTo(contact2);
67         }
68
69         /**
70          * Copies all attributes from other contact object to this
71          * <p>
72          * @param sourceContact Source instance
73          * @param targetContact Target instance
74          */
75         public static void copyContactData (final Contact sourceContact, final Contact targetContact) {
76                 // Contact should be valid
77                 if (null == sourceContact) {
78                         // Throw NPE
79                         throw new NullPointerException("sourceContact is null"); //NOI18N
80                 } else if (null == targetContact) {
81                         // Throw NPE
82                         throw new NullPointerException("targetContact is null"); //NOI18N
83                 } else if (Objects.equals(sourceContact, targetContact)) {
84                         // Throw IAE
85                         throw new IllegalArgumentException("sourceContact and targetContact are the same."); //NOI18N
86                 }
87
88                 // Copy all:
89                 // - base data
90                 targetContact.setContactPersonalTitle(sourceContact.getContactPersonalTitle());
91                 targetContact.setContactTitle(sourceContact.getContactTitle());
92                 targetContact.setContactFirstName(sourceContact.getContactFirstName());
93                 targetContact.setContactFamilyName(sourceContact.getContactFamilyName());
94                 targetContact.setContactStreet(sourceContact.getContactStreet());
95                 targetContact.setContactHouseNumber(sourceContact.getContactHouseNumber());
96                 targetContact.setContactHouseNumberExtension(sourceContact.getContactHouseNumberExtension());
97                 targetContact.setContactZipCode(sourceContact.getContactZipCode());
98                 targetContact.setContactCity(sourceContact.getContactCity());
99                 targetContact.setContactCountry(sourceContact.getContactCountry());
100
101                 // - phone, fax, email
102                 targetContact.setContactLandLineNumber(sourceContact.getContactLandLineNumber());
103                 targetContact.setContactFaxNumber(sourceContact.getContactFaxNumber());
104                 targetContact.setContactMobileNumber(sourceContact.getContactMobileNumber());
105
106                 // - other data
107                 targetContact.setContactBirthday(sourceContact.getContactBirthday());
108                 targetContact.setContactComment(sourceContact.getContactComment());
109                 targetContact.setContactEmailAddress(sourceContact.getContactEmailAddress());
110                 targetContact.setContactOwnContact(sourceContact.isOwnContact());
111         }
112
113         /**
114          * Checks whether both contacts are same, but ignoring id number. If you
115          * want to include id number in comparison, better use Objects.equals() as
116          * the equal() method is implemented and checks all fields.
117          * <p>
118          * @param contact Contact one
119          * @param other   Contact two
120          * <p>
121          * @return Whether both are the same
122          */
123         public static boolean isSameContact (final Contact contact, final Contact other) {
124                 // Both should not be null
125                 if (null == contact) {
126                         // First contact is null
127                         throw new NullPointerException("contact is null"); //NOI18N
128                 } else if (null == other) {
129                         // Second contact is null
130                         throw new NullPointerException("other is null"); //NOI18N
131                 }
132
133                 // Check all data fields, except id number
134                 return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
135                                 (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
136                                 (Objects.equals(contact.getContactComment(), other.getContactComment())) &&
137                                 (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
138                                 (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
139                                 (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
140                                 (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
141                                 (Objects.equals(contact.getContactPersonalTitle(), other.getContactPersonalTitle())) &&
142                                 (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
143                                 (Objects.equals(contact.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) &&
144                                 (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
145                                 (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
146                                 (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())) &&
147                                 (Objects.equals(contact.isOwnContact(), other.isOwnContact())));
148         }
149
150         /**
151          * Updates fax number data in contact instance. This method also removes the
152          * land-line instance if no country is selected. A bean (mostly EJB) should
153          * then make sure that the land-line entry is being unlinked from contact
154          * instance or being removed, if no longer used.
155          * <p>
156          * @param contact     Contact instance being updated
157          * @param faxCountry  Updated fax number or null
158          * @param faxAreaCode Updated fax area code or null
159          * @param faxNumber   Updated fax number
160          * <p>
161          * @return Whether the fax number has been unlinked in contact object
162          */
163         public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
164                 // At least contact must be valid
165                 if (null == contact) {
166                         // Throw NPE
167                         throw new NullPointerException("contact is null"); //NOI18N
168                 }
169
170                 // Default is not unlinked
171                 boolean isUnlinked = false;
172
173                 // Is there a fax instance?
174                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
175                         // Found existing fax number, remove it?
176                         if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
177                                 // Remove existing instance
178                                 contact.setContactFaxNumber(null);
179
180                                 // Mark it as being removed
181                                 isUnlinked = true;
182                         } else {
183                                 // Set all data
184                                 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
185                                 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
186                                 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
187                         }
188                 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
189                         // Set new land-line number
190                         final DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
191
192                         // Set it in contact
193                         contact.setContactFaxNumber(fax);
194                 }
195
196                 // Return status
197                 return isUnlinked;
198         }
199
200         /**
201          * Updates land-line data in contact instance. This method also removes the
202          * land-line instance if no country is selected. A bean (mostly EJB) should
203          * then make sure that the land-line entry is being unlinked from contact
204          * instance or being removed, if no longer used.
205          * <p>
206          * @param contact       Contact instance being updated
207          * @param phoneCountry  New phone country or old or null
208          * @param phoneAreaCode New phone's area code (or old)
209          * @param phoneNumber   New phone number (or old)
210          * <p>
211          * @return Whether the land-line number has been unlinked in contact object
212          */
213         public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
214                 // At least contact must be valid
215                 if (null == contact) {
216                         // Throw NPE
217                         throw new NullPointerException("contact is null"); //NOI18N
218                 }
219
220                 // Default is not unlinked
221                 boolean isUnlinked = false;
222
223                 // Is there a land-line instance?
224                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
225                         // Found existing land-line number, remove it?
226                         if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
227                                 // Remove existing instance
228                                 contact.setContactLandLineNumber(null);
229
230                                 // Mark it as being removed
231                                 isUnlinked = true;
232                         } else {
233                                 // Set all data
234                                 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
235                                 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
236                                 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
237                         }
238                 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
239                         // Set new land-line number
240                         final DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
241
242                         // Set it in contact
243                         contact.setContactLandLineNumber(landLine);
244                 }
245
246                 // Return status
247                 return isUnlinked;
248         }
249
250         /**
251          * Updates mobile data in contact instance. This method also removes the
252          * mobile instance if no provider is selected. A bean (mostly EJB) should
253          * then make sure that the mobile entry is being unlinked from contact
254          * instance or being removed, if no longer used.
255          * <p>
256          * @param contact        Contact instance to update
257          * @param mobileProvider New mobile provider (or old)
258          * @param mobileNumber   New mobile number (or old)
259          * <p>
260          * @return Whether the mobile has been unlinked in contact object
261          */
262         public static boolean updateMobileNumber (final Contact contact, final MobileProvider mobileProvider, final Long mobileNumber) {
263                 // At least contact must be valid
264                 if (null == contact) {
265                         // Throw NPE
266                         throw new NullPointerException("contact is null"); //NOI18N
267                 } else if ((mobileProvider instanceof MobileProvider) && (null == mobileNumber)) {
268                         // Mobile provider given, but no number
269                         throw new NullPointerException("mobileNumber is null"); //NOI18N
270                 }
271
272                 // Default is not unlinked
273                 boolean isUnlinked = false;
274
275                 // Is there a mobile number?
276                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
277                         // Is provider null?
278                         if ((null == mobileProvider) || (null == mobileNumber) || (mobileNumber == 0)) {
279                                 // Remove instance
280                                 contact.setContactMobileNumber(null);
281
282                                 // Mark as unlinked
283                                 isUnlinked = true;
284                         } else {
285                                 // Yes, then update as well
286                                 contact.getContactMobileNumber().setMobileProvider(mobileProvider);
287                                 contact.getContactMobileNumber().setMobileNumber(mobileNumber);
288                         }
289                 } else if ((mobileProvider instanceof MobileProvider) && (mobileNumber > 0)) {
290                         // Create new instance
291                         final DialableMobileNumber mobile = new MobileNumber(mobileProvider, mobileNumber);
292
293                         // Set it in contact
294                         contact.setContactMobileNumber(mobile);
295                 }
296
297                 // Return status
298                 return isUnlinked;
299         }
300
301         /**
302          * Private constructor for utilities
303          */
304         private ContactUtils () {
305         }
306
307 }