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.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;
32 * Utilities for contacts
34 * @author Roland Haeder<roland@mxchange.org>
36 public class ContactUtils extends BaseFrameworkSystem {
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.
43 * @param contact Contact one
44 * @param other Contact two
46 * @return Whether both are the same
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
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())));
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.
78 * @param contact Contact instance to update
79 * @param cellphoneProvider New cellphone provider (or old)
80 * @param cellphoneNumber New cellphone number (or old)
82 * @return Whether the cellphone has been unlinked in contact object
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) {
88 throw new NullPointerException("contact is null"); //NOI18N
91 // Default is not unlinked
92 boolean isUnlinked = false;
94 // Is there a cellphone number?
95 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
97 if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {
99 contact.setContactCellphoneNumber(null);
104 // Yes, then update as well
105 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);
106 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);
108 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {
109 // Create new instance
110 DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);
113 contact.setContactCellphoneNumber(cellphone);
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.
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
131 * @return Whether the fax number has been unlinked in contact object
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) {
137 throw new NullPointerException("contact is null"); //NOI18N
140 // Default is not unlinked
141 boolean isUnlinked = false;
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);
150 // Mark it as being removed
154 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
155 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
156 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
158 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
159 // Set new land-line number
160 DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
163 contact.setContactFaxNumber(fax);
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.
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)
181 * @return Whether the land-line number has been unlinked in contact object
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) {
187 throw new NullPointerException("contact is null"); //NOI18N
190 // Default is not unlinked
191 boolean isUnlinked = false;
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);
200 // Mark it as being removed
204 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
205 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
206 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
208 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
209 // Set new land-line number
210 DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
213 contact.setContactLandLineNumber(landLine);
221 * Private constructor for utilities
223 private ContactUtils () {