]> git.mxchange.org Git - jphone-core.git/blob - src/org/mxchange/jphone/model/utils/LandLineNumberUtils.java
Continued:
[jphone-core.git] / src / org / mxchange / jphone / model / utils / LandLineNumberUtils.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 targetNumber 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.landline.DialableLandLineNumber;
22
23 /**
24  * An utilities class for land-line numbers
25  * <p>
26  * @author Roland Häder<roland@mxchange.org>
27  */
28 public class LandLineNumberUtils implements Serializable {
29
30         /**
31          * Serial number
32          */
33         private static final long serialVersionUID = 18_698_467_372_167_561L;
34
35         /**
36          * Copy all fields from source object to this
37          * <p>
38          * @param sourceNumber Source number object
39          * @param targetNumber Target number object
40          */
41         public static void copyLandLineNumberData (final DialableLandLineNumber sourceNumber, final DialableLandLineNumber targetNumber) {
42                 // Validate instance
43                 if (null == sourceNumber) {
44                         // Throw NPE
45                         throw new NullPointerException("sourceNumber is null"); //NOI18N
46                 } else if (null == targetNumber) {
47                         // Throw NPE
48                         throw new NullPointerException("targetNumber is null"); //NOI18N
49                 } else if (Objects.equals(sourceNumber, targetNumber)) {
50                         // Is exactly the same!
51                         throw new IllegalArgumentException("sourcerNumber and targetNumber are the same."); //NOI18N
52                 }
53
54                 // Copy all
55                 targetNumber.setPhoneAreaCode(sourceNumber.getPhoneAreaCode());
56                 targetNumber.setPhoneCountry(sourceNumber.getPhoneCountry());
57                 targetNumber.setPhoneId(sourceNumber.getPhoneId());
58                 targetNumber.setPhoneNumber(sourceNumber.getPhoneNumber());
59         }
60
61         /**
62          * Checks if both are the same
63          * <p>
64          * @param landLineNumber First land-line number
65          * @param otherNumber    Second land-line number
66          * <p>
67          * @return Whether both are the same
68          */
69         public static boolean isSameLandLineNumber (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber otherNumber) {
70                 // Test object equality first
71                 if (Objects.equals(landLineNumber, otherNumber)) {
72                         // Both the same object (null/null or same object)
73                         return true;
74                 } else if (((null == landLineNumber) && (otherNumber instanceof DialableLandLineNumber)) || ((null == otherNumber) && (landLineNumber instanceof DialableLandLineNumber))) {
75                         // One is null the other not
76                         return false;
77                 }
78                 // Now compare deeper
79                 final boolean sameCountry = Objects.equals(landLineNumber.getPhoneCountry(), otherNumber.getPhoneCountry());
80                 final boolean sameAreaCode = Objects.equals(landLineNumber.getPhoneAreaCode(), otherNumber.getPhoneAreaCode());
81                 final boolean sameNumber = Objects.equals(landLineNumber.getPhoneNumber(), otherNumber.getPhoneNumber());
82                 // All are the same?
83                 return sameCountry && sameAreaCode && sameNumber;
84         }
85
86         /**
87          * No constructor for utilities classes
88          */
89         private LandLineNumberUtils () {
90         }
91
92 }