net.protempore.utils
Class Validator

java.lang.Object
  extended by net.protempore.utils.Validator

public class Validator
extends java.lang.Object

A simple validator intended to be a standalone validation class that takes advantage of JDK5+ features that ease common validation tasks (like error messages with arguments that don't get formatted into the message if the assertion doesn't fail), as well as being more configurable in terms of reacting to failed assertions (at present, we can throw a user-configuration runtime exception and/or log using built-in Java logging).

This is motivated by 2 primary issues that frustrate me when using the otherwise invaluable commons-lang Validate class: 1) if you provide detailed error messages, you're almost certainly building some big and complicated strings, and the performance hit is often so great (if you validate a lot like you should) that you're forced to put lots of guards in to only build the big strings in case of actual failure); 2) IllegalArgumentException.class is the only exception that commons-lang can throw in case of a failure, so you can't use it without either forcing your clients to catch IllegalArgumentException as well, or catching it yourself and rethrowing the exception you'd like to have thrown as a wrapper around the commons-lang exception.

These annoyances, at least, are fixed by: 1) the validation methods in this class accept message arguments, and the message arguments are only interpolated into the error message if there is actually a failure; 2) a user-defined runtime exception (configurable per assertion if desired) will be thrown instead of the default ValidationException if the appropriate method is called.

Here's an example that illustrates both points:

Validator val = Validator.validate(UninitializedException.class);

val.isTrue(isInitialized(), "'%s'[id='%s'] must be initialized first.", getClass().getname(), getId());

This would result in an UnitializedException if isInitialized() returns false, and the error message for the exception would be java.lang.String.format("'%s'[id='%s'] must be initialized first.", getClass().getname(), getId()), which would only be invoked if there is a failure. (Unfortunately, the argument expressions still get evaluated before being passed, but there is no way around that without something like Lisp-style macros or more clutter.)

The above example looks more complex than commons-lang Validate, but the simple case, via static import, would just be:

validate().isTrue(age >= requiredAge, "age '%s' arg must be at least '%s'.", age, requiredAge);

A couple more examples:

validate(IllegalStateException.class).isTrue(isInitialized()); validate(IllegalArgumentException.class).notNull(intObj).isTrue(intObj >= 21);

This class also has additional validation methods that are useful, such as the same and notSame that JUnit has, as well as some others for which I have had use.

Additionally, validation errors can automatically be logged using one of the validate methods that takes a log argument. We use commons-logging for the logger, so you can use any logging library that commons-logging supports (log4j, java.util.logging, and many more).

TODO: look into overloaded methods for primitive numbers where it makes sense. Autoboxing handles primitives for now, but the performance hit may be too great to use this is a permanent solution. On the other hand, how many milliseconds does the average app spend validating primitive numbers anyway?


Nested Class Summary
static class Validator.ValidatorException
          Exception only thrown in case of validator not behaving the way it is intended to behave -- i.e., this never indicates a failure on the part of the client.
 
Constructor Summary
protected Validator()
          Construct a validator that throws ValidationException in case of failure and does not log failures.
protected Validator(java.lang.Class<? extends java.lang.RuntimeException> clazz)
          Construct a validator that throws exceptions of the given class in case of failure and does not log failures.
protected Validator(java.lang.Class<? extends java.lang.RuntimeException> clazz, org.apache.commons.logging.Log logger)
          Construct a validator that logs failures to the given logger at the given log level and then throws an exception that is an instance of the given class.
protected Validator(org.apache.commons.logging.Log logger)
          Construct a validator that logs failures to the supplied logger (at predefined level SEVERE) and does not throw any exception.
 
Method Summary
 Validator allElementsOfType(java.util.Collection<?> collection, java.lang.Class<?> cls)
           Verify that the collection reference is non-null and that each element, if any, of the collection is an instance of the given class, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.
 Validator allElementsOfType(java.util.Collection<?> collection, java.lang.Class<?> cls, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that the collection reference is non-null and that each element, if any, of the collection is an instance of the given class, as defined by Class.isInstance(Object).
 Validator allElementsOfType(java.util.Map<?,?> map, java.lang.Class<?> keyClass, java.lang.Class<?> valueClass)
           Verify that the map reference is non-null and that each key in an instance of keyClass and each value is an instance of valueClass, as defined by Class.isInstance(Object).
 Validator allElementsOfType(java.util.Map<?,?> map, java.lang.Class<?> keyClass, java.lang.Class<?> valueClass, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that the map reference is non-null and that each key in an instance of keyClass and each value is an instance of valueClass, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.
 Validator allElementsOfType(java.lang.Object[] array, java.lang.Class<?> cls)
           Verify that the array reference is non-null and that each element, if any, of the array is an instance of the given class, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.
 Validator allElementsOfType(java.lang.Object[] array, java.lang.Class<?> cls, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that the array reference is non-null and that each element, if any, of the array is an instance of the given class, as defined by Class.isInstance(Object).
 Validator equal(java.lang.Object expected, java.lang.Object actual)
           Verify that expected is equal (via .equals) to actual, considering 2 null references to be equal.
 Validator equal(java.lang.Object expected, java.lang.Object actual, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that expected is equal (via .equals) to actual, considering 2 null references to be equal, using the given error message and message arguments in case of failure.
 void fail()
          Fail, logging the failure and/or throwing an exception depending on configuration.
static void fail(java.lang.Class<? extends java.lang.RuntimeException> cls, org.apache.commons.logging.Log logger, java.lang.String msg, java.lang.Object... msgArgs)
          A static convenience method that fails, logging a message to the supplied logger (as an error) and throwing an exception of type cls, using the given error message and message arguments.
static void fail(java.lang.Class<? extends java.lang.RuntimeException> cls, java.lang.String msg, java.lang.Object... msgArgs)
          A static convenience method that fails and throws an exception of type cls, using the given error message and message arguments.
static void fail(org.apache.commons.logging.Log logger, java.lang.String msg, java.lang.Object... msgArgs)
          A static convenience method that fails, logging a message to the supplied logger (as an error), using the given error message and message arguments.
 void fail(java.lang.String msg, java.lang.Object... msgArgs)
          Fail, using the given error message and message arguments, logging the message and/or throwing an exception depending on configuration.
 java.lang.Class<? extends java.lang.RuntimeException> getFailureClass()
          Get the class of exception this validator throws in case of failure, or null if none was defined.
 Validator isFalse(boolean condition)
          Verify that the condition is false.
 Validator isFalse(boolean condition, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the condition is false, using the given error message and message arguments in case of failure.
 Validator isNull(java.lang.Object obj)
          Verify that the obj reference is null.
 Validator isNull(java.lang.Object obj, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the obj reference is null, using the given error message and message arguments in case of failure.
 Validator isTrue(boolean condition)
          Verify that the condition is true.
 Validator isTrue(boolean condition, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the condition is true, using the given error message and message arguments in case of failure.
 Validator noNullElements(java.util.Collection<?> collection)
          Verify that the collection reference is non-null and contains no null elements.
 Validator noNullElements(java.util.Collection<?> collection, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the collection reference is non-null and contains no null elements, using the given error message and message arguments in case of failure.
 Validator noNullElements(java.lang.Object[] array)
          Verify that the array reference is non-null and contains no null elements.
 Validator noNullElements(java.lang.Object[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and contains no null elements, using the given error message and message arguments in case of failure.
 Validator notEmpty(byte[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(byte[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(java.lang.CharSequence chars)
          Verify that the chars reference is non-null and has a length of at least 1.
 Validator notEmpty(java.lang.CharSequence chars, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the chars reference is non-null and has a length of at least 1, using the given error message and message arguments in case of failure.
 Validator notEmpty(java.util.Collection<?> collection)
          Verify that the collection reference is non-null and has at least 1 element.
 Validator notEmpty(java.util.Collection<?> collection, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the collection reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(double[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(double[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(float[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(float[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(int[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(int[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(long[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(long[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(java.util.Map<?,?> map)
          Verify that the map reference is non-null and has at least 1 key-value pair.
 Validator notEmpty(java.util.Map<?,?> map, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the map reference is non-null and has at least 1 key-value pair, using the given error message and message arguments in case of failure.
 Validator notEmpty(java.lang.Object[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(java.lang.Object[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEmpty(short[] array)
          Verify that the array reference is non-null and has at least 1 element.
 Validator notEmpty(short[] array, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.
 Validator notEqual(java.lang.Object unexpected, java.lang.Object actual)
           Verify that expected is not equal (via .equals) to actual, considering 2 null references to be equal.
 Validator notEqual(java.lang.Object unexpected, java.lang.Object actual, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that expected is not equal (via .equals) to actual, considering 2 null references to be equal, using the given error message and message arguments in case of failure.
 Validator notNull(java.lang.Object obj)
          Verify that the obj reference is null.
 Validator notNull(java.lang.Object obj, java.lang.String msg, java.lang.Object... msgArgs)
          Verify that the obj reference is not null, using the given error message and message arguments in case of failure.
 Validator notSame(java.lang.Object expected, java.lang.Object actual)
           Verify that actual does not refer to the same object as expected.
 Validator notSame(java.lang.Object expected, java.lang.Object actual, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that actual does not refer to the same object as expected, using the given error message and message arguments in case of failure.
 Validator ofType(java.lang.Object object, java.lang.Class<?> type)
           Verify that the object is an instance of type, which may be an interface or a class, as defined by Class.isInstance(Object).
 Validator ofType(java.lang.Object object, java.lang.Class<?> type, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that the object is an instance of type, which may be an interface or a class, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.
 Validator same(java.lang.Object expected, java.lang.Object actual)
           Verify that actual refers to the same object as expected (or both are null references).
 Validator same(java.lang.Object expected, java.lang.Object actual, java.lang.String msg, java.lang.Object... msgArgs)
           Verify that actual refers to the same object as expected (or both are null references), using the given error message and message arguments in case of failure.
static Validator validate()
          Get a validator that throws a ValidationException in case of failure.
static Validator validate(java.lang.Class<? extends java.lang.RuntimeException> clazz)
          Get a validator that throws an exception of type clazz (and does no logging) in case of failure.
static Validator validate(java.lang.Class<? extends java.lang.RuntimeException> clazz, org.apache.commons.logging.Log logger)
          Get a validator that logs a failure to the given logger (as an error)and throws an exception of type clazz in case of failure.
static Validator validate(org.apache.commons.logging.Log logger)
          Get a validator that logs a failure (as error) to the given logger and does not throw an exception in case of failure.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Validator

protected Validator()
Construct a validator that throws ValidationException in case of failure and does not log failures.


Validator

protected Validator(java.lang.Class<? extends java.lang.RuntimeException> clazz)
Construct a validator that throws exceptions of the given class in case of failure and does not log failures.

Parameters:
clazz - A non-null class of a runtime exception.

Validator

protected Validator(org.apache.commons.logging.Log logger)
Construct a validator that logs failures to the supplied logger (at predefined level SEVERE) and does not throw any exception.

Parameters:
logger - A non-null logger.
logLevel - A non-null log level at which to log failures.

Validator

protected Validator(java.lang.Class<? extends java.lang.RuntimeException> clazz,
                    org.apache.commons.logging.Log logger)
Construct a validator that logs failures to the given logger at the given log level and then throws an exception that is an instance of the given class.

Parameters:
clazz - A non-null class of a runtime exception.
logger - A non-null logger.
logLevel - A non-null log level at which to log failures.
Method Detail

getFailureClass

public java.lang.Class<? extends java.lang.RuntimeException> getFailureClass()
Get the class of exception this validator throws in case of failure, or null if none was defined.

Returns:
The class.

notNull

public Validator notNull(java.lang.Object obj)
Verify that the obj reference is null.

Parameters:
obj - An object.
Returns:
This instance.

notNull

public Validator notNull(java.lang.Object obj,
                         java.lang.String msg,
                         java.lang.Object... msgArgs)
Verify that the obj reference is not null, using the given error message and message arguments in case of failure.

Parameters:
obj - An object.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

isNull

public Validator isNull(java.lang.Object obj)
Verify that the obj reference is null.

Parameters:
obj - An object.
Returns:
This instance.

isNull

public Validator isNull(java.lang.Object obj,
                        java.lang.String msg,
                        java.lang.Object... msgArgs)
Verify that the obj reference is null, using the given error message and message arguments in case of failure.

Parameters:
obj - An object.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

isTrue

public Validator isTrue(boolean condition)
Verify that the condition is true.

Parameters:
condition - A boolean representing a condition.
Returns:
This instance.

isTrue

public Validator isTrue(boolean condition,
                        java.lang.String msg,
                        java.lang.Object... msgArgs)
Verify that the condition is true, using the given error message and message arguments in case of failure.

Parameters:
condition - A boolean representing a condition.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

isFalse

public Validator isFalse(boolean condition)
Verify that the condition is false.

Parameters:
condition - A boolean representing a condition.
Returns:
This instance.

isFalse

public Validator isFalse(boolean condition,
                         java.lang.String msg,
                         java.lang.Object... msgArgs)
Verify that the condition is false, using the given error message and message arguments in case of failure.

Parameters:
condition - A boolean representing a condition.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

noNullElements

public Validator noNullElements(java.util.Collection<?> collection)
Verify that the collection reference is non-null and contains no null elements.

Parameters:
collection - A collection.
Returns:
This instance.

noNullElements

public Validator noNullElements(java.util.Collection<?> collection,
                                java.lang.String msg,
                                java.lang.Object... msgArgs)
Verify that the collection reference is non-null and contains no null elements, using the given error message and message arguments in case of failure.

Parameters:
collection - A collection.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

noNullElements

public Validator noNullElements(java.lang.Object[] array)
Verify that the array reference is non-null and contains no null elements.

Parameters:
array - An array.
Returns:
This instance.

noNullElements

public Validator noNullElements(java.lang.Object[] array,
                                java.lang.String msg,
                                java.lang.Object... msgArgs)
Verify that the array reference is non-null and contains no null elements, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.util.Collection<?> collection)
Verify that the collection reference is non-null and has at least 1 element.

Parameters:
collection - A collection.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.util.Collection<?> collection,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the collection reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
collection - A collection.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.util.Map<?,?> map)
Verify that the map reference is non-null and has at least 1 key-value pair.

Parameters:
map - A map.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.util.Map<?,?> map,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the map reference is non-null and has at least 1 key-value pair, using the given error message and message arguments in case of failure.

Parameters:
map - A map.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.lang.Object[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.lang.Object[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(byte[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(byte[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(short[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(short[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(int[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(int[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(long[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(long[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(float[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(float[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(double[] array)
Verify that the array reference is non-null and has at least 1 element.

Parameters:
array - An array.
Returns:
This instance.

notEmpty

public Validator notEmpty(double[] array,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the array reference is non-null and has at least 1 element, using the given error message and message arguments in case of failure.

Parameters:
array - An array.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.lang.CharSequence chars)
Verify that the chars reference is non-null and has a length of at least 1.

Parameters:
chars - A character sequence object.
Returns:
This instance.

notEmpty

public Validator notEmpty(java.lang.CharSequence chars,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)
Verify that the chars reference is non-null and has a length of at least 1, using the given error message and message arguments in case of failure.

Parameters:
chars - A character sequence object.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

allElementsOfType

public Validator allElementsOfType(java.util.Collection<?> collection,
                                   java.lang.Class<?> cls)

Verify that the collection reference is non-null and that each element, if any, of the collection is an instance of the given class, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.

An empty but non-null collection succeeds, provided that the cls reference is non-null.

Parameters:
collection - A non-null collection.
cls - A non-null class.
Returns:
This instance.

allElementsOfType

public Validator allElementsOfType(java.util.Collection<?> collection,
                                   java.lang.Class<?> cls,
                                   java.lang.String msg,
                                   java.lang.Object... msgArgs)

Verify that the collection reference is non-null and that each element, if any, of the collection is an instance of the given class, as defined by Class.isInstance(Object).

An empty but non-null collection succeeds, provided that the cls reference is non-null.

Parameters:
collection - A non-null collection.
cls - A non-null class.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

allElementsOfType

public Validator allElementsOfType(java.lang.Object[] array,
                                   java.lang.Class<?> cls)

Verify that the array reference is non-null and that each element, if any, of the array is an instance of the given class, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.

An empty but non-null array succeeds, provided that the cls reference is non-null.

Parameters:
array - A non-null array.
cls - A non-null class.
Returns:
This instance.

allElementsOfType

public Validator allElementsOfType(java.lang.Object[] array,
                                   java.lang.Class<?> cls,
                                   java.lang.String msg,
                                   java.lang.Object... msgArgs)

Verify that the array reference is non-null and that each element, if any, of the array is an instance of the given class, as defined by Class.isInstance(Object).

An empty but non-null array succeeds, provided that the cls reference is non-null.

Parameters:
array - A non-null array.
cls - A non-null class.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

allElementsOfType

public Validator allElementsOfType(java.util.Map<?,?> map,
                                   java.lang.Class<?> keyClass,
                                   java.lang.Class<?> valueClass)

Verify that the map reference is non-null and that each key in an instance of keyClass and each value is an instance of valueClass, as defined by Class.isInstance(Object).

An empty but non-null map succeeds, provided that both class references are non-null.

Parameters:
map - A non-null map.
keyClass - A non-null class for verifying keys.
valueClass - A non-null class for verifying values.
Returns:
This instance.

allElementsOfType

public Validator allElementsOfType(java.util.Map<?,?> map,
                                   java.lang.Class<?> keyClass,
                                   java.lang.Class<?> valueClass,
                                   java.lang.String msg,
                                   java.lang.Object... msgArgs)

Verify that the map reference is non-null and that each key in an instance of keyClass and each value is an instance of valueClass, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.

An empty but non-null map succeeds, provided that both class references are non-null.

Parameters:
map - A non-null map.
keyClass - A non-null class for verifying keys.
valueClass - A non-null class for verifying values.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

ofType

public Validator ofType(java.lang.Object object,
                        java.lang.Class<?> type)

Verify that the object is an instance of type, which may be an interface or a class, as defined by Class.isInstance(Object).

Parameters:
object - An object.
type - A non-null class object representing an interface or class.
Returns:
This instance.

ofType

public Validator ofType(java.lang.Object object,
                        java.lang.Class<?> type,
                        java.lang.String msg,
                        java.lang.Object... msgArgs)

Verify that the object is an instance of type, which may be an interface or a class, as defined by Class.isInstance(Object), using the given error message and message arguments in case of failure.

Parameters:
object - An object.
type - A non-null class object representing an interface or class.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.
Returns:
This instance.

equal

public Validator equal(java.lang.Object expected,
                       java.lang.Object actual)

Verify that expected is equal (via .equals) to actual, considering 2 null references to be equal.

Parameters:
expected - Expected object.
actual - Object to test for equality with expected.
Returns:
This instance.

equal

public Validator equal(java.lang.Object expected,
                       java.lang.Object actual,
                       java.lang.String msg,
                       java.lang.Object... msgArgs)

Verify that expected is equal (via .equals) to actual, considering 2 null references to be equal, using the given error message and message arguments in case of failure.

Parameters:
expected - Expected object.
actual - Object to test for equality with expected.
msg - Error message, non-empty.
msgArgs - Any number of msg args.
Returns:
This instance.

notEqual

public Validator notEqual(java.lang.Object unexpected,
                          java.lang.Object actual)

Verify that expected is not equal (via .equals) to actual, considering 2 null references to be equal.

Parameters:
unexpected - The object to test against.
actual - The object that shouldn't be equal to unexpected object.
Returns:
This instance.

notEqual

public Validator notEqual(java.lang.Object unexpected,
                          java.lang.Object actual,
                          java.lang.String msg,
                          java.lang.Object... msgArgs)

Verify that expected is not equal (via .equals) to actual, considering 2 null references to be equal, using the given error message and message arguments in case of failure.

Parameters:
unexpected - The object to test against.
actual - The object that shouldn't be equal to unexpected object.
msg - Error message, non-empty.
msgArgs - Any number of msg args.
Returns:
This instance.

same

public Validator same(java.lang.Object expected,
                      java.lang.Object actual)

Verify that actual refers to the same object as expected (or both are null references).

Parameters:
expected - Expected object.
actual - Object to test for identity with expected.
Returns:
This instance.

same

public Validator same(java.lang.Object expected,
                      java.lang.Object actual,
                      java.lang.String msg,
                      java.lang.Object... msgArgs)

Verify that actual refers to the same object as expected (or both are null references), using the given error message and message arguments in case of failure.

Parameters:
expected - Expected object.
actual - Object to test for identity with expected.
msg - Error message, non-empty.
msgArgs - Any number of msg args.
Returns:
This instance.

notSame

public Validator notSame(java.lang.Object expected,
                         java.lang.Object actual)

Verify that actual does not refer to the same object as expected.

Parameters:
expected - Expected object.
actual - Object to test for identity with expected.
Returns:
This instance.

notSame

public Validator notSame(java.lang.Object expected,
                         java.lang.Object actual,
                         java.lang.String msg,
                         java.lang.Object... msgArgs)

Verify that actual does not refer to the same object as expected, using the given error message and message arguments in case of failure.

Parameters:
expected - Expected object.
actual - Object to test for identity with expected.
msg - Error message, non-empty.
msgArgs - Any number of msg args.
Returns:
This instance.

fail

public void fail()
Fail, logging the failure and/or throwing an exception depending on configuration.


fail

public void fail(java.lang.String msg,
                 java.lang.Object... msgArgs)
Fail, using the given error message and message arguments, logging the message and/or throwing an exception depending on configuration.

Parameters:
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.

fail

public static void fail(java.lang.Class<? extends java.lang.RuntimeException> cls,
                        java.lang.String msg,
                        java.lang.Object... msgArgs)
A static convenience method that fails and throws an exception of type cls, using the given error message and message arguments.

Parameters:
cls - A non-null runtime exception class.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.

fail

public static void fail(java.lang.Class<? extends java.lang.RuntimeException> cls,
                        org.apache.commons.logging.Log logger,
                        java.lang.String msg,
                        java.lang.Object... msgArgs)
A static convenience method that fails, logging a message to the supplied logger (as an error) and throwing an exception of type cls, using the given error message and message arguments.

Parameters:
cls - A non-null runtime exception class.
logger - A non-null logger.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.

fail

public static void fail(org.apache.commons.logging.Log logger,
                        java.lang.String msg,
                        java.lang.Object... msgArgs)
A static convenience method that fails, logging a message to the supplied logger (as an error), using the given error message and message arguments.

Parameters:
logger - A non-null logger.
msg - A non-null message string.
msgArgs - The arguments to be interpolated into the supplied message.

validate

public static Validator validate()
Get a validator that throws a ValidationException in case of failure.

Returns:
The instance.

validate

public static Validator validate(java.lang.Class<? extends java.lang.RuntimeException> clazz)
Get a validator that throws an exception of type clazz (and does no logging) in case of failure.

Parameters:
clazz - A non-null class.
Returns:
The instance.

validate

public static Validator validate(org.apache.commons.logging.Log logger)
Get a validator that logs a failure (as error) to the given logger and does not throw an exception in case of failure.

Parameters:
logger - A non-null logger.
Returns:
The instance.

validate

public static Validator validate(java.lang.Class<? extends java.lang.RuntimeException> clazz,
                                 org.apache.commons.logging.Log logger)
Get a validator that logs a failure to the given logger (as an error)and throws an exception of type clazz in case of failure.

Parameters:
clazz - A non-null unchecked exception class.
logger - A non-null logger.
Returns:
The instance.