2 * Copyright (C) 2017, 2018 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcontactsbusiness.model.department;
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;
26 * Utilities class for departments
28 * @author Roland Häder<roland@mxchange.org>
30 public class Departments implements Serializable {
35 private static final long serialVersionUID = 20_154_686_786_561L;
38 * Compares both department instances. This method returns -1 if second
41 * @param department1 Department instance 1
42 * @param department2 Department instance 2
44 * @return Comparison value
46 * @throws NullPointerException If first instance is null
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)) {
53 } else if (null == department1) {
54 // First cannot be null
55 throw new NullPointerException("department1 is null"); //NOI18N
56 } else if (null == department2) {
61 // Invoke compareTo() method
62 return department1.compareTo(department2);
66 * Checks if both departments are the same (entity) or if at least basic
67 * company data and department name are matching.
69 * @param department1 Department 1
70 * @param department2 Department 2
72 * @return Whether both are the same
74 public static boolean isSameDepartment (final Department department1, final Department department2) {
76 if (null == department1) {
78 throw new NullPointerException("department1 is null"); //NOI18N
79 } else if (null == department2) {
81 throw new NullPointerException("department2 is null"); //NOI18N
82 } else if (Objects.equals(department1, department2)) {
85 } else if (department1.getDepartmentCompany() == null) {
87 throw new NullPointerException("department1.departmentCompany is null"); //NOI18N
88 } else if (department1.getDepartmentCompany().getBasicDataId() == null) {
90 throw new NullPointerException("department1.departmentCompany.basicDataId is null"); //NOI18N
91 } else if (department1.getDepartmentCompany().getBasicDataId() < 1) {
93 throw new NullPointerException(MessageFormat.format("department1.departmentCompany.basicDataId={0} is valid.", department1.getDepartmentCompany().getBasicDataId())); //NOI18N
94 } else if (department1.getDepartmentI18nKey() == null) {
96 throw new NullPointerException("department1.departmentName is null"); //NOI18N
97 } else if (department1.getDepartmentI18nKey().isEmpty()) {
99 throw new IllegalArgumentException("department1.departmentName is empty"); //NOI18N
100 } else if (department2.getDepartmentCompany() == null) {
102 throw new NullPointerException("department2.departmentCompany is null"); //NOI18N
103 } else if (department2.getDepartmentCompany().getBasicDataId() == null) {
105 throw new NullPointerException("department2.departmentCompany.basicDataId is null"); //NOI18N
106 } else if (department2.getDepartmentCompany().getBasicDataId() < 1) {
108 throw new NullPointerException(MessageFormat.format("department2.departmentCompany.basicDataId={0} is valid.", department2.getDepartmentCompany().getBasicDataId())); //NOI18N
109 } else if (department2.getDepartmentI18nKey() == null) {
111 throw new NullPointerException("department2.departmentName is null"); //NOI18N
112 } else if (department2.getDepartmentI18nKey().isEmpty()) {
114 throw new IllegalArgumentException("department2.departmentName is empty"); //NOI18N
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())));
130 * Private default constructor
132 private Departments () {
133 // Prevent instances from utilities classes