com.iparelan.util.validation
Class RangeValidationStrategy<T extends Comparable<? super T>>

java.lang.Object
  extended by com.iparelan.util.UtilObject
      extended by com.iparelan.util.validation.AbstractValidationStrategy<T>
          extended by com.iparelan.util.validation.RangeValidationStrategy<T>
Type Parameters:
T - The Comparable type.
All Implemented Interfaces:
ValidationStrategy<T>

@Immutable
@Copyright(value="Copyright © 2008, Iparelan Solutions, LLC. All rights reserved.")
public class RangeValidationStrategy<T extends Comparable<? super T>>
extends AbstractValidationStrategy<T>

A validator that ensures that a value of a total ordering is within some range of values.

Version:
July 2008
Author:
Greg Mattes
See Also:
"The Java Programming Language," 4th ed., Arnold et. al., pp. 258-9 for an explanation of the generic type syntax wizardry used here.

Nested Class Summary
protected static class RangeValidationStrategy.ContextMinMaxIndexOffset
          Offsets from the context position where the minimum is specified.
 
Nested classes/interfaces inherited from class com.iparelan.util.validation.AbstractValidationStrategy
AbstractValidationStrategy.ContextIndex
 
Field Summary
private  T maximum
          The stateful maximum range limit.
private  T minimum
          The stateful minimum range limit.
 
Fields inherited from class com.iparelan.util.validation.AbstractValidationStrategy
valueDescription
 
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
RangeValidationStrategy()
          Creates a RangeValidationStrategy without a stateful value description or stateful range limits.
RangeValidationStrategy(String valueDescription)
          Creates a RangeValidationStrategy with a stateful value description but without stateful range limits.
RangeValidationStrategy(String valueDescription, T minimum, T maximum)
          Creates a RangeValidationStrategy with a stateful value description and stateful range limits.
RangeValidationStrategy(T minimum, T maximum)
          Creates a RangeValidationStrategy without a stateful value description but with stateful range limits.
 
Method Summary
private  T getEffectiveValue(RangeValidationStrategy.ContextMinMaxIndexOffset index, T statefulValue, Object... context)
          Produces the effective value of the selected range limit.
 T getMaximum()
          Produces the stateful maximum range limit which may be null if the validation was not created with stateful range limits.
private  T getMaximum(Object... context)
          Produces the effective range maximum.
 T getMinimum()
          Produces the stateful minimum range limit which may be null if the validation was not created with stateful range limits.
private  T getMinimum(Object... context)
          Produces the effective range minimum.
 String getValidatorDescription()
          Produces the description of this ValidationStrategy.
 void validate(T unvalidated, Object... context)
          Validates the specified value.
protected  void validateRangeContext(Object... context)
          Ensures that the validation-time context is valid as described by the context parameter of validate(T, Object...).
 
Methods inherited from class com.iparelan.util.validation.AbstractValidationStrategy
getValueDescription, getValueDescription, toString, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

minimum

@Nullable
private final T extends Comparable<? super T> minimum
The stateful minimum range limit.


maximum

@Nullable
private final T extends Comparable<? super T> maximum
The stateful maximum range limit.

Constructor Detail

RangeValidationStrategy

RangeValidationStrategy(T minimum,
                        T maximum)
Creates a RangeValidationStrategy without a stateful value description but with stateful range limits.

Parameters:
minimum - The minimum range value. May not be null.
maximum - The maximum range value. May not be null.
Throws:
IllegalArgumentException - Iff either minimum or maximum are null.

RangeValidationStrategy

RangeValidationStrategy()
Creates a RangeValidationStrategy without a stateful value description or stateful range limits.


RangeValidationStrategy

public RangeValidationStrategy(String valueDescription)
Creates a RangeValidationStrategy with a stateful value description but without stateful range limits.

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

RangeValidationStrategy

public RangeValidationStrategy(String valueDescription,
                               T minimum,
                               T maximum)
Creates a RangeValidationStrategy with a stateful value description and stateful range limits.

Parameters:
valueDescription - The value description of this ValidationStrategy. May not be null.
minimum - The minimum range value. May not be null.
maximum - The maximum range value. May not be null.
Throws:
IllegalArgumentException - Iff either minimum or maximum are null.
Method Detail

validate

public void validate(T unvalidated,
                     Object... context)
Validates the specified value.

Parameters:
unvalidated - The value to be validated. May not be null.
context - The context must have one of the following organizations or unvalidated cannot be validated:
  1. [value-description : String, minimum : T, maximum : T]: Validation may procede regardless of whether the validator was created with stateful range limit values, however stateful range limit values take precedence over contextual range limit values.
  2. [minimum : T, maximum : T]: Validation may procede regardless of whether the validator was created with stateful range limit values, however stateful range limit values take precedence over contextual range limit values.
  3. [value-description : String]: The validator must have been created with stateful range limit values or unvalidated cannot be validated.
  4. []: The validator must have been created with stateful range limit values or unvalidated cannot be validated.
  5. null: The validator must have been created with stateful range limit values or unvalidated cannot be validated.
Throws:
ValidationException - Iff unvalidated is invalid or cannot be validated because context is insufficient.

getValidatorDescription

public String getValidatorDescription()
Description copied from class: AbstractValidationStrategy
Produces the description of this ValidationStrategy.

Specified by:
getValidatorDescription in class AbstractValidationStrategy<T extends Comparable<? super T>>
Returns:
The description of this ValidationStrategy. Never null.

getMinimum

public T getMinimum()
Produces the stateful minimum range limit which may be null if the validation was not created with stateful range limits.

Returns:
The stateful minimum range limit. May be null.

getMaximum

public T getMaximum()
Produces the stateful maximum range limit which may be null if the validation was not created with stateful range limits.

Returns:
The stateful maximum range limit. May be null.

getMinimum

private T getMinimum(Object... context)
Produces the effective range minimum. The range minimum is either the stateful minimum range limit set at the creation time of this validator or the contextual range minimum provided at validation time. The stateful minimum range limit takes precedence.

Parameters:
context - As described by validate(T, Object...).
Returns:
The effective range minimum. Never null.
Throws:
ValidationException - Iff the effective range minmum cannot be computed because no stateful range limit minmum is set and the context does not contain a validation-time range limit minmum.

getMaximum

private T getMaximum(Object... context)
Produces the effective range maximum. The range maximum is either the stateful maximum range limit set at the creation time of this validator or the contextual range maximum provided at validation time. The stateful maximum range limit takes precedence.

Parameters:
context - As described by validate(T, Object...).
Returns:
The effective range maximum. Never null.
Throws:
ValidationException - Iff the effective range maxmum cannot be computed because no stateful range limit maxmum is set and the context does not contain a validation-time range limit maxmum.

getEffectiveValue

private T getEffectiveValue(RangeValidationStrategy.ContextMinMaxIndexOffset index,
                            @Nullable
                            T statefulValue,
                            Object... context)
Produces the effective value of the selected range limit.

Parameters:
index - Selects the effective range limit value to produce. May not be null.
statefulValue - The stateful range limit value, or null if not is set.
context - The validation-time context that may contain contextual range limits. May be null if this validator has stateful range limits.
Returns:
The effective value for the range limit specified by index. Never null.
Throws:
ValidationException - Iff the context contains range limit values that are not of type T, or if the context does not contain any range limit values and no stateful range limit values are set..

validateRangeContext

protected void validateRangeContext(@Nullable
                                    Object... context)
Ensures that the validation-time context is valid as described by the context parameter of validate(T, Object...). The type of contextual minimum and maximum range limits is not validated here, see getEffectiveValue(ContextMinMaxIndexOffset, T, Object...). Also, whether stateful minimum and maximum range limits are available in the absence on contextual limits is not validated here.

Parameters:
context - The validation-time context to check for validity. See the context parameter of validate(T, Object...) for valid context organizations. May not be null.
Throws:
ValidationException - Iff the context does not have a proper length or if the context length indicates that the first element should be a value description but the first element is not a String.


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