2 * Copyright (C) 2016, 2017 Roland Häder
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;
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;
31 * Utilities for contacts
33 * @author Roland Häder<roland@mxchange.org>
35 public class ContactUtils implements Serializable {
40 private static final long serialVersionUID = 26_785_734_719_670L;
43 * Copies all attributes from other contact object to this
45 * @param sourceContact Source instance
46 * @param targetContact Target instance
48 public static void copyAll (final Contact sourceContact, final Contact targetContact) {
49 // Contact should be valid
50 if (null == sourceContact) {
52 throw new NullPointerException("sourceContact is null"); //NOI18N
53 } else if (null == targetContact) {
55 throw new NullPointerException("targetContact is null"); //NOI18N
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());
71 // - phone, fax, email
72 targetContact.setContactLandLineNumber(sourceContact.getContactLandLineNumber());
73 targetContact.setContactFaxNumber(sourceContact.getContactFaxNumber());
74 targetContact.setContactMobileNumber(sourceContact.getContactMobileNumber());
77 targetContact.setContactBirthday(sourceContact.getContactBirthday());
78 targetContact.setContactComment(sourceContact.getContactComment());
79 targetContact.setContactCreated(sourceContact.getContactCreated());
80 targetContact.setContactUpdated(sourceContact.getContactUpdated());
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.
88 * @param contact Contact one
89 * @param other Contact two
91 * @return Whether both are the same
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
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())));
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.
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
128 * @return Whether the fax number has been unlinked in contact object
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) {
134 throw new NullPointerException("contact is null"); //NOI18N
137 // Default is not unlinked
138 boolean isUnlinked = false;
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);
147 // Mark it as being removed
151 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
152 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
153 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
155 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
156 // Set new land-line number
157 DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
160 contact.setContactFaxNumber(fax);
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.
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)
178 * @return Whether the land-line number has been unlinked in contact object
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) {
184 throw new NullPointerException("contact is null"); //NOI18N
187 // Default is not unlinked
188 boolean isUnlinked = false;
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);
197 // Mark it as being removed
201 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
202 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
203 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
205 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
206 // Set new land-line number
207 DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
210 contact.setContactLandLineNumber(landLine);
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.
223 * @param contact Contact instance to update
224 * @param mobileProvider New mobile provider (or old)
225 * @param mobileNumber New mobile number (or old)
227 * @return Whether the mobile has been unlinked in contact object
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) {
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
239 // Default is not unlinked
240 boolean isUnlinked = false;
242 // Is there a mobile number?
243 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
245 if ((null == mobileProvider) || (null == mobileNumber) || (mobileNumber == 0)) {
247 contact.setContactMobileNumber(null);
252 // Yes, then update as well
253 contact.getContactMobileNumber().setMobileProvider(mobileProvider);
254 contact.getContactMobileNumber().setPhoneNumber(mobileNumber);
256 } else if ((mobileProvider instanceof MobileProvider) && (mobileNumber > 0)) {
257 // Create new instance
258 DialableMobileNumber mobile = new MobileNumber(mobileProvider, mobileNumber);
261 contact.setContactMobileNumber(mobile);
269 * Private constructor for utilities
271 private ContactUtils () {