From 1f3811ba4681ac3a583b2d08f54d541d22f73979 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 26 Apr 2016 11:05:55 +0200 Subject: [PATCH] Added isSameContact() which checks if it the same "contact" as humans would expect (no direct object comparison). --- .../jcontacts/contact/utils/ContactUtils.java | 417 ++++++++++-------- 1 file changed, 226 insertions(+), 191 deletions(-) diff --git a/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java b/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java index 3718069..4b5baf3 100644 --- a/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java +++ b/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java @@ -1,191 +1,226 @@ -/* - * Copyright (C) 2016 Roland Haeder - * - * 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 . - */ -package org.mxchange.jcontacts.contact.utils; - -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcore.BaseFrameworkSystem; -import org.mxchange.jcountry.data.Country; -import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.fax.FaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; -import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; - -/** - * Utilities for contacts - *

- * @author Roland Haeder - */ -public class ContactUtils extends BaseFrameworkSystem { - - /** - * Updates cellphone data in contact instance. This method also removes the - * cellphone instance if no provider is selected. A bean (mostly EJB) should - * then make sure that the cellphone entry is being unlinked from contact - * instance or being removed, if no longer used. - *

- * @param contact Contact instance to update - * @param cellphoneProvider New cellphone provider (or old) - * @param cellphoneNumber New cellphone number (or old) - *

- * @return Whether the cellphone has been unlinked in contact object - */ - public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) { - // 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 cellphone number? - if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) { - // Is provider null? - if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) { - // Remove instance - contact.setContactCellphoneNumber(null); - - // Mark as unlinked - isUnlinked = true; - } else { - // Yes, then update as well - contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider); - contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber); - } - } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) { - // Create new instance - DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber); - - // Set it in contact - contact.setContactCellphoneNumber(cellphone); - } - - // 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. - *

- * @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 - *

- * @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 - 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. - *

- * @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) - *

- * @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 - DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber); - - // Set it in contact - contact.setContactLandLineNumber(landLine); - } - - // Return status - return isUnlinked; - } - - /** - * Private constructor for utilities - */ - private ContactUtils () { - } - -} +/* + * Copyright (C) 2016 Roland Haeder + * + * 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 . + */ +package org.mxchange.jcontacts.contact.utils; + +import java.util.Objects; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcore.BaseFrameworkSystem; +import org.mxchange.jcountry.data.Country; +import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.fax.FaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; +import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; + +/** + * Utilities for contacts + *

+ * @author Roland Haeder + */ +public class ContactUtils extends BaseFrameworkSystem { + + /** + * 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. + *

+ * @param contact Contact one + * @param other Contact two + *

+ * @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.getContactGender(), other.getContactGender())) && + (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 cellphone data in contact instance. This method also removes the + * cellphone instance if no provider is selected. A bean (mostly EJB) should + * then make sure that the cellphone entry is being unlinked from contact + * instance or being removed, if no longer used. + *

+ * @param contact Contact instance to update + * @param cellphoneProvider New cellphone provider (or old) + * @param cellphoneNumber New cellphone number (or old) + *

+ * @return Whether the cellphone has been unlinked in contact object + */ + public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) { + // 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 cellphone number? + if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) { + // Is provider null? + if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) { + // Remove instance + contact.setContactCellphoneNumber(null); + + // Mark as unlinked + isUnlinked = true; + } else { + // Yes, then update as well + contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider); + contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber); + } + } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) { + // Create new instance + DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber); + + // Set it in contact + contact.setContactCellphoneNumber(cellphone); + } + + // 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. + *

+ * @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 + *

+ * @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 + 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. + *

+ * @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) + *

+ * @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 + DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber); + + // Set it in contact + contact.setContactLandLineNumber(landLine); + } + + // Return status + return isUnlinked; + } + + /** + * Private constructor for utilities + */ + private ContactUtils () { + } + +} -- 2.39.5