]> git.mxchange.org Git - jphone-core.git/blob - src/org/mxchange/jphone/model/utils/FaxNumberUtils.java
21bc09511cf6d5de921ae81a58c2886046cab91a
[jphone-core.git] / src / org / mxchange / jphone / model / utils / FaxNumberUtils.java
1 /*
2  * Copyright (C) 2017 - 2022 Free Software Foundation
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.model.utils;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
22
23 /**
24  * An utilities class for fax numbers
25  * <p>
26  * @author Roland Häder<roland@mxchange.org>
27  */
28 public class FaxNumberUtils implements Serializable {
29
30         /**
31          * Serial number
32          */
33         private static final long serialVersionUID = 1_948_653_672_761L;
34
35         /**
36          * Compares two fax number instances with each other
37          * <p>
38          * @param faxNumber1 First instance of a DialableFaxNumber class
39          * @param faxNumber2 Second instance of a DialableFaxNumber class
40          * <p>
41          * @return Comparison value
42          */
43         public static int compare (final DialableFaxNumber faxNumber1, final DialableFaxNumber faxNumber2) {
44                 // Check equality, then at least first must be given
45                 if (Objects.equals(faxNumber1, faxNumber2)) {
46                         // Both are same
47                         return 0;
48                 } else if (null == faxNumber1) {
49                         // First is null
50                         return -1;
51                 } else if (null == faxNumber2) {
52                         // Second is null
53                         return 1;
54                 }
55
56                 // Invoke compareTo() method
57                 return faxNumber1.compareTo(faxNumber2);
58         }
59
60         /**
61          * Copy all fields from source object to this
62          * <p>
63          * @param sourceNumber Source number object
64          * @param targetNumber Target number object
65          */
66         public static void copyFaxNumberData (final DialableFaxNumber sourceNumber, final DialableFaxNumber targetNumber) {
67                 // Validate instance
68                 if (null == sourceNumber) {
69                         // Throw NPE
70                         throw new NullPointerException("sourceNumber is null"); //NOI18N
71                 } else if (null == targetNumber) {
72                         // Throw NPE
73                         throw new NullPointerException("targetNumber is null"); //NOI18N
74                 } else if (Objects.equals(sourceNumber, targetNumber)) {
75                         // Is exactly the same!
76                         throw new IllegalArgumentException("sourcerNumber and targetNumber are the same."); //NOI18N
77                 }
78
79                 // Copy all
80                 targetNumber.setPhoneAreaCode(sourceNumber.getPhoneAreaCode());
81                 targetNumber.setPhoneCountry(sourceNumber.getPhoneCountry());
82                 targetNumber.setPhoneId(sourceNumber.getPhoneId());
83                 targetNumber.setPhoneNumber(sourceNumber.getPhoneNumber());
84         }
85
86         /**
87          * Checks if both are the same
88          * <p>
89          * @param faxNumber   First fax number
90          * @param otherNumber Second fax number
91          * <p>
92          * @return Whether both are the same
93          */
94         public static boolean isSameFaxNumber (final DialableFaxNumber faxNumber, DialableFaxNumber otherNumber) {
95                 // Test object equality first
96                 if (Objects.equals(faxNumber, otherNumber)) {
97                         // Both the same object (null/null or same object)
98                         return true;
99                 } else if (((null == faxNumber) && (otherNumber instanceof DialableFaxNumber)) || ((null == otherNumber) && (faxNumber instanceof DialableFaxNumber))) {
100                         // One is null the other not
101                         return false;
102                 } else if (null == faxNumber && null == otherNumber) {
103                         // Throw NPE
104                         throw new NullPointerException("Both faxNumber and otherNumber are null"); //NOI18N
105                 }
106
107                 // Now compare deeper
108                 final boolean sameCountry = Objects.equals(faxNumber.getPhoneCountry(), otherNumber.getPhoneCountry());
109                 final boolean sameAreaCode = Objects.equals(faxNumber.getPhoneAreaCode(), otherNumber.getPhoneAreaCode());
110                 final boolean sameNumber = Objects.equals(faxNumber.getPhoneNumber(), otherNumber.getPhoneNumber());
111
112                 // All are the same?
113                 return sameCountry && sameAreaCode && sameNumber;
114         }
115
116         /**
117          * No constructor for utilities classes
118          */
119         private FaxNumberUtils () {
120         }
121
122 }