org.dellroad.stuff.validation
Class ValidationContext<T>

java.lang.Object
  extended by org.dellroad.stuff.validation.ValidationContext<T>

public class ValidationContext<T>
extends Object

Provides additional context for ConstraintValidator implementations.

ValidationContext gives ConstraintValidator implementations access to the root object being validated. This breaks the usual principle of locality for validation (i.e., that validation of a specific bean proceeds unaware of that bean's parents) but it can make custom validators more convenient to implement. Subclasses are encouraged to provide additional application-specific information.

Validation must be performed via validate() for this class to work.


Constructor Summary
ValidationContext(T root)
          Construct a new validation context configured to validate the given root object.
 
Method Summary
static
<T extends ValidationContext<?>>
T
getCurrentContext(Class<T> type)
          Get the ValidationContext associated with the current thread, cast to the desired type.
static Object getCurrentRoot()
          Convenience method to get the root object being validated by the current thread.
 T getRoot()
          Get the root object associated with this instance.
 Set<ConstraintViolation<T>> validate()
          Validate this instance's root object.
 Set<ConstraintViolation<T>> validate(Validator validator)
          Validate this instance's root object using the given Validator, making the context available to the current thread during the validation process via getCurrentContext(java.lang.Class).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValidationContext

public ValidationContext(T root)
Construct a new validation context configured to validate the given root object.

Parameters:
root - root object to be validated
Throws:
IllegalArgumentException - if root is null
Method Detail

getRoot

public final T getRoot()
Get the root object associated with this instance.


validate

public Set<ConstraintViolation<T>> validate()
Validate this instance's root object. This is a convenience method, equivalent to:
validate(Validation.buildDefaultValidatorFactory().getValidator())

Throws:
IllegalStateException - if this method is invoked re-entrantly

validate

public Set<ConstraintViolation<T>> validate(Validator validator)
Validate this instance's root object using the given Validator, making the context available to the current thread during the validation process via getCurrentContext(java.lang.Class).

Throws:
IllegalStateException - if this method is invoked re-entrantly

getCurrentContext

public static <T extends ValidationContext<?>> T getCurrentContext(Class<T> type)
Get the ValidationContext associated with the current thread, cast to the desired type. This method is only valid during invocations of validate().

Parameters:
type - required type
Returns:
current ValidationContext
Throws:
IllegalStateException - if validate() is not currently executing
ClassCastException - if the current ValidationContext is not of type type

getCurrentRoot

public static Object getCurrentRoot()
Convenience method to get the root object being validated by the current thread. This method is only valid during invocations of validate(). Subclasses may want to override to narrow the return type.

Returns:
current validation root object
Throws:
IllegalStateException - if validate() is not currently executing