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