From 8f331f126edddc08fc09d1787d85c301752b8eb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 19 Jan 2023 08:00:12 +0100 Subject: [PATCH] Continued: - added new method SkillUtils.isSameSkill() - added some constructors to exceptions --- .../SkillAlreadyAddedException.java | 13 +++++ .../exceptions/SkillNotFoundException.java | 11 +++++ .../jjobs/model/utils/SkillUtils.java | 48 +++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/org/mxchange/jjobs/exceptions/SkillAlreadyAddedException.java b/src/org/mxchange/jjobs/exceptions/SkillAlreadyAddedException.java index 1e63e92..7a02468 100644 --- a/src/org/mxchange/jjobs/exceptions/SkillAlreadyAddedException.java +++ b/src/org/mxchange/jjobs/exceptions/SkillAlreadyAddedException.java @@ -16,6 +16,9 @@ */ package org.mxchange.jjobs.exceptions; +import java.text.MessageFormat; +import org.mxchange.jjobs.model.skill.Skillable; + /** * An exception being thrown when a skill instance has already been found *

@@ -28,4 +31,14 @@ public class SkillAlreadyAddedException extends Exception { */ private static final long serialVersionUID = 42_895_928_956_700_002L; + /** + * Constructor with a Skillable class + *

+ * @param skill An instance of a Skillable class + */ + public SkillAlreadyAddedException (final Skillable skill) { + // Construct message and invoke super + super(MessageFormat.format("Skill with id={0},name={1} already added", skill.getSkillId(), skill.getSkillName())); + } + } diff --git a/src/org/mxchange/jjobs/exceptions/SkillNotFoundException.java b/src/org/mxchange/jjobs/exceptions/SkillNotFoundException.java index 1cf8687..0dd46d4 100644 --- a/src/org/mxchange/jjobs/exceptions/SkillNotFoundException.java +++ b/src/org/mxchange/jjobs/exceptions/SkillNotFoundException.java @@ -17,6 +17,7 @@ package org.mxchange.jjobs.model.exceptions; import java.text.MessageFormat; +import org.mxchange.jjobs.model.skill.Skillable; /** * An exception thrown when a Skillable instance wasn't found @@ -39,4 +40,14 @@ public class SkillNotFoundException extends Exception { super(MessageFormat.format("Skill id {0} not found.", skillId)); //NOI18N } + /** + * Constructor with Skillable instance + *

+ * @param skillable An instance of a Skillable class + */ + public SkillNotFoundException (final Skillable skillable) { + // Construct message and invoke super + super(MessageFormat.format("Skill with name={0} not found.", skillable.getSkillName())); + } + } diff --git a/src/org/mxchange/jjobs/model/utils/SkillUtils.java b/src/org/mxchange/jjobs/model/utils/SkillUtils.java index 2a57e2e..4b42105 100644 --- a/src/org/mxchange/jjobs/model/utils/SkillUtils.java +++ b/src/org/mxchange/jjobs/model/utils/SkillUtils.java @@ -17,6 +17,7 @@ package org.mxchange.jjobs.model.utils; import java.util.Objects; +import org.apache.commons.lang3.StringUtils; import org.mxchange.jjobs.model.skill.Skillable; /** @@ -78,6 +79,53 @@ public class SkillUtils { targetSkillable.setSkillStatus(sourceSkillable.getSkillStatus()); } + /** + * Checks if both instances are the same + *

+ * @param skillable1 First instance of a Skillable class + * @param skillable2 Second instance of a Skillable class + *

+ * @return Whether they are the same + */ + public static boolean isSameSkill (final Skillable skillable1, final Skillable skillable2) { + // Validate parameter + if (null == skillable1) { + // Throw NPE + throw new NullPointerException("Parameter 'skillable1' is null"); // NOI18N + } else if (null == skillable2) { + // Throw it again + throw new NullPointerException("Parameter 'skillable2' is null"); // NOI18N + } else if (Objects.equals(skillable1, skillable2)) { + // Is the same + return true; + } else if (skillable1.getSkillName() == null) { + // Throw NPE again + throw new NullPointerException("skillable1.skillName is null"); // NOI18N + } else if (skillable1.getSkillName().isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("skillable1.skillName is empty"); // NOI18N + } else if (skillable2.getSkillName() == null) { + // Throw NPE again + throw new NullPointerException("skillable2.skillName is null"); // NOI18N + } else if (skillable2.getSkillName() == null) { + // Throw NPE again + throw new NullPointerException("skillable2.skillName is null"); // NOI18N + } else if (skillable2.getSkillName().isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("skillable2.skillName is empty"); // NOI18N + } else if (skillable1.getSkillStatus() == null) { + // Throw NPE again + throw new NullPointerException("skillable1.skillStatus is null"); // NOI18N + } else if (skillable2.getSkillStatus() == null) { + // Throw NPE again + throw new NullPointerException("skillable2.skillStatus is null"); // NOI18N + } + + // Now that all at least required fields are there, let's check them all + return ((StringUtils.equals(skillable1.getSkillName(), skillable2.getSkillName())) && + (skillable1.getSkillStatus().equals(skillable2.getSkillStatus()))); + } + /** * Utilities classes don't have constructors */ -- 2.39.5