]> git.mxchange.org Git - jjobs-core.git/blob - src/org/mxchange/jjobs/model/utils/SkillUtils.java
Continued:
[jjobs-core.git] / src / org / mxchange / jjobs / model / utils / SkillUtils.java
1 /*
2  * Copyright (C) 2023 Roland Häder<roland@mxchange.org>
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.jjobs.model.utils;
18
19 import java.util.Objects;
20 import org.apache.commons.lang3.StringUtils;
21 import org.mxchange.jjobs.model.skill.Skillable;
22
23 /**
24  * Utilities class for skills to learn and practice
25  * <p>
26  * @author Roland Häder<roland@mxchange.org>
27  */
28 public class SkillUtils {
29
30         /**
31          * Compares two skills with each other
32          * <p>
33          * @param skill1 Skill instance 1
34          * @param skill2 Skill instance 2
35          * <p>
36          * @return Comparison value
37          */
38         public static int compare (final Skillable skill1, final Skillable skill2) {
39                 // Check equality, then at least first must be given
40                 if (Objects.equals(skill1, skill2)) {
41                         // Both are same
42                         return 0;
43                 } else if (null == skill1) {
44                         // First is null
45                         return -1;
46                 } else if (null == skill2) {
47                         // Second is null
48                         return 1;
49                 }
50
51                 // Invoke compare() method
52                 return skill1.compareTo(skill2);
53         }
54
55         /**
56          * Copies all fields exception created/updated timestamps from source
57          * Skillable instance to target instance all fields.
58          * <p>
59          * @param sourceSkillable Source instance of a Skillable class
60          * @param targetSkillable Target instance of a Skillable class
61          */
62         public static void copySkillData (final Skillable sourceSkillable, final Skillable targetSkillable) {
63                 // Check that both parameters are not null
64                 if (null == sourceSkillable) {
65                         // Throw NPE
66                         throw new NullPointerException("sourceSkillable is null"); //NOI18N
67                 } else if (null == targetSkillable) {
68                         // Throw NPE
69                         throw new NullPointerException("targetSkillable is null"); //NOI18N
70                 } else if (Objects.equals(sourceSkillable, targetSkillable)) {
71                         // Throw IAE
72                         throw new IllegalArgumentException("sourceSkillable and targetSkillable are the same"); //NOI18N
73                 }
74
75                 // Copy all fields
76                 targetSkillable.setSkillId(sourceSkillable.getSkillId());
77                 targetSkillable.setSkillLastLocked(sourceSkillable.getSkillLastLocked());
78                 targetSkillable.setSkillName(sourceSkillable.getSkillName());
79                 targetSkillable.setSkillStatus(sourceSkillable.getSkillStatus());
80         }
81
82         /**
83          * Checks if both instances are the same
84          * <p>
85          * @param skillable1 First instance of a Skillable class
86          * @param skillable2 Second instance of a Skillable class
87          * <p>
88          * @return Whether they are the same
89          */
90         public static boolean isSameSkill (final Skillable skillable1, final Skillable skillable2) {
91                 // Validate parameter
92                 if (null == skillable1) {
93                         // Throw NPE
94                         throw new NullPointerException("Parameter 'skillable1' is null"); // NOI18N
95                 } else if (null == skillable2) {
96                         // Throw it again
97                         throw new NullPointerException("Parameter 'skillable2' is null"); // NOI18N
98                 } else if (Objects.equals(skillable1, skillable2)) {
99                         // Is the same
100                         return true;
101                 } else if (skillable1.getSkillName() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("skillable1.skillName is null"); // NOI18N
104                 } else if (skillable1.getSkillName().isEmpty()) {
105                         // Throw IAE
106                         throw new IllegalArgumentException("skillable1.skillName is empty"); // NOI18N
107                 } else if (skillable2.getSkillName() == null) {
108                         // Throw NPE again
109                         throw new NullPointerException("skillable2.skillName is null"); // NOI18N
110                 } else if (skillable2.getSkillName() == null) {
111                         // Throw NPE again
112                         throw new NullPointerException("skillable2.skillName is null"); // NOI18N
113                 } else if (skillable2.getSkillName().isEmpty()) {
114                         // Throw IAE
115                         throw new IllegalArgumentException("skillable2.skillName is empty"); // NOI18N
116                 } else if (skillable1.getSkillStatus() == null) {
117                         // Throw NPE again
118                         throw new NullPointerException("skillable1.skillStatus is null"); // NOI18N
119                 } else if (skillable2.getSkillStatus() == null) {
120                         // Throw NPE again
121                         throw new NullPointerException("skillable2.skillStatus is null"); // NOI18N
122                 }
123
124                 // Now that all at least required fields are there, let's check them all
125                 return ((StringUtils.equals(skillable1.getSkillName(), skillable2.getSkillName())) &&
126                                 (skillable1.getSkillStatus().equals(skillable2.getSkillStatus())));
127         }
128
129         /**
130          * Utilities classes don't have constructors
131          */
132         private SkillUtils () {
133         }
134 }