T
- The argument type, defined by the concrete check classesC
- The type of the concrete check class. This parameter is necessary
for this base class to define methods that return the check object
itself (with the correct static type), which is necessary for the
fluent interface of all checks.@GwtCompatible public abstract class ObjectBaseCheck<T,C extends ObjectBaseCheck<T,C>> extends BaseCheck<C>
BaseCheck
.PrimitiveBaseCheck
Modifier and Type | Field and Description |
---|---|
protected T |
arg
The argument that is checked.
|
protected boolean |
nullAllowed
This flag indicates that
null is a valid value for the
argument of this check. |
Modifier | Constructor and Description |
---|---|
protected |
ObjectBaseCheck(T arg) |
Modifier and Type | Method and Description |
---|---|
protected C |
check(boolean condition,
MessageType msgType,
Object... msgArgs)
A helper method that allows subclasses to perform simple checks
conveniently with a one-liner.
|
protected void |
checkNull()
Performs the implicit
null check that should be done before
each check. |
protected C |
checkWithCause(boolean condition,
MessageType msgType,
Throwable cause,
Object... msgArgs)
The same as
check(boolean, MessageType, Object...) but with the
possibility to define a cause for the exception that is thrown if the
check fails. |
C |
hasClass(Class<?> clazz)
Checks that the argument has the given class, throwing an exception
otherwise.
|
protected IntCheck |
intPropertyCheck(int property,
String propertyName)
Creates an
int property check for this argument. |
C |
isEqualTo(Object object)
Checks that the argument is equal to the
given object, throwing an exception otherwise.
|
C |
isInstanceOf(Class<?> type)
Checks that the argument is an instance of the given type, throwing an
exception otherwise.
|
C |
isNotNull()
Checks that the argument is not
null , throwing an exception
otherwise. |
C |
isNullOr()
Allow the argument to be
null . |
C |
isSameAs(Object object)
Checks that the argument is the same instance as the given object,
throwing an exception otherwise.
|
protected <T1,C1 extends ObjectBaseCheck<T1,C1>> |
objectPropertyCheck(Class<C1> checkClass,
T1 property,
String propertyName)
Creates an object property check for this argument.
|
checkConversion, me, named, not
protected final T arg
protected boolean nullAllowed
null
is a valid value for the
argument of this check. It is initialized to false
and may
be set using the isNullOr()
modifier.
All checks must respect this flag. If the
check(boolean, MessageType, Object...)
helper method is used,
the condition passed to it must always start with "
arg == null ||
" to not cause a NullPointerException
.
protected ObjectBaseCheck(T arg)
public final C isNullOr()
null
. This method modifies all
subsequent checks of this check object to not perform the implicit not-
null
check that is done by default. It should be called
before any of the check methods because the checks are performed
immediately when a check method is called.public final C isNotNull()
null
, throwing an exception
otherwise. This check is sensitive to both the isNullOr()
and
the BaseCheck.not()
modifier. If the former is called before this one,
this check is redundant. The later however, changes this check to
only accept null
as a value.
The message type used for exceptions thrown by this method is
MessageType.ARG_NULL
.
IllegalArgumentException
- if the argument is null
public final C isSameAs(Object object)
BaseCheck.not()
.
The message type used for exceptions thrown by this method is
MessageType.ARG_SAME_AS
.
IllegalArgumentException
- if the argument is not the same as the given objectpublic final C isEqualTo(Object object)
The message type used for exceptions thrown by this method is
MessageType.ARG_EQUAL_TO
.
IllegalArgumentException
- if the argument is not equal to the given object@GwtIncompatible(value="Class.isAssignableFrom") public final C isInstanceOf(Class<?> type)
hasClass(Class)
method.
The message type used for exceptions thrown by this method is
MessageType.ARG_INSTANCE_OF
.
type
- The type the argument should be an instance ofIllegalArgumentException
- if the argument is not an instance of the given typepublic final C hasClass(Class<?> clazz)
isInstanceOf(Class)
check.
The message type used for exceptions thrown by this method is
MessageType.ARG_CLASS
.
clazz
- The class the argument should haveIllegalArgumentException
- if the argument does not have the given classprotected final C check(boolean condition, MessageType msgType, Object... msgArgs)
isNullOr
-aware)
null
-check
true
or
false
respectively, depending on the
inverted
flag. If the check fails, an
IllegalArgumentException
is thrown, with a message formatted
using the format belonging to the given message type and the given
message arguments.
inverted
flag.
condition
- The check conditionmsgType
- The message type to be used for the exception message thrown
if the check failsmsgArgs
- The arguments for the exception messageprotected final C checkWithCause(boolean condition, MessageType msgType, Throwable cause, Object... msgArgs)
check(boolean, MessageType, Object...)
but with the
possibility to define a cause for the exception that is thrown if the
check fails.condition
- The check conditionmsgType
- The message type to be used for the exception message thrown
if the check failscause
- The cause for the exceptionmsgArgs
- The arguments for the exception messageprotected final void checkNull()
null
check that should be done before
each check. It takes the isNullOr()
modifier into account.
This method can be used by check methods if the check can not be
implemented using a check(boolean, MessageType, Object...)
one-liner.
protected final <T1,C1 extends ObjectBaseCheck<T1,C1>> C1 objectPropertyCheck(Class<C1> checkClass, T1 property, String propertyName)
UrlCheck.hasHostWhich()
. It can be used
to write something like this:
Check.that(url).hasHostWhich().endsWith(".ch");
This method can only be used to create property checks for object
properties (as opposed to primitive properties). The
intPropertyCheck(int, String)
method can be used to create
checks for int
checks. Other primitive types are not
supported (or needed) yet.
checkClass
- The class of the property checkproperty
- The property to be checkedpropertyName
- The name of the property. This is used to provide meaningful
exception messages for property checksprotected final IntCheck intPropertyCheck(int property, String propertyName)
int
property check for this argument. An example
of an int
property check is
StringCheck.hasLengthWhich()
. It can be used to write something
like this:
Check.that(name).hasLengthWhich().isGreaterThan(10);
property
- The int
property to be checkedpropertyName
- The name of the property. This is used to provide meaningful
exception messages for property checksCopyright © 2012–2017 Michael Faes. All rights reserved.