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