Package de.bcxp.challenge.common.utility
Class ParameterValidationUtility
java.lang.Object
de.bcxp.challenge.common.utility.ParameterValidationUtility
Utility class providing methods to check method parameters for validity. Used instead of Objects.requireNonNull() for logging purposes, and to throw more fitting exceptions than NullPointerException.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
This is a utility class which provides only static methods, therefore it shouldn't be instantiated. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
nullCheck
(Object o, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the provided object reference is notnull
.static void
validateDocument
(Document document, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the provided document and it's entries are non-null.static void
validateEntries
(Collection<? extends DocumentEntry> entries, boolean allowEmpty, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the providedCollection
ofDocumentEntry
objects contents are non-null.private static void
validateLoggerAndMessages
(org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the providedLogger
and message strings are notnull
.static void
validateNumericTupleDocumentEntries
(List<DocumentEntry> entries, org.apache.logging.log4j.Logger logger) Checks if the list ofDocumentEntry
entries from aDocument
all implement theIEntryWithComparableNumericTuple
interface and are all of the same concrete type.static void
validateString
(String string, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the provided string is non-null and not empty or blank.
-
Field Details
-
STRING_LOG
- See Also:
-
STRING_EXCEPTION
- See Also:
-
COLLECTION_LOG
- See Also:
-
COLLECTION_EXCEPTION
- See Also:
-
DOCUMENT_LOG
- See Also:
-
DOCUMENT_EXCEPTION
- See Also:
-
-
Constructor Details
-
ParameterValidationUtility
private ParameterValidationUtility()This is a utility class which provides only static methods, therefore it shouldn't be instantiated.
-
-
Method Details
-
validateString
public static void validateString(String string, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) throws IllegalArgumentException Validates that the provided string is non-null and not empty or blank.- Parameters:
string
- the string to validatelogger
- the logger to use for warningslogMessage
- the message to log if validation failsexceptionMessage
- the message to include in the thrown exception.- Throws:
IllegalArgumentException
- if the string isnull
or empty
-
nullCheck
public static void nullCheck(Object o, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the provided object reference is notnull
.If the object is
null
, this method logs a warning message using the providedLogger
and then throws anIllegalArgumentException
with the specified exception message.- Parameters:
o
- the object reference to validatelogger
- the logger to use for warningslogMessage
- the message to log if validation failsexceptionMessage
- the message to include in the thrown exception.- Throws:
IllegalArgumentException
- ifo
isnull
-
validateDocument
public static void validateDocument(Document document, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) throws IllegalArgumentException Validates that the provided document and it's entries are non-null.- Parameters:
document
- TheDocument
to validatelogger
- the logger to use for warningslogMessage
- the message to log if validation failsexceptionMessage
- the message to include in the thrown exception- Throws:
IllegalArgumentException
-
validateEntries
public static void validateEntries(Collection<? extends DocumentEntry> entries, boolean allowEmpty, org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) throws IllegalArgumentException, NoSuchElementException, IllegalStateException Validates that the provided
Collection
ofDocumentEntry
objects contents are non-null.- Parameters:
entries
- the entries to validateallowEmpty
- Flag to set true if the entries can be empty, false otherwiselogger
- the logger to use for warningslogMessage
- the message to log if validation failsexceptionMessage
- the message to include in the thrown exception- Throws:
IllegalArgumentException
- if the list isnull
or emptyNoSuchElementException
IllegalStateException
-
validateNumericTupleDocumentEntries
public static void validateNumericTupleDocumentEntries(List<DocumentEntry> entries, org.apache.logging.log4j.Logger logger) throws IllegalArgumentException Checks if the list ofDocumentEntry
entries from aDocument
all implement theIEntryWithComparableNumericTuple
interface and are all of the same concrete type.- Parameters:
entries
-DocumentEntry
objects contained in aDocument
logger
- Logger to log possible error messages to- Throws:
IllegalArgumentException
-
validateLoggerAndMessages
private static void validateLoggerAndMessages(org.apache.logging.log4j.Logger logger, String logMessage, String exceptionMessage) Validates that the providedLogger
and message strings are notnull
.If any of the parameters are
null
, the method logs a warning using the givenLogger
(if notnull
) and throws anIllegalArgumentException
.- Parameters:
logger
- theLogger
instance to validate; must not benull
.logMessage
- the log message string to validate; must not benull
.exceptionMessage
- the exception message string to validate; must not benull
.- Throws:
IllegalArgumentException
- if any parameter isnull
.
-