From 639e528462ec1653af89f67791bb4eb5bbca3d02 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 26 Apr 2016 13:59:34 +0200 Subject: [PATCH] Added methods for comparing cellphone, land-line and fax numbers --- src/org/mxchange/jphone/utils/PhoneUtils.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/org/mxchange/jphone/utils/PhoneUtils.java diff --git a/src/org/mxchange/jphone/utils/PhoneUtils.java b/src/org/mxchange/jphone/utils/PhoneUtils.java new file mode 100644 index 0000000..94e28ed --- /dev/null +++ b/src/org/mxchange/jphone/utils/PhoneUtils.java @@ -0,0 +1,125 @@ +/* + * 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.jphone.utils; + +import java.io.Serializable; +import java.util.Objects; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; + +/** + * + * @author Roland Haeder + */ +public class PhoneUtils implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 183_598_328_176_450L; + + /** + * Checks if both are the same + *

+ * @param cellphoneNumber Cellphone number 1 + * @param otherNumber Cellphone number 2 + *

+ * @return Whether both are the same number + */ + public static boolean isSameCellphoneNumber (final DialableCellphoneNumber cellphoneNumber, final DialableCellphoneNumber otherNumber) { + // Test object equality first + if (Objects.equals(cellphoneNumber, otherNumber)) { + // Both the same object (null/null or same object) + return true; + } else if (((null == cellphoneNumber) && (otherNumber instanceof DialableCellphoneNumber)) || ((null == otherNumber) && (cellphoneNumber instanceof DialableCellphoneNumber))) { + // One is null the other not + return false; + } + + // Now compare deeper + @SuppressWarnings ("null") + boolean sameProvider = Objects.equals(cellphoneNumber.getCellphoneProvider(), otherNumber.getCellphoneProvider()); + boolean sameNumber = Objects.equals(cellphoneNumber.getPhoneNumber(), otherNumber.getPhoneNumber()); + + // All are the same? + return (sameProvider && sameNumber); + } + + /** + * Checks if both are the same + *

+ * @param faxNumber First fax number + * @param otherNumber Second fax number + *

+ * @return Whether both are the same + */ + public static boolean isSameFaxNumber (final DialableFaxNumber faxNumber, DialableFaxNumber otherNumber) { + // Test object equality first + if (Objects.equals(faxNumber, otherNumber)) { + // Both the same object (null/null or same object) + return true; + } else if (((null == faxNumber) && (otherNumber instanceof DialableFaxNumber)) || ((null == otherNumber) && (faxNumber instanceof DialableFaxNumber))) { + // One is null the other not + return false; + } + + // Now compare deeper + @SuppressWarnings ("null") + boolean sameCountry = Objects.equals(faxNumber.getPhoneCountry(), otherNumber.getPhoneCountry()); + boolean sameAreaCode = Objects.equals(faxNumber.getPhoneAreaCode(), otherNumber.getPhoneAreaCode()); + boolean sameNumber = Objects.equals(faxNumber.getPhoneNumber(), otherNumber.getPhoneNumber()); + + // All are the same? + return (sameCountry && sameAreaCode && sameNumber); + } + + /** + * Checks if both are the same + *

+ * @param landLineNumber First land-line number + * @param otherNumber Second land-line number + *

+ * @return Whether both are the same + */ + public static boolean isSameLandLineNumber (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber otherNumber) { + // Test object equality first + if (Objects.equals(landLineNumber, otherNumber)) { + // Both the same object (null/null or same object) + return true; + } else if (((null == landLineNumber) && (otherNumber instanceof DialableLandLineNumber)) || ((null == otherNumber) && (landLineNumber instanceof DialableLandLineNumber))) { + // One is null the other not + return false; + } + + // Now compare deeper + @SuppressWarnings ("null") + boolean sameCountry = Objects.equals(landLineNumber.getPhoneCountry(), otherNumber.getPhoneCountry()); + boolean sameAreaCode = Objects.equals(landLineNumber.getPhoneAreaCode(), otherNumber.getPhoneAreaCode()); + boolean sameNumber = Objects.equals(landLineNumber.getPhoneNumber(), otherNumber.getPhoneNumber()); + + // All are the same? + return (sameCountry && sameAreaCode && sameNumber); + } + + /** + * Private constructor for utility classes + */ + private PhoneUtils () { + } + +} -- 2.39.5