com.iparelan.util.validation
Class AbstractValidationStrategy<T>

java.lang.Object
  extended by com.iparelan.util.UtilObject
      extended by com.iparelan.util.validation.AbstractValidationStrategy<T>
All Implemented Interfaces:
ValidationStrategy<T>
Direct Known Subclasses:
NonEmptyStringValidationStrategy, NonNullClassValidationStrategy, NonNullNonEmptyStringValidationStrategy, NonNullStringValidationStrategy, NonNullValidationStrategy, NoValidationStrategy, RangeValidationStrategy

@Immutable
@Copyright(value="Copyright © 2008, Iparelan Solutions, LLC. All rights reserved.")
public abstract class AbstractValidationStrategy<T>
extends UtilObject
implements ValidationStrategy<T>

Provides logic common to all validation strategies.

Version:
July 2008
Author:
Greg Mattes

Nested Class Summary
protected static class AbstractValidationStrategy.ContextIndex
          The default position of the value description in the validation context.
 
Field Summary
protected  String valueDescription
          The value description of this validation strategy.
 
Fields inherited from class com.iparelan.util.UtilObject
LOGGER, MSGS, UTILITIES_LOGGER_NAME
 
Fields inherited from interface com.iparelan.util.validation.ValidationStrategy
BIG_DECIMAL_RANGE, BIG_INTEGER_RANGE, BYTE_RANGE, CALENDAR_RANGE, CHARACTER_RANGE, DATE_RANGE, DOUBLE_RANGE, FILE, FLOAT_RANGE, GREGORIAN_CALENDAR_RANGE, INTEGER, INTEGER_RANGE, LONG_RANGE, NON_EMPTY_STRING, NON_NULL_CLASS, NON_NULL_NON_EMPTY_STRING, NON_NULL_OBJECT, NON_NULL_STRING, SHORT_RANGE, SQL_DATE_RANGE, SQL_TIME_RANGE, SQL_TIMESTAMP_RANGE
 
Constructor Summary
(package private) AbstractValidationStrategy()
          Creates an AbstractValidationStrategy.
protected AbstractValidationStrategy(String valueDescription)
          Creates an AbstractValidationStrategy.
 
Method Summary
abstract  String getValidatorDescription()
          Produces the description of this ValidationStrategy.
 String getValueDescription()
          Produces the description of the values that are validated.
protected  String getValueDescription(Object... context)
          A convenience routine that fetches the creation-time (constructor argument) value description if one was provided, otherwise fetches a value description from the provided context, if the context contains a value description, otherwise produces the empty string.
private  Object[] mergeContext(String valueDescription, Object... context)
          Produces a new context by prepending valueDescription to context.
 String toString()
           
protected  void validate(T unvalidated, String valueDescription, Object... context)
          Invokes ValidationStrategy.validate(T, Object...) after prepending valueDescription to context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.iparelan.util.validation.ValidationStrategy
validate
 

Field Detail

valueDescription

protected final String valueDescription
The value description of this validation strategy.

Constructor Detail

AbstractValidationStrategy

AbstractValidationStrategy()
Creates an AbstractValidationStrategy. Only members of this package may create a ValidationStrategy without an explicit value description. This is allows self-explanatory, general-purpose validators to be created by component implementers, but requires application programmers to provide a value description.


AbstractValidationStrategy

protected AbstractValidationStrategy(String valueDescription)
Creates an AbstractValidationStrategy.

Parameters:
valueDescription - The value description of this ValidationStrategy. May not be null or the empty string.
Throws:
IllegalArgumentException - Iff valueDescription is either null or the empty string.
Method Detail

validate

protected final void validate(T unvalidated,
                              String valueDescription,
                              Object... context)
Invokes ValidationStrategy.validate(T, Object...) after prepending valueDescription to context. This operation is useful in the validate method of validator subtypes that are implemented by composing other validators. The composed validators are given access to the value description by invoking this form of validate.

Parameters:
unvalidated - See ValidationStrategy.validate(T, Object...).
valueDescription - A description of the value being validated that is prepended to context. May not be null or the empty string.
context - See ValidationStrategy.validate(T, Object...).
Throws:
ValidationException - See ValidationStrategy.validate(T, Object...).

getValueDescription

public final String getValueDescription()
Produces the description of the values that are validated. This description is intended to be used when reporting validation errors with messages like, "The value <value-description> is invalid."

Returns:
The description of the values being validated. Never null.

getValidatorDescription

public abstract String getValidatorDescription()
Produces the description of this ValidationStrategy.

Returns:
The description of this ValidationStrategy. Never null.

toString

public final String toString()
Overrides:
toString in class Object

getValueDescription

protected final String getValueDescription(Object... context)
A convenience routine that fetches the creation-time (constructor argument) value description if one was provided, otherwise fetches a value description from the provided context, if the context contains a value description, otherwise produces the empty string.

Parameters:
context - Any information required for, or related to, the validation of unvalidated. The exact semantics of this parameter are defined by the override of this method. May be omitted or null.
Returns:
The value description of the validator. Never null, but may be the empty string.

mergeContext

private Object[] mergeContext(String valueDescription,
                              Object... context)
Produces a new context by prepending valueDescription to context.

Parameters:
valueDescription - The value description to prepend to context. May not be null or the empty string.
context - The context to which valueDescription is prepended. May not be null.
Returns:
A new context with valueDescription prepended to context. Never null.
Throws:
IllegalArgumentException - Iff valueDescription is either null or the empty string.


Copyright © 2008 Iparelan Solutions, LLC. All rights reserved.