+++ /dev/null
-/*
- * Copyright (C) 2016 - 2022 Free Software Foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jcontacts.model.contact;
-
-import java.io.Serializable;
-import java.util.Objects;
-import org.mxchange.jcountry.model.data.Country;
-import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
-import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
-import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
-import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
-import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
-import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
-import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
-
-/**
- * Utilities for contacts
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public class ContactUtils implements Serializable {
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 26_785_734_719_670L;
-
- /**
- * Compares both contact instances. This method returns -1 if second
- * instance is null.
- * <p>
- * @param contact1 Contact instance 1
- * @param contact2 Contact instance 2
- * <p>
- * @return Comparison value
- */
- public static int compare (final Contact contact1, final Contact contact2) {
- // Check equality, then at least first must be given
- if (Objects.equals(contact1, contact2)) {
- // Both are same
- return 0;
- } else if (null == contact1) {
- // First is null
- return -1;
- } else if (null == contact2) {
- // Second is null
- return 1;
- }
-
- // Invoke compareTo() method
- return contact1.compareTo(contact2);
- }
-
- /**
- * Copies all attributes from other contact object to this
- * <p>
- * @param sourceContact Source instance
- * @param targetContact Target instance
- */
- public static void copyContactData (final Contact sourceContact, final Contact targetContact) {
- // Contact should be valid
- if (null == sourceContact) {
- // Throw NPE
- throw new NullPointerException("sourceContact is null"); //NOI18N
- } else if (null == targetContact) {
- // Throw NPE
- throw new NullPointerException("targetContact is null"); //NOI18N
- } else if (Objects.equals(sourceContact, targetContact)) {
- // Throw IAE
- throw new IllegalArgumentException("sourceContact and targetContact are the same."); //NOI18N
- }
-
- // Copy all:
- // - base data
- targetContact.setContactPersonalTitle(sourceContact.getContactPersonalTitle());
- targetContact.setContactTitle(sourceContact.getContactTitle());
- targetContact.setContactFirstName(sourceContact.getContactFirstName());
- targetContact.setContactFamilyName(sourceContact.getContactFamilyName());
- targetContact.setContactStreet(sourceContact.getContactStreet());
- targetContact.setContactHouseNumber(sourceContact.getContactHouseNumber());
- targetContact.setContactHouseNumberExtension(sourceContact.getContactHouseNumberExtension());
- targetContact.setContactZipCode(sourceContact.getContactZipCode());
- targetContact.setContactCity(sourceContact.getContactCity());
- targetContact.setContactCountry(sourceContact.getContactCountry());
-
- // - phone, fax, email
- targetContact.setContactLandLineNumber(sourceContact.getContactLandLineNumber());
- targetContact.setContactFaxNumber(sourceContact.getContactFaxNumber());
- targetContact.setContactMobileNumber(sourceContact.getContactMobileNumber());
-
- // - other data
- targetContact.setContactBirthday(sourceContact.getContactBirthday());
- targetContact.setContactComment(sourceContact.getContactComment());
- }
-
- /**
- * Checks whether both contacts are same, but ignoring id number. If you
- * want to include id number in comparison, better use Objects.equals() as
- * the equal() method is implemented and checks all fields.
- * <p>
- * @param contact Contact one
- * @param other Contact two
- * <p>
- * @return Whether both are the same
- */
- public static boolean isSameContact (final Contact contact, final Contact other) {
- // Both should not be null
- if (null == contact) {
- // First contact is null
- throw new NullPointerException("contact is null"); //NOI18N
- } else if (null == other) {
- // Secondcontact is null
- throw new NullPointerException("other is null"); //NOI18N
- }
-
- // Check all data fields, except id number
- return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
- (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
- (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
- (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
- (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
- (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
- (Objects.equals(contact.getContactPersonalTitle(), other.getContactPersonalTitle())) &&
- (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
- (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
- (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
- (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())));
- }
-
- /**
- * Updates land-line data in contact instance. This method also removes the
- * land-line instance if no country is selected. A bean (mostly EJB) should
- * then make sure that the land-line entry is being unlinked from contact
- * instance or being removed, if no longer used.
- * <p>
- * @param contact Contact instance being updated
- * @param faxCountry Updated fax number or null
- * @param faxAreaCode Updated fax area code or null
- * @param faxNumber Updated fax number
- * <p>
- * @return Whether the fax number has been unlinked in contact object
- */
- public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
- // At least contact must be valid
- if (null == contact) {
- // Throw NPE
- throw new NullPointerException("contact is null"); //NOI18N
- }
-
- // Default is not unlinked
- boolean isUnlinked = false;
-
- // Is there a fax instance?
- if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
- // Found existing fax number, remove it?
- if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
- // Remove existing instance
- contact.setContactFaxNumber(null);
-
- // Mark it as being removed
- isUnlinked = true;
- } else {
- // Set all data
- contact.getContactFaxNumber().setPhoneCountry(faxCountry);
- contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
- contact.getContactFaxNumber().setPhoneNumber(faxNumber);
- }
- } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
- // Set new land-line number
- final DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
-
- // Set it in contact
- contact.setContactFaxNumber(fax);
- }
-
- // Return status
- return isUnlinked;
- }
-
- /**
- * Updates land-line data in contact instance. This method also removes the
- * land-line instance if no country is selected. A bean (mostly EJB) should
- * then make sure that the land-line entry is being unlinked from contact
- * instance or being removed, if no longer used.
- * <p>
- * @param contact Contact instance being updated
- * @param phoneCountry New phone country or old or null
- * @param phoneAreaCode New phone's area code (or old)
- * @param phoneNumber New phone number (or old)
- * <p>
- * @return Whether the land-line number has been unlinked in contact object
- */
- public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
- // At least contact must be valid
- if (null == contact) {
- // Throw NPE
- throw new NullPointerException("contact is null"); //NOI18N
- }
-
- // Default is not unlinked
- boolean isUnlinked = false;
-
- // Is there a land-line instance?
- if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
- // Found existing land-line number, remove it?
- if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
- // Remove existing instance
- contact.setContactLandLineNumber(null);
-
- // Mark it as being removed
- isUnlinked = true;
- } else {
- // Set all data
- contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
- contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
- contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
- }
- } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
- // Set new land-line number
- final DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
-
- // Set it in contact
- contact.setContactLandLineNumber(landLine);
- }
-
- // Return status
- return isUnlinked;
- }
-
- /**
- * Updates mobile data in contact instance. This method also removes the
- * mobile instance if no provider is selected. A bean (mostly EJB) should
- * then make sure that the mobile entry is being unlinked from contact
- * instance or being removed, if no longer used.
- * <p>
- * @param contact Contact instance to update
- * @param mobileProvider New mobile provider (or old)
- * @param mobileNumber New mobile number (or old)
- * <p>
- * @return Whether the mobile has been unlinked in contact object
- */
- public static boolean updateMobileNumber (final Contact contact, final MobileProvider mobileProvider, final Long mobileNumber) {
- // At least contact must be valid
- if (null == contact) {
- // Throw NPE
- throw new NullPointerException("contact is null"); //NOI18N
- } else if ((mobileProvider instanceof MobileProvider) && (null == mobileNumber)) {
- // Mobile provider given, but no number
- throw new NullPointerException("mobileNumber is null"); //NOI18N
- }
-
- // Default is not unlinked
- boolean isUnlinked = false;
-
- // Is there a mobile number?
- if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
- // Is provider null?
- if ((null == mobileProvider) || (null == mobileNumber) || (mobileNumber == 0)) {
- // Remove instance
- contact.setContactMobileNumber(null);
-
- // Mark as unlinked
- isUnlinked = true;
- } else {
- // Yes, then update as well
- contact.getContactMobileNumber().setMobileProvider(mobileProvider);
- contact.getContactMobileNumber().setMobileNumber(mobileNumber);
- }
- } else if ((mobileProvider instanceof MobileProvider) && (mobileNumber > 0)) {
- // Create new instance
- final DialableMobileNumber mobile = new MobileNumber(mobileProvider, mobileNumber);
-
- // Set it in contact
- contact.setContactMobileNumber(mobile);
- }
-
- // Return status
- return isUnlinked;
- }
-
- /**
- * Private constructor for utilities
- */
- private ContactUtils () {
- }
-
-}
import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
import org.mxchange.jcoreutils.Comparables;
import org.mxchange.jcoreutils.SafeNumberUtils;
-import org.mxchange.jcountry.model.data.CountryUtils;
+import org.mxchange.jcountry.model.utils.CountryUtils;
import org.mxchange.jcountry.model.data.Country;
import org.mxchange.jcountry.model.data.CountryData;
import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
--- /dev/null
+/*
+ * Copyright (C) 2016 - 2022 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcontacts.model.utils;
+
+import java.io.Serializable;
+import java.util.Objects;
+import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.jcountry.model.data.Country;
+import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
+import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
+import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
+import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
+import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
+
+/**
+ * Utilities for contacts
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class ContactUtils implements Serializable {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 26_785_734_719_670L;
+
+ /**
+ * Compares both contact instances. This method returns -1 if second
+ * instance is null.
+ * <p>
+ * @param contact1 Contact instance 1
+ * @param contact2 Contact instance 2
+ * <p>
+ * @return Comparison value
+ */
+ public static int compare (final Contact contact1, final Contact contact2) {
+ // Check equality, then at least first must be given
+ if (Objects.equals(contact1, contact2)) {
+ // Both are same
+ return 0;
+ } else if (null == contact1) {
+ // First is null
+ return -1;
+ } else if (null == contact2) {
+ // Second is null
+ return 1;
+ }
+
+ // Invoke compareTo() method
+ return contact1.compareTo(contact2);
+ }
+
+ /**
+ * Copies all attributes from other contact object to this
+ * <p>
+ * @param sourceContact Source instance
+ * @param targetContact Target instance
+ */
+ public static void copyContactData (final Contact sourceContact, final Contact targetContact) {
+ // Contact should be valid
+ if (null == sourceContact) {
+ // Throw NPE
+ throw new NullPointerException("sourceContact is null"); //NOI18N
+ } else if (null == targetContact) {
+ // Throw NPE
+ throw new NullPointerException("targetContact is null"); //NOI18N
+ } else if (Objects.equals(sourceContact, targetContact)) {
+ // Throw IAE
+ throw new IllegalArgumentException("sourceContact and targetContact are the same."); //NOI18N
+ }
+
+ // Copy all:
+ // - base data
+ targetContact.setContactPersonalTitle(sourceContact.getContactPersonalTitle());
+ targetContact.setContactTitle(sourceContact.getContactTitle());
+ targetContact.setContactFirstName(sourceContact.getContactFirstName());
+ targetContact.setContactFamilyName(sourceContact.getContactFamilyName());
+ targetContact.setContactStreet(sourceContact.getContactStreet());
+ targetContact.setContactHouseNumber(sourceContact.getContactHouseNumber());
+ targetContact.setContactHouseNumberExtension(sourceContact.getContactHouseNumberExtension());
+ targetContact.setContactZipCode(sourceContact.getContactZipCode());
+ targetContact.setContactCity(sourceContact.getContactCity());
+ targetContact.setContactCountry(sourceContact.getContactCountry());
+
+ // - phone, fax, email
+ targetContact.setContactLandLineNumber(sourceContact.getContactLandLineNumber());
+ targetContact.setContactFaxNumber(sourceContact.getContactFaxNumber());
+ targetContact.setContactMobileNumber(sourceContact.getContactMobileNumber());
+
+ // - other data
+ targetContact.setContactBirthday(sourceContact.getContactBirthday());
+ targetContact.setContactComment(sourceContact.getContactComment());
+ }
+
+ /**
+ * Checks whether both contacts are same, but ignoring id number. If you
+ * want to include id number in comparison, better use Objects.equals() as
+ * the equal() method is implemented and checks all fields.
+ * <p>
+ * @param contact Contact one
+ * @param other Contact two
+ * <p>
+ * @return Whether both are the same
+ */
+ public static boolean isSameContact (final Contact contact, final Contact other) {
+ // Both should not be null
+ if (null == contact) {
+ // First contact is null
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (null == other) {
+ // Secondcontact is null
+ throw new NullPointerException("other is null"); //NOI18N
+ }
+
+ // Check all data fields, except id number
+ return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) &&
+ (Objects.equals(contact.getContactCity(), other.getContactCity())) &&
+ (Objects.equals(contact.getContactCountry(), other.getContactCountry())) &&
+ (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) &&
+ (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) &&
+ (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) &&
+ (Objects.equals(contact.getContactPersonalTitle(), other.getContactPersonalTitle())) &&
+ (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) &&
+ (Objects.equals(contact.getContactStreet(), other.getContactStreet())) &&
+ (Objects.equals(contact.getContactTitle(), other.getContactTitle())) &&
+ (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())));
+ }
+
+ /**
+ * Updates land-line data in contact instance. This method also removes the
+ * land-line instance if no country is selected. A bean (mostly EJB) should
+ * then make sure that the land-line entry is being unlinked from contact
+ * instance or being removed, if no longer used.
+ * <p>
+ * @param contact Contact instance being updated
+ * @param faxCountry Updated fax number or null
+ * @param faxAreaCode Updated fax area code or null
+ * @param faxNumber Updated fax number
+ * <p>
+ * @return Whether the fax number has been unlinked in contact object
+ */
+ public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
+ // At least contact must be valid
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ // Default is not unlinked
+ boolean isUnlinked = false;
+
+ // Is there a fax instance?
+ if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
+ // Found existing fax number, remove it?
+ if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
+ // Remove existing instance
+ contact.setContactFaxNumber(null);
+
+ // Mark it as being removed
+ isUnlinked = true;
+ } else {
+ // Set all data
+ contact.getContactFaxNumber().setPhoneCountry(faxCountry);
+ contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
+ contact.getContactFaxNumber().setPhoneNumber(faxNumber);
+ }
+ } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
+ // Set new land-line number
+ final DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
+
+ // Set it in contact
+ contact.setContactFaxNumber(fax);
+ }
+
+ // Return status
+ return isUnlinked;
+ }
+
+ /**
+ * Updates land-line data in contact instance. This method also removes the
+ * land-line instance if no country is selected. A bean (mostly EJB) should
+ * then make sure that the land-line entry is being unlinked from contact
+ * instance or being removed, if no longer used.
+ * <p>
+ * @param contact Contact instance being updated
+ * @param phoneCountry New phone country or old or null
+ * @param phoneAreaCode New phone's area code (or old)
+ * @param phoneNumber New phone number (or old)
+ * <p>
+ * @return Whether the land-line number has been unlinked in contact object
+ */
+ public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
+ // At least contact must be valid
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ // Default is not unlinked
+ boolean isUnlinked = false;
+
+ // Is there a land-line instance?
+ if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+ // Found existing land-line number, remove it?
+ if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
+ // Remove existing instance
+ contact.setContactLandLineNumber(null);
+
+ // Mark it as being removed
+ isUnlinked = true;
+ } else {
+ // Set all data
+ contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
+ contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
+ contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
+ }
+ } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
+ // Set new land-line number
+ final DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
+
+ // Set it in contact
+ contact.setContactLandLineNumber(landLine);
+ }
+
+ // Return status
+ return isUnlinked;
+ }
+
+ /**
+ * Updates mobile data in contact instance. This method also removes the
+ * mobile instance if no provider is selected. A bean (mostly EJB) should
+ * then make sure that the mobile entry is being unlinked from contact
+ * instance or being removed, if no longer used.
+ * <p>
+ * @param contact Contact instance to update
+ * @param mobileProvider New mobile provider (or old)
+ * @param mobileNumber New mobile number (or old)
+ * <p>
+ * @return Whether the mobile has been unlinked in contact object
+ */
+ public static boolean updateMobileNumber (final Contact contact, final MobileProvider mobileProvider, final Long mobileNumber) {
+ // At least contact must be valid
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if ((mobileProvider instanceof MobileProvider) && (null == mobileNumber)) {
+ // Mobile provider given, but no number
+ throw new NullPointerException("mobileNumber is null"); //NOI18N
+ }
+
+ // Default is not unlinked
+ boolean isUnlinked = false;
+
+ // Is there a mobile number?
+ if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
+ // Is provider null?
+ if ((null == mobileProvider) || (null == mobileNumber) || (mobileNumber == 0)) {
+ // Remove instance
+ contact.setContactMobileNumber(null);
+
+ // Mark as unlinked
+ isUnlinked = true;
+ } else {
+ // Yes, then update as well
+ contact.getContactMobileNumber().setMobileProvider(mobileProvider);
+ contact.getContactMobileNumber().setMobileNumber(mobileNumber);
+ }
+ } else if ((mobileProvider instanceof MobileProvider) && (mobileNumber > 0)) {
+ // Create new instance
+ final DialableMobileNumber mobile = new MobileNumber(mobileProvider, mobileNumber);
+
+ // Set it in contact
+ contact.setContactMobileNumber(mobile);
+ }
+
+ // Return status
+ return isUnlinked;
+ }
+
+ /**
+ * Private constructor for utilities
+ */
+ private ContactUtils () {
+ }
+
+}