com.guiseframework.validator
Class AbstractValidator<V>

java.lang.Object
  extended by com.globalmentor.beans.BoundPropertyObject
      extended by com.guiseframework.event.GuiseBoundPropertyObject
          extended by com.guiseframework.validator.AbstractValidator<V>
Type Parameters:
V - The value type this validator supports.
All Implemented Interfaces:
com.globalmentor.beans.PropertyBindable, com.globalmentor.beans.PropertyConstrainable, Validator<V>
Direct Known Subclasses:
AbstractRangeValidator, AbstractRegularExpressionValidator, PANValidator, ResourceImportValidator, TextSearchPanel.PatternSyntaxValidator, ValueRequiredValidator

public abstract class AbstractValidator<V>
extends GuiseBoundPropertyObject
implements Validator<V>

An abstract implementation of an object that can determine whether a value is valid.

Author:
Garret Wilson

Field Summary
 
Fields inherited from class com.globalmentor.beans.BoundPropertyObject
NO_PROPERTY_CHANGE_LISTENERS, NO_VETOABLE_CHANGE_LISTENERS
 
Fields inherited from interface com.guiseframework.validator.Validator
INVALID_VALUE_MESSAGE_PROPERTY, VALUE_REQUIRED_MESSAGE_PROPERTY, VALUE_REQUIRED_PROPERTY
 
Constructor Summary
AbstractValidator()
          Default constructor with no value required.
AbstractValidator(boolean valueRequired)
          Value required constructor.
 
Method Summary
 java.lang.String getInvalidValueMessage()
           
 java.lang.String getValueRequiredMessage()
           
 boolean isValid(V value)
          Determines whether a given value is valid.
 boolean isValueRequired()
           
 void setInvalidValueMessage(java.lang.String newInvalidValueMessage)
          Sets the text of the invalid value message.
 void setValueRequired(boolean newValueRequired)
          Sets whether the value must be non-null in order to be considered valid.
 void setValueRequiredMessage(java.lang.String newValueRequiredMessage)
          Sets the text of the value required message.
 void throwInvalidValueValidationException(V value)
          Throws a validation exception with a message indicating that the given value is invalid.
 void throwValueRequiredValidationException(V value)
          Throws a validation exception with a message indicating that a valid is required.
protected  java.lang.String toString(V value)
          Retrieves a string representation of the given value appropriate for error messages.
 void validate(V value)
          Checks whether a given value is valid, and throws an exception if not.
 
Methods inherited from class com.guiseframework.event.GuiseBoundPropertyObject
getSession
 
Methods inherited from class com.globalmentor.beans.BoundPropertyObject
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, createPostponedPropertyChangeEvent, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getForwardPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getRepeatPropertyChangeListener, getRepeatVetoableChangeListener, getVetoableChangeListeners, getVetoableChangeListeners, getVetoableChangeSupport, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.guiseframework.validator.Validator
getSession
 
Methods inherited from interface com.globalmentor.beans.PropertyBindable
addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, hasPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
 

Constructor Detail

AbstractValidator

public AbstractValidator()
Default constructor with no value required.


AbstractValidator

public AbstractValidator(boolean valueRequired)
Value required constructor.

Parameters:
valueRequired - Whether the value must be non-null in order to be considered valid.
Method Detail

isValueRequired

public boolean isValueRequired()
Returns:
Whether the value must be non-null in order to be considered valid.

setValueRequired

public void setValueRequired(boolean newValueRequired)
Sets whether the value must be non-null in order to be considered valid. This is a bound property of type Boolean.

Parameters:
newValueRequired - true if the value must be non-null in order to be considered valid.
See Also:
Validator.VALUE_REQUIRED_PROPERTY

getInvalidValueMessage

public java.lang.String getInvalidValueMessage()
Specified by:
getInvalidValueMessage in interface Validator<V>
Returns:
The invalid value message text, which may include a resource reference.

setInvalidValueMessage

public void setInvalidValueMessage(java.lang.String newInvalidValueMessage)
Sets the text of the invalid value message. This is a bound property.

Specified by:
setInvalidValueMessage in interface Validator<V>
Parameters:
newInvalidValueMessage - The new text of the invalid value message, which may include a resource reference.
Throws:
java.lang.NullPointerException - if the given message is null.
See Also:
Validator.INVALID_VALUE_MESSAGE_PROPERTY

getValueRequiredMessage

public java.lang.String getValueRequiredMessage()
Specified by:
getValueRequiredMessage in interface Validator<V>
Returns:
The value required message text, which may include a resource reference.

setValueRequiredMessage

public void setValueRequiredMessage(java.lang.String newValueRequiredMessage)
Sets the text of the value required message. This is a bound property.

Specified by:
setValueRequiredMessage in interface Validator<V>
Parameters:
newValueRequiredMessage - The new text of the value required message, which may include a resource reference.
Throws:
java.lang.NullPointerException - if the given message is null.
See Also:
#VALUE_REQUIRED_VALUE_MESSAGE_PROPERTY

throwInvalidValueValidationException

public void throwInvalidValueValidationException(V value)
                                          throws ValidationException
Throws a validation exception with a message indicating that the given value is invalid.

Parameters:
value - The value being validated.
Throws:
ValidationException - to indicate that the given value is invalid.
See Also:
getInvalidValueMessage()

throwValueRequiredValidationException

public void throwValueRequiredValidationException(V value)
                                           throws ValidationException
Throws a validation exception with a message indicating that a valid is required.

Parameters:
value - The value being validated.
Throws:
ValidationException - to indicate that a value is required.
See Also:
getValueRequiredMessage()

validate

public void validate(V value)
              throws ValidationException
Checks whether a given value is valid, and throws an exception if not.

The message of the thrown exception should be appropriate for display to the user, although it may include string resource references. If a child class has no specific message to return, that class may call throwInvalidValueValidationException(Object) as a convenience. A child class may also call throwValueRequiredValidationException(Object) as a convenience, but this is usually not required if this version of the method, which provides a missing value check, is called first.

This version checks whether a value is provided if values are required. Child classes should call this version as a convenience for checking non-null and required status.

Adding new validation logic always requires overriding this method. Although isValid(Object) may be overridden to provide optimized fast-fail determinations, adding new logic to isValid(Object) cannot be used in place of overriding this method.

Specified by:
validate in interface Validator<V>
Parameters:
value - The value to validate, which may be null.
Throws:
ValidationException - if the provided value is not valid.
See Also:
throwInvalidValueValidationException(Object), throwValueRequiredValidationException(Object)

isValid

public boolean isValid(V value)
Determines whether a given value is valid. This convenience version calls validate(Object), returning false only if an exception is thrown. Although this method may be overridden to provide optimized fast-fail determinations, adding new logic to this method cannot be used in place of overriding validate(Object).

Specified by:
isValid in interface Validator<V>
Parameters:
value - The value to validate.
Returns:
true if a value is given and the value is valid; or a value is not required, else false.

toString

protected java.lang.String toString(V value)
Retrieves a string representation of the given value appropriate for error messages. This implementation returns the Object.toString() string representation of the value.

Parameters:
value - The value for which a string representation should be returned.
Returns:
A string representation of the given value.


Copyright © 2005-2010 GlobalMentor, Inc. All Rights Reserved.