]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
Added isSameContact() which checks if it the same "contact" as humans would expect...
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / utils / ContactUtils.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.utils;
18
19 import java.util.Objects;
20 import org.mxchange.jcontacts.contact.Contact;
21 import org.mxchange.jcore.BaseFrameworkSystem;
22 import org.mxchange.jcountry.data.Country;
23 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
24 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
25 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
26 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
27 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
28 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
29 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
30
31 /**
32  * Utilities for contacts
33  * <p>
34  * @author Roland Haeder<roland@mxchange.org>
35  */
36 public class ContactUtils extends BaseFrameworkSystem {
37
38         /**
39          * Checks whether both contacts are same, but ignoring id number. If you
40          * want to include id number in comparison, better use Objects.equals() as
41          * the equal() method is implemented and checks all fields.
42          * <p>
43          * @param contact Contact one
44          * @param other Contact two
45          * <p>
46          * @return Whether both are the same
47          */
48         public static boolean isSameContact (final Contact contact, final Contact other) {
49                 // Both should not be null
50                 if (null == contact) {
51                         // First contact is null
52                         throw new NullPointerException("contact is null"); //NOI18N
53                 } else if (null == other) {
54                         // Secondcontact is null
55                         throw new NullPointerException("other is null"); //NOI18N
56                 }
57
58                 // Check all data fields, except id number
59                 return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
60                                 (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
61                                 (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
62                                 (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
63                                 (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
64                                 (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
65                                 (Objects.equals(contact.getContactGender(), other.getContactGender())) &&
66                                 (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
67                                 (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
68                                 (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
69                                 (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())));
70         }
71
72         /**
73          * Updates cellphone data in contact instance. This method also removes the
74          * cellphone instance if no provider is selected. A bean (mostly EJB) should
75          * then make sure that the cellphone entry is being unlinked from contact
76          * instance or being removed, if no longer used.
77          * <p>
78          * @param contact Contact instance to update
79          * @param cellphoneProvider New cellphone provider (or old)
80          * @param cellphoneNumber New cellphone number (or old)
81          * <p>
82          * @return Whether the cellphone has been unlinked in contact object
83          */
84         public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {
85                 // At least contact must be valid
86                 if (null == contact) {
87                         // Throw NPE
88                         throw new NullPointerException("contact is null"); //NOI18N
89                 }
90
91                 // Default is not unlinked
92                 boolean isUnlinked = false;
93
94                 // Is there a cellphone number?
95                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
96                         // Is provider null?
97                         if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {
98                                 // Remove instance
99                                 contact.setContactCellphoneNumber(null);
100
101                                 // Mark as unlinked
102                                 isUnlinked = true;
103                         } else {
104                                 // Yes, then update as well
105                                 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);
106                                 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);
107                         }
108                 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {
109                         // Create new instance
110                         DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);
111
112                         // Set it in contact
113                         contact.setContactCellphoneNumber(cellphone);
114                 }
115
116                 // Return status
117                 return isUnlinked;
118         }
119
120         /**
121          * Updates land-line data in contact instance. This method also removes the
122          * land-line instance if no country is selected. A bean (mostly EJB) should
123          * then make sure that the land-line entry is being unlinked from contact
124          * instance or being removed, if no longer used.
125          * <p>
126          * @param contact Contact instance being updated
127          * @param faxCountry Updated fax number or null
128          * @param faxAreaCode Updated fax area code or null
129          * @param faxNumber Updated fax number
130          * <p>
131          * @return Whether the fax number has been unlinked in contact object
132          */
133         public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
134                 // At least contact must be valid
135                 if (null == contact) {
136                         // Throw NPE
137                         throw new NullPointerException("contact is null"); //NOI18N
138                 }
139
140                 // Default is not unlinked
141                 boolean isUnlinked = false;
142
143                 // Is there a fax instance?
144                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
145                         // Found existing fax number, remove it?
146                         if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
147                                 // Remove existing instance
148                                 contact.setContactFaxNumber(null);
149
150                                 // Mark it as being removed
151                                 isUnlinked = true;
152                         } else {
153                                 // Set all data
154                                 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
155                                 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
156                                 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
157                         }
158                 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
159                         // Set new land-line number
160                         DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
161
162                         // Set it in contact
163                         contact.setContactFaxNumber(fax);
164                 }
165
166                 // Return status
167                 return isUnlinked;
168         }
169
170         /**
171          * Updates land-line data in contact instance. This method also removes the
172          * land-line instance if no country is selected. A bean (mostly EJB) should
173          * then make sure that the land-line entry is being unlinked from contact
174          * instance or being removed, if no longer used.
175          * <p>
176          * @param contact Contact instance being updated
177          * @param phoneCountry New phone country or old or null
178          * @param phoneAreaCode New phone's area code (or old)
179          * @param phoneNumber New phone number (or old)
180          * <p>
181          * @return Whether the land-line number has been unlinked in contact object
182          */
183         public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
184                 // At least contact must be valid
185                 if (null == contact) {
186                         // Throw NPE
187                         throw new NullPointerException("contact is null"); //NOI18N
188                 }
189
190                 // Default is not unlinked
191                 boolean isUnlinked = false;
192
193                 // Is there a land-line instance?
194                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
195                         // Found existing land-line number, remove it?
196                         if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
197                                 // Remove existing instance
198                                 contact.setContactLandLineNumber(null);
199
200                                 // Mark it as being removed
201                                 isUnlinked = true;
202                         } else {
203                                 // Set all data
204                                 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
205                                 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
206                                 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
207                         }
208                 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
209                         // Set new land-line number
210                         DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
211
212                         // Set it in contact
213                         contact.setContactLandLineNumber(landLine);
214                 }
215
216                 // Return status
217                 return isUnlinked;
218         }
219
220         /**
221          * Private constructor for utilities
222          */
223         private ContactUtils () {
224         }
225
226 }