]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/department/Departments.java
e78550cc80c3f65a9723941cd9cbf49b894310a5
[jcontacts-business-core.git] / src / org / mxchange / jcontactsbusiness / model / department / Departments.java
1 /*
2  * Copyright (C) 2017, 2018 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.jcontactsbusiness.model.department;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
23 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
24
25 /**
26  * Utilities class for departments
27  * <p>
28  * @author Roland Häder<roland@mxchange.org>
29  */
30 public class Departments implements Serializable {
31
32         /**
33          * Serial number
34          */
35         private static final long serialVersionUID = 20_154_686_786_561L;
36
37         /**
38          * Compares both department instances. This method returns -1 if second
39          * instance is null.
40          * <p>
41          * @param department1 Department instance 1
42          * @param department2 Department instance 2
43          * <p>
44          * @return Comparison value
45          */
46         public static int compare (final Department department1, final Department department2) {
47                 // Check euqality, then at least first must be given
48                 if (Objects.equals(department1, department2)) {
49                         // Both are same
50                         return 0;
51                 } else if (null == department1) {
52                         // First is null
53                         return -1;
54                 } else if (null == department2) {
55                         // Second is null
56                         return 1;
57                 }
58
59                 // Invoke compareTo() method
60                 return department1.compareTo(department2);
61         }
62
63         /**
64          * Checks if both departments are the same (entity) or if at least basic
65          * company data and department name are matching.
66          * <p>
67          * @param department1 Department 1
68          * @param department2 Department 2
69          * <p>
70          * @return Whether both are the same
71          */
72         public static boolean isSameDepartment (final Department department1, final Department department2) {
73                 // Now check it
74                 if (null == department1) {
75                         // Throw NPE
76                         throw new NullPointerException("department1 is null"); //NOI18N
77                 } else if (null == department2) {
78                         // Throw it again
79                         throw new NullPointerException("department2 is null"); //NOI18N
80                 } else if (Objects.equals(department1, department2)) {
81                         // Same department
82                         return true;
83                 } else if (department1.getDepartmentCompany() == null) {
84                         // Throw NPE again
85                         throw new NullPointerException("department1.departmentCompany is null"); //NOI18N
86                 } else if (department1.getDepartmentCompany().getBasicDataId() == null) {
87                         // Throw NPE again
88                         throw new NullPointerException("department1.departmentCompany.basicDataId is null"); //NOI18N
89                 } else if (department1.getDepartmentCompany().getBasicDataId() < 1) {
90                         // Throw NPE again
91                         throw new NullPointerException(MessageFormat.format("department1.departmentCompany.basicDataId={0} is valid.", department1.getDepartmentCompany().getBasicDataId())); //NOI18N
92                 } else if (department1.getDepartmentI18nKey() == null) {
93                         // Throw it again
94                         throw new NullPointerException("department1.departmentName is null"); //NOI18N
95                 } else if (department1.getDepartmentI18nKey().isEmpty()) {
96                         // Throw IAE
97                         throw new IllegalArgumentException("department1.departmentName is empty"); //NOI18N
98                 } else if (department2.getDepartmentCompany() == null) {
99                         // Throw NPE again
100                         throw new NullPointerException("department2.departmentCompany is null"); //NOI18N
101                 } else if (department2.getDepartmentCompany().getBasicDataId() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("department2.departmentCompany.basicDataId is null"); //NOI18N
104                 } else if (department2.getDepartmentCompany().getBasicDataId() < 1) {
105                         // Throw NPE again
106                         throw new NullPointerException(MessageFormat.format("department2.departmentCompany.basicDataId={0} is valid.", department2.getDepartmentCompany().getBasicDataId())); //NOI18N
107                 } else if (department2.getDepartmentI18nKey() == null) {
108                         // Throw it again
109                         throw new NullPointerException("department2.departmentName is null"); //NOI18N
110                 } else if (department2.getDepartmentI18nKey().isEmpty()) {
111                         // Throw IAE
112                         throw new IllegalArgumentException("department2.departmentName is empty"); //NOI18N
113                 }
114
115                 // Same company and same name?
116                 boolean isSame = ((Objects.equals(department1.getDepartmentCompany(), department2.getDepartmentCompany())) &&
117                                                   (((department1.getDepartmentBranchOffice() instanceof BranchOffice) &&
118                                                         (Objects.equals(department1.getDepartmentBranchOffice(), department2.getDepartmentBranchOffice()))) ||
119                                                    ((department1.getDepartmentHeadquarter() instanceof Headquarter) &&
120                                                         (Objects.equals(department1.getDepartmentHeadquarter(), department2.getDepartmentHeadquarter())))) &&
121                                                   (Objects.equals(department1.getDepartmentI18nKey(), department2.getDepartmentI18nKey())));
122
123                 // Return it
124                 return isSame;
125         }
126
127         /**
128          * Private default constructor
129          */
130         private Departments () {
131                 // Prevent instances from utilities classes
132         }
133
134 }