2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcontacts.contact.utils;
19 import java.io.Serializable;
20 import java.util.Objects;
21 import org.mxchange.jcontacts.contact.Contact;
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;
32 * Utilities for contacts
34 * @author Roland Haeder<roland@mxchange.org>
36 public class ContactUtils implements Serializable {
41 private static final long serialVersionUID = 26_785_734_719_670L;
44 * Checks whether both contacts are same, but ignoring id number. If you
45 * want to include id number in comparison, better use Objects.equals() as
46 * the equal() method is implemented and checks all fields.
48 * @param contact Contact one
49 * @param other Contact two
51 * @return Whether both are the same
53 public static boolean isSameContact (final Contact contact, final Contact other) {
54 // Both should not be null
55 if (null == contact) {
56 // First contact is null
57 throw new NullPointerException("contact is null"); //NOI18N
58 } else if (null == other) {
59 // Secondcontact is null
60 throw new NullPointerException("other is null"); //NOI18N
63 // Check all data fields, except id number
64 return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
65 (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
66 (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
67 (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
68 (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
69 (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
70 (Objects.equals(contact.getContactGender(), other.getContactGender())) &&
71 (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
72 (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
73 (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
74 (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())));
78 * Updates cellphone data in contact instance. This method also removes the
79 * cellphone instance if no provider is selected. A bean (mostly EJB) should
80 * then make sure that the cellphone entry is being unlinked from contact
81 * instance or being removed, if no longer used.
83 * @param contact Contact instance to update
84 * @param cellphoneProvider New cellphone provider (or old)
85 * @param cellphoneNumber New cellphone number (or old)
87 * @return Whether the cellphone has been unlinked in contact object
89 public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {
90 // At least contact must be valid
91 if (null == contact) {
93 throw new NullPointerException("contact is null"); //NOI18N
96 // Default is not unlinked
97 boolean isUnlinked = false;
99 // Is there a cellphone number?
100 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
102 if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {
104 contact.setContactCellphoneNumber(null);
109 // Yes, then update as well
110 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);
111 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);
113 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {
114 // Create new instance
115 DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);
118 contact.setContactCellphoneNumber(cellphone);
126 * Updates land-line data in contact instance. This method also removes the
127 * land-line instance if no country is selected. A bean (mostly EJB) should
128 * then make sure that the land-line entry is being unlinked from contact
129 * instance or being removed, if no longer used.
131 * @param contact Contact instance being updated
132 * @param faxCountry Updated fax number or null
133 * @param faxAreaCode Updated fax area code or null
134 * @param faxNumber Updated fax number
136 * @return Whether the fax number has been unlinked in contact object
138 public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
139 // At least contact must be valid
140 if (null == contact) {
142 throw new NullPointerException("contact is null"); //NOI18N
145 // Default is not unlinked
146 boolean isUnlinked = false;
148 // Is there a fax instance?
149 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
150 // Found existing fax number, remove it?
151 if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
152 // Remove existing instance
153 contact.setContactFaxNumber(null);
155 // Mark it as being removed
159 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
160 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
161 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
163 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
164 // Set new land-line number
165 DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
168 contact.setContactFaxNumber(fax);
176 * Updates land-line data in contact instance. This method also removes the
177 * land-line instance if no country is selected. A bean (mostly EJB) should
178 * then make sure that the land-line entry is being unlinked from contact
179 * instance or being removed, if no longer used.
181 * @param contact Contact instance being updated
182 * @param phoneCountry New phone country or old or null
183 * @param phoneAreaCode New phone's area code (or old)
184 * @param phoneNumber New phone number (or old)
186 * @return Whether the land-line number has been unlinked in contact object
188 public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
189 // At least contact must be valid
190 if (null == contact) {
192 throw new NullPointerException("contact is null"); //NOI18N
195 // Default is not unlinked
196 boolean isUnlinked = false;
198 // Is there a land-line instance?
199 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
200 // Found existing land-line number, remove it?
201 if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
202 // Remove existing instance
203 contact.setContactLandLineNumber(null);
205 // Mark it as being removed
209 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
210 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
211 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
213 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
214 // Set new land-line number
215 DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
218 contact.setContactLandLineNumber(landLine);
226 * Private constructor for utilities
228 private ContactUtils () {