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