2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.model.utils;
19 import java.io.Serializable;
20 import java.util.Objects;
21 import org.mxchange.jcontacts.model.contact.Contact;
22 import org.mxchange.jcountry.model.data.Country;
23 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
24 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
25 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
26 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
27 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
28 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
29 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
32 * Utilities for contacts
34 * @author Roland Häder<roland@mxchange.org>
36 public class ContactUtils implements Serializable {
41 private static final long serialVersionUID = 26_785_734_719_670L;
44 * Compares both contact instances. This method returns -1 if second
47 * @param contact1 Contact instance 1
48 * @param contact2 Contact instance 2
50 * @return Comparison value
52 public static int compare (final Contact contact1, final Contact contact2) {
53 // Check equality, then at least first must be given
54 if (Objects.equals(contact1, contact2)) {
57 } else if (null == contact1) {
60 } else if (null == contact2) {
65 // Invoke compareTo() method
66 return contact1.compareTo(contact2);
70 * Copies all attributes from other contact object to this
72 * @param sourceContact Source instance
73 * @param targetContact Target instance
75 public static void copyContactData (final Contact sourceContact, final Contact targetContact) {
76 // Contact should be valid
77 if (null == sourceContact) {
79 throw new NullPointerException("sourceContact is null"); //NOI18N
80 } else if (null == targetContact) {
82 throw new NullPointerException("targetContact is null"); //NOI18N
83 } else if (Objects.equals(sourceContact, targetContact)) {
85 throw new IllegalArgumentException("sourceContact and targetContact are the same."); //NOI18N
90 targetContact.setContactPersonalTitle(sourceContact.getContactPersonalTitle());
91 targetContact.setContactTitle(sourceContact.getContactTitle());
92 targetContact.setContactFirstName(sourceContact.getContactFirstName());
93 targetContact.setContactFamilyName(sourceContact.getContactFamilyName());
94 targetContact.setContactStreet(sourceContact.getContactStreet());
95 targetContact.setContactHouseNumber(sourceContact.getContactHouseNumber());
96 targetContact.setContactHouseNumberExtension(sourceContact.getContactHouseNumberExtension());
97 targetContact.setContactZipCode(sourceContact.getContactZipCode());
98 targetContact.setContactCity(sourceContact.getContactCity());
99 targetContact.setContactCountry(sourceContact.getContactCountry());
101 // - phone, fax, email
102 targetContact.setContactLandLineNumber(sourceContact.getContactLandLineNumber());
103 targetContact.setContactFaxNumber(sourceContact.getContactFaxNumber());
104 targetContact.setContactMobileNumber(sourceContact.getContactMobileNumber());
107 targetContact.setContactBirthday(sourceContact.getContactBirthday());
108 targetContact.setContactComment(sourceContact.getContactComment());
109 targetContact.setContactEmailAddress(sourceContact.getContactEmailAddress());
110 targetContact.setContactOwnContact(sourceContact.isOwnContact());
114 * Checks whether both contacts are same, but ignoring id number. If you
115 * want to include id number in comparison, better use Objects.equals() as
116 * the equal() method is implemented and checks all fields.
118 * @param contact Contact one
119 * @param other Contact two
121 * @return Whether both are the same
123 public static boolean isSameContact (final Contact contact, final Contact other) {
124 // Both should not be null
125 if (null == contact) {
126 // First contact is null
127 throw new NullPointerException("contact is null"); //NOI18N
128 } else if (null == other) {
129 // Second contact is null
130 throw new NullPointerException("other is null"); //NOI18N
133 // Check all data fields, except id number
134 return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
135 (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
136 (Objects.equals(contact.getContactComment(), other.getContactComment())) &&
137 (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
138 (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
139 (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
140 (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
141 (Objects.equals(contact.getContactPersonalTitle(), other.getContactPersonalTitle())) &&
142 (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
143 (Objects.equals(contact.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) &&
144 (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
145 (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
146 (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())) &&
147 (Objects.equals(contact.isOwnContact(), other.isOwnContact())));
151 * Updates fax number data in contact instance. This method also removes the
152 * land-line instance if no country is selected. A bean (mostly EJB) should
153 * then make sure that the land-line entry is being unlinked from contact
154 * instance or being removed, if no longer used.
156 * @param contact Contact instance being updated
157 * @param faxCountry Updated fax number or null
158 * @param faxAreaCode Updated fax area code or null
159 * @param faxNumber Updated fax number
161 * @return Whether the fax number has been unlinked in contact object
163 public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
164 // At least contact must be valid
165 if (null == contact) {
167 throw new NullPointerException("contact is null"); //NOI18N
170 // Default is not unlinked
171 boolean isUnlinked = false;
173 // Is there a fax instance?
174 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
175 // Found existing fax number, remove it?
176 if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
177 // Remove existing instance
178 contact.setContactFaxNumber(null);
180 // Mark it as being removed
184 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
185 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
186 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
188 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
189 // Set new land-line number
190 final DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
193 contact.setContactFaxNumber(fax);
201 * Updates land-line data in contact instance. This method also removes the
202 * land-line instance if no country is selected. A bean (mostly EJB) should
203 * then make sure that the land-line entry is being unlinked from contact
204 * instance or being removed, if no longer used.
206 * @param contact Contact instance being updated
207 * @param phoneCountry New phone country or old or null
208 * @param phoneAreaCode New phone's area code (or old)
209 * @param phoneNumber New phone number (or old)
211 * @return Whether the land-line number has been unlinked in contact object
213 public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
214 // At least contact must be valid
215 if (null == contact) {
217 throw new NullPointerException("contact is null"); //NOI18N
220 // Default is not unlinked
221 boolean isUnlinked = false;
223 // Is there a land-line instance?
224 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
225 // Found existing land-line number, remove it?
226 if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
227 // Remove existing instance
228 contact.setContactLandLineNumber(null);
230 // Mark it as being removed
234 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
235 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
236 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
238 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
239 // Set new land-line number
240 final DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
243 contact.setContactLandLineNumber(landLine);
251 * Updates mobile data in contact instance. This method also removes the
252 * mobile instance if no provider is selected. A bean (mostly EJB) should
253 * then make sure that the mobile entry is being unlinked from contact
254 * instance or being removed, if no longer used.
256 * @param contact Contact instance to update
257 * @param mobileProvider New mobile provider (or old)
258 * @param mobileNumber New mobile number (or old)
260 * @return Whether the mobile has been unlinked in contact object
262 public static boolean updateMobileNumber (final Contact contact, final MobileProvider mobileProvider, final Long mobileNumber) {
263 // At least contact must be valid
264 if (null == contact) {
266 throw new NullPointerException("contact is null"); //NOI18N
267 } else if ((mobileProvider instanceof MobileProvider) && (null == mobileNumber)) {
268 // Mobile provider given, but no number
269 throw new NullPointerException("mobileNumber is null"); //NOI18N
272 // Default is not unlinked
273 boolean isUnlinked = false;
275 // Is there a mobile number?
276 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
278 if ((null == mobileProvider) || (null == mobileNumber) || (mobileNumber == 0)) {
280 contact.setContactMobileNumber(null);
285 // Yes, then update as well
286 contact.getContactMobileNumber().setMobileProvider(mobileProvider);
287 contact.getContactMobileNumber().setMobileNumber(mobileNumber);
289 } else if ((mobileProvider instanceof MobileProvider) && (mobileNumber > 0)) {
290 // Create new instance
291 final DialableMobileNumber mobile = new MobileNumber(mobileProvider, mobileNumber);
294 contact.setContactMobileNumber(mobile);
302 * Private constructor for utilities
304 private ContactUtils () {