-/*\r
- * Copyright (C) 2016 Roland Haeder\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package org.mxchange.jcontacts.contact.utils;\r
-\r
-import org.mxchange.jcontacts.contact.Contact;\r
-import org.mxchange.jcore.BaseFrameworkSystem;\r
-import org.mxchange.jcountry.data.Country;\r
-import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;\r
-import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;\r
-import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;\r
-import org.mxchange.jphone.phonenumbers.fax.FaxNumber;\r
-import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;\r
-import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;\r
-import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;\r
-\r
-/**\r
- * Utilities for contacts\r
- * <p>\r
- * @author Roland Haeder<roland@mxchange.org>\r
- */\r
-public class ContactUtils extends BaseFrameworkSystem {\r
-\r
- /**\r
- * Updates cellphone data in contact instance. This method also removes the\r
- * cellphone instance if no provider is selected. A bean (mostly EJB) should\r
- * then make sure that the cellphone entry is being unlinked from contact\r
- * instance or being removed, if no longer used.\r
- * <p>\r
- * @param contact Contact instance to update\r
- * @param cellphoneProvider New cellphone provider (or old)\r
- * @param cellphoneNumber New cellphone number (or old)\r
- * <p>\r
- * @return Whether the cellphone has been unlinked in contact object\r
- */\r
- public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {\r
- // At least contact must be valid\r
- if (null == contact) {\r
- // Throw NPE\r
- throw new NullPointerException("contact is null"); //NOI18N\r
- }\r
-\r
- // Default is not unlinked\r
- boolean isUnlinked = false;\r
-\r
- // Is there a cellphone number?\r
- if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {\r
- // Is provider null?\r
- if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {\r
- // Remove instance\r
- contact.setContactCellphoneNumber(null);\r
-\r
- // Mark as unlinked\r
- isUnlinked = true;\r
- } else {\r
- // Yes, then update as well\r
- contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);\r
- contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);\r
- }\r
- } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {\r
- // Create new instance\r
- DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);\r
-\r
- // Set it in contact\r
- contact.setContactCellphoneNumber(cellphone);\r
- }\r
-\r
- // Return status\r
- return isUnlinked;\r
- }\r
-\r
- /**\r
- * Updates land-line data in contact instance. This method also removes the\r
- * land-line instance if no country is selected. A bean (mostly EJB) should\r
- * then make sure that the land-line entry is being unlinked from contact\r
- * instance or being removed, if no longer used.\r
- * <p>\r
- * @param contact Contact instance being updated\r
- * @param faxCountry Updated fax number or null\r
- * @param faxAreaCode Updated fax area code or null\r
- * @param faxNumber Updated fax number\r
- * <p>\r
- * @return Whether the fax number has been unlinked in contact object\r
- */\r
- public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {\r
- // At least contact must be valid\r
- if (null == contact) {\r
- // Throw NPE\r
- throw new NullPointerException("contact is null"); //NOI18N\r
- }\r
-\r
- // Default is not unlinked\r
- boolean isUnlinked = false;\r
-\r
- // Is there a fax instance?\r
- if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {\r
- // Found existing fax number, remove it?\r
- if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {\r
- // Remove existing instance\r
- contact.setContactFaxNumber(null);\r
-\r
- // Mark it as being removed\r
- isUnlinked = true;\r
- } else {\r
- // Set all data\r
- contact.getContactFaxNumber().setPhoneCountry(faxCountry);\r
- contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);\r
- contact.getContactFaxNumber().setPhoneNumber(faxNumber);\r
- }\r
- } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {\r
- // Set new land-line number\r
- DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);\r
-\r
- // Set it in contact\r
- contact.setContactFaxNumber(fax);\r
- }\r
-\r
- // Return status\r
- return isUnlinked;\r
- }\r
-\r
- /**\r
- * Updates land-line data in contact instance. This method also removes the\r
- * land-line instance if no country is selected. A bean (mostly EJB) should\r
- * then make sure that the land-line entry is being unlinked from contact\r
- * instance or being removed, if no longer used.\r
- * <p>\r
- * @param contact Contact instance being updated\r
- * @param phoneCountry New phone country or old or null\r
- * @param phoneAreaCode New phone's area code (or old)\r
- * @param phoneNumber New phone number (or old)\r
- * <p>\r
- * @return Whether the land-line number has been unlinked in contact object\r
- */\r
- public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {\r
- // At least contact must be valid\r
- if (null == contact) {\r
- // Throw NPE\r
- throw new NullPointerException("contact is null"); //NOI18N\r
- }\r
-\r
- // Default is not unlinked\r
- boolean isUnlinked = false;\r
-\r
- // Is there a land-line instance?\r
- if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {\r
- // Found existing land-line number, remove it?\r
- if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {\r
- // Remove existing instance\r
- contact.setContactLandLineNumber(null);\r
-\r
- // Mark it as being removed\r
- isUnlinked = true;\r
- } else {\r
- // Set all data\r
- contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);\r
- contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);\r
- contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);\r
- }\r
- } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {\r
- // Set new land-line number\r
- DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);\r
-\r
- // Set it in contact\r
- contact.setContactLandLineNumber(landLine);\r
- }\r
-\r
- // Return status\r
- return isUnlinked;\r
- }\r
-\r
- /**\r
- * Private constructor for utilities\r
- */\r
- private ContactUtils () {\r
- }\r
-\r
-}\r
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+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.
+ * <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.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.
+ * <p>
+ * @param contact Contact instance to update
+ * @param cellphoneProvider New cellphone provider (or old)
+ * @param cellphoneNumber New cellphone number (or old)
+ * <p>
+ * @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.
+ * <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
+ 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
+ DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
+
+ // Set it in contact
+ contact.setContactLandLineNumber(landLine);
+ }
+
+ // Return status
+ return isUnlinked;
+ }
+
+ /**
+ * Private constructor for utilities
+ */
+ private ContactUtils () {
+ }
+
+}