public final class Param
extends java.lang.Object
Param
helps check constraints on method and constructor
parameters. This is in accordance with the "fail fast" principle so that
errors can be found much faster than if invalid parameters and states were
ignored.Modifier and Type | Method and Description |
---|---|
static <T> void |
equals(T param,
T expected,
java.lang.String name)
Returns whether the given parameter is what is expected.
|
static void |
greaterThanOrEqual(java.lang.Comparable first,
java.lang.Comparable second,
java.lang.String firstName,
java.lang.String secondName)
Checks that
first is greater than or equal to
second . |
static void |
notNull(java.lang.Object param,
java.lang.String name)
Checks that the
param is not null. |
static void |
notNullOrEmpty(java.util.Collection param,
java.lang.String name)
Checks that the
param is not null or empty. |
static void |
notNullOrEmpty(java.lang.String param,
java.lang.String name)
Checks that the
param is not null or empty. |
public static <T> void equals(T param, T expected, java.lang.String name) throws java.lang.IllegalArgumentException
T
- The type of the input parameter. This cannot be a Double,
Float, or array.param
- The value to check. May be null.expected
- The expected value. May be null.name
- The name of the parameter in the code. This is used to
construct the message for the exception. If null or empty, the
message will be malformed.java.lang.IllegalArgumentException
- If param
is not the same as
expected
, or the type is invalid.public static void greaterThanOrEqual(java.lang.Comparable first, java.lang.Comparable second, java.lang.String firstName, java.lang.String secondName) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
first
is greater than or equal to
second
. Normally this is used to compare numbers, but can
also be used on any Comparable
object if that makes sense to
check.first
- The first value to compare. Cannot be null.second
- The second value to compare. Cannot be null.firstName
- The name of the first value in the code. This is used to
construct the message for the exception. If null or empty, the
message will be malformed.secondName
- The name of the second value in the code. This is used to
construct the message for the exception. If null or empty, the
message will be malformed.java.lang.NullPointerException
- If first
or second
is null.java.lang.IllegalArgumentException
- If first
is less than second
.public static void notNull(java.lang.Object param, java.lang.String name) throws java.lang.NullPointerException
param
is not null. If so, a
NullPointerException is thrown.param
- The parameter value to check. Cannot be null.name
- The name of the parameter in the code. This is used to
construct the message for the exception. If null or empty, the
message will be malformed.java.lang.NullPointerException
- If param
is null.public static void notNullOrEmpty(java.util.Collection param, java.lang.String name) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
param
is not null or empty.param
- The parameter value to check. Cannot be null or empty.name
- The name of the parameter in the code. This is used to
construct the message for the exception. If null or empty, the
message will be malformed.java.lang.NullPointerException
- If param
is null.java.lang.IllegalArgumentException
- If param
is empty.public static void notNullOrEmpty(java.lang.String param, java.lang.String name) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
param
is not null or empty.param
- The parameter value to check. Cannot be null or empty.name
- The name of the parameter in the code. This is used to
construct the message for the exception. If null or empty, the
message will be malformed.java.lang.NullPointerException
- If param
is null.java.lang.IllegalArgumentException
- If param
is empty.