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