]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/department/Departments.java
Continued:
[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          * <p>
46          * @throws NullPointerException If first instance is null
47          */
48         public static int compare (final Department department1, final Department department2) {
49                 // Check euqality, then at least first must be given
50                 if (Objects.equals(department1, department2)) {
51                         // Both are same
52                         return 0;
53                 } else if (null == department1) {
54                         // First cannot be null
55                         throw new NullPointerException("department1 is null"); //NOI18N
56                 } else if (null == department2) {
57                         // Second is null
58                         return 1;
59                 }
60
61                 // Invoke compareTo() method
62                 return department1.compareTo(department2);
63         }
64
65         /**
66          * Checks if both departments are the same (entity) or if at least basic
67          * company data and department name are matching.
68          * <p>
69          * @param department1 Department 1
70          * @param department2 Department 2
71          * <p>
72          * @return Whether both are the same
73          */
74         public static boolean isSameDepartment (final Department department1, final Department department2) {
75                 // Now check it
76                 if (null == department1) {
77                         // Throw NPE
78                         throw new NullPointerException("department1 is null"); //NOI18N
79                 } else if (null == department2) {
80                         // Throw it again
81                         throw new NullPointerException("department2 is null"); //NOI18N
82                 } else if (Objects.equals(department1, department2)) {
83                         // Same department
84                         return true;
85                 } else if (department1.getDepartmentCompany() == null) {
86                         // Throw NPE again
87                         throw new NullPointerException("department1.departmentCompany is null"); //NOI18N
88                 } else if (department1.getDepartmentCompany().getBasicDataId() == null) {
89                         // Throw NPE again
90                         throw new NullPointerException("department1.departmentCompany.basicDataId is null"); //NOI18N
91                 } else if (department1.getDepartmentCompany().getBasicDataId() < 1) {
92                         // Throw NPE again
93                         throw new NullPointerException(MessageFormat.format("department1.departmentCompany.basicDataId={0} is valid.", department1.getDepartmentCompany().getBasicDataId())); //NOI18N
94                 } else if (department1.getDepartmentI18nKey() == null) {
95                         // Throw it again
96                         throw new NullPointerException("department1.departmentName is null"); //NOI18N
97                 } else if (department1.getDepartmentI18nKey().isEmpty()) {
98                         // Throw IAE
99                         throw new IllegalArgumentException("department1.departmentName is empty"); //NOI18N
100                 } else if (department2.getDepartmentCompany() == null) {
101                         // Throw NPE again
102                         throw new NullPointerException("department2.departmentCompany is null"); //NOI18N
103                 } else if (department2.getDepartmentCompany().getBasicDataId() == null) {
104                         // Throw NPE again
105                         throw new NullPointerException("department2.departmentCompany.basicDataId is null"); //NOI18N
106                 } else if (department2.getDepartmentCompany().getBasicDataId() < 1) {
107                         // Throw NPE again
108                         throw new NullPointerException(MessageFormat.format("department2.departmentCompany.basicDataId={0} is valid.", department2.getDepartmentCompany().getBasicDataId())); //NOI18N
109                 } else if (department2.getDepartmentI18nKey() == null) {
110                         // Throw it again
111                         throw new NullPointerException("department2.departmentName is null"); //NOI18N
112                 } else if (department2.getDepartmentI18nKey().isEmpty()) {
113                         // Throw IAE
114                         throw new IllegalArgumentException("department2.departmentName is empty"); //NOI18N
115                 }
116
117                 // Same company and same name?
118                 boolean isSame = ((Objects.equals(department1.getDepartmentCompany(), department2.getDepartmentCompany())) &&
119                                                   (((department1.getDepartmentBranchOffice() instanceof BranchOffice) &&
120                                                         (Objects.equals(department1.getDepartmentBranchOffice(), department2.getDepartmentBranchOffice()))) ||
121                                                    ((department1.getDepartmentHeadquarter() instanceof Headquarter) &&
122                                                         (Objects.equals(department1.getDepartmentHeadquarter(), department2.getDepartmentHeadquarter())))) &&
123                                                   (Objects.equals(department1.getDepartmentI18nKey(), department2.getDepartmentI18nKey())));
124
125                 // Return it
126                 return isSame;
127         }
128
129         /**
130          * Private default constructor
131          */
132         private Departments () {
133                 // Prevent instances from utilities classes
134         }
135
136 }