]> git.mxchange.org Git - jphone-core.git/blob - src/org/mxchange/jphone/utils/PhoneUtils.java
Added methods for comparing cellphone, land-line and fax numbers
[jphone-core.git] / src / org / mxchange / jphone / utils / PhoneUtils.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jphone.utils;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
22 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
23 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
24
25 /**
26  *
27  * @author Roland Haeder<roland@mxchange.org>
28  */
29 public class PhoneUtils implements Serializable {
30
31         /**
32          * Serial number
33          */
34         private static final long serialVersionUID = 183_598_328_176_450L;
35
36         /**
37          * Checks if both are the same
38          * <p>
39          * @param cellphoneNumber Cellphone number 1
40          * @param otherNumber Cellphone number 2
41          * <p>
42          * @return Whether both are the same number
43          */
44         public static boolean isSameCellphoneNumber (final DialableCellphoneNumber cellphoneNumber, final DialableCellphoneNumber otherNumber) {
45                 // Test object equality first
46                 if (Objects.equals(cellphoneNumber, otherNumber)) {
47                         // Both the same object (null/null or same object)
48                         return true;
49                 } else if (((null == cellphoneNumber) && (otherNumber instanceof DialableCellphoneNumber)) || ((null == otherNumber) && (cellphoneNumber instanceof DialableCellphoneNumber))) {
50                         // One is null the other not
51                         return false;
52                 }
53
54                 // Now compare deeper
55                 @SuppressWarnings ("null")
56                 boolean sameProvider = Objects.equals(cellphoneNumber.getCellphoneProvider(), otherNumber.getCellphoneProvider());
57                 boolean sameNumber = Objects.equals(cellphoneNumber.getPhoneNumber(), otherNumber.getPhoneNumber());
58
59                 // All are the same?
60                 return (sameProvider && sameNumber);
61         }
62
63         /**
64          * Checks if both are the same
65          * <p>
66          * @param faxNumber First fax number
67          * @param otherNumber Second fax number
68          * <p>
69          * @return Whether both are the same
70          */
71         public static boolean isSameFaxNumber (final DialableFaxNumber faxNumber, DialableFaxNumber otherNumber) {
72                 // Test object equality first
73                 if (Objects.equals(faxNumber, otherNumber)) {
74                         // Both the same object (null/null or same object)
75                         return true;
76                 } else if (((null == faxNumber) && (otherNumber instanceof DialableFaxNumber)) || ((null == otherNumber) && (faxNumber instanceof DialableFaxNumber))) {
77                         // One is null the other not
78                         return false;
79                 }
80
81                 // Now compare deeper
82                 @SuppressWarnings ("null")
83                 boolean sameCountry = Objects.equals(faxNumber.getPhoneCountry(), otherNumber.getPhoneCountry());
84                 boolean sameAreaCode = Objects.equals(faxNumber.getPhoneAreaCode(), otherNumber.getPhoneAreaCode());
85                 boolean sameNumber = Objects.equals(faxNumber.getPhoneNumber(), otherNumber.getPhoneNumber());
86
87                 // All are the same?
88                 return (sameCountry && sameAreaCode && sameNumber);
89         }
90
91         /**
92          * Checks if both are the same
93          * <p>
94          * @param landLineNumber First land-line number
95          * @param otherNumber Second land-line number
96          * <p>
97          * @return Whether both are the same
98          */
99         public static boolean isSameLandLineNumber (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber otherNumber) {
100                 // Test object equality first
101                 if (Objects.equals(landLineNumber, otherNumber)) {
102                         // Both the same object (null/null or same object)
103                         return true;
104                 } else if (((null == landLineNumber) && (otherNumber instanceof DialableLandLineNumber)) || ((null == otherNumber) && (landLineNumber instanceof DialableLandLineNumber))) {
105                         // One is null the other not
106                         return false;
107                 }
108
109                 // Now compare deeper
110                 @SuppressWarnings ("null")
111                 boolean sameCountry = Objects.equals(landLineNumber.getPhoneCountry(), otherNumber.getPhoneCountry());
112                 boolean sameAreaCode = Objects.equals(landLineNumber.getPhoneAreaCode(), otherNumber.getPhoneAreaCode());
113                 boolean sameNumber = Objects.equals(landLineNumber.getPhoneNumber(), otherNumber.getPhoneNumber());
114
115                 // All are the same?
116                 return (sameCountry && sameAreaCode && sameNumber);
117         }
118
119         /**
120          * Private constructor for utility classes
121          */
122         private PhoneUtils () {
123         }
124
125 }