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