From feb992178bf4163d9bbb5ff79f4784bf958d5d75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 23 Oct 2022 18:03:35 +0200 Subject: [PATCH] Continued: - added a long description, why you should invoke a simplier constructor - added checks on arguments/parameter in some constructors - sorted enum members, thanks to EnumType.STRING you don't have to touch your data --- .../password_history/UserPasswordHistory.java | 32 +++++++++++++++++++ .../model/user/profilemodes/ProfileMode.java | 12 ++++++- .../model/user/status/UserAccountStatus.java | 12 +++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/org/mxchange/jusercore/model/user/password_history/UserPasswordHistory.java b/src/org/mxchange/jusercore/model/user/password_history/UserPasswordHistory.java index 83c73fd..16a64d6 100644 --- a/src/org/mxchange/jusercore/model/user/password_history/UserPasswordHistory.java +++ b/src/org/mxchange/jusercore/model/user/password_history/UserPasswordHistory.java @@ -16,6 +16,7 @@ */ package org.mxchange.jusercore.model.user.password_history; +import java.text.MessageFormat; import java.util.Date; import java.util.Objects; import javax.persistence.Basic; @@ -102,6 +103,37 @@ public class UserPasswordHistory implements PasswordHistory { * @param userPasswordHistoryUser User instance */ public UserPasswordHistory (final String userPasswordHistoryPasswordHash, final User userPasswordHistoryUser) { + /* + * Invoke default constructor. This one here (above this constructor) + * might do nothing useful, that's true. It is an empty method for the + * JPA to invoke, that's true, too. The thing here is, that you might + * become lazy and forget it somewhere else where you really have to + * invoke it or otherwise your object isn't fully/correctly initialized. + * This has leaded already to funny side-effects such as "suddenly it + * doesn't work, then it works again" and also to NPEs in the + * history.Thumb of a rule: If there is a simplier constructor, DO + * INVOKE it. + */ + this(); + + // Validate all parameter + if (null == userPasswordHistoryPasswordHash) { + // Throw NPE + throw new NullPointerException("userPasswordHistoryPasswordHash is null"); //NOI18N + } else if (userPasswordHistoryPasswordHash.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("userPasswordHistoryPasswordHash is empty"); //NOI18N + } else if (null == userPasswordHistoryUser) { + // Throw NPE + throw new NullPointerException("userPasswordHistoryUser is null"); //NOI18N + } else if (userPasswordHistoryUser.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("userPasswordHistoryUser.userId is null"); //NOI18N + } else if (userPasswordHistoryUser.getUserId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("userPasswordHistoryUser.userId={0} is not valid", userPasswordHistoryUser.getUserId())); //NOI18N + } + // Set all this.userPasswordHistoryPasswordHash = userPasswordHistoryPasswordHash; this.userPasswordHistoryUser = userPasswordHistoryUser; diff --git a/src/org/mxchange/jusercore/model/user/profilemodes/ProfileMode.java b/src/org/mxchange/jusercore/model/user/profilemodes/ProfileMode.java index 3e3c562..abab3ac 100644 --- a/src/org/mxchange/jusercore/model/user/profilemodes/ProfileMode.java +++ b/src/org/mxchange/jusercore/model/user/profilemodes/ProfileMode.java @@ -26,7 +26,7 @@ import java.io.Serializable; public enum ProfileMode implements Serializable { /** - * Invisible to guests and members (not administrators) + * Invisible to guests and members (but visible to administrators) */ INVISIBLE("PROFILE_MODE_INVISIBLE"), //NOI18N @@ -51,6 +51,16 @@ public enum ProfileMode implements Serializable { * @param messageKey Message key */ private ProfileMode (final String messageKey) { + // Validate all parameter + if (null == messageKey) { + // Throw NPE + throw new NullPointerException("messageKey is null"); //NOI18N + } else if (messageKey.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("messageKey is empty"); //NOI18N + } + + // Set all enum fields this.messageKey = messageKey; } diff --git a/src/org/mxchange/jusercore/model/user/status/UserAccountStatus.java b/src/org/mxchange/jusercore/model/user/status/UserAccountStatus.java index 584ec89..6da2179 100644 --- a/src/org/mxchange/jusercore/model/user/status/UserAccountStatus.java +++ b/src/org/mxchange/jusercore/model/user/status/UserAccountStatus.java @@ -25,11 +25,6 @@ import java.io.Serializable; */ public enum UserAccountStatus implements Serializable { - /** - * Unconfirmed (default) - */ - UNCONFIRMED("USER_ACCOUNT_STATUS_UNCONFIRMED", "user-status-unconfirmed"), //NOI18N - /** * Confirmed (email address validated) */ @@ -38,7 +33,12 @@ public enum UserAccountStatus implements Serializable { /** * Locked (maybe violated T&C) */ - LOCKED("USER_ACCOUNT_STATUS_LOCKED", "user-status-locked"); //NOI18N + LOCKED("USER_ACCOUNT_STATUS_LOCKED", "user-status-locked"), //NOI18N + + /** + * Unconfirmed (default) + */ + UNCONFIRMED("DEPRECATED", "user-status-unconfirmed"); //NOI18N /** * Message key -- 2.39.5