Class AbstractValidator<V>
- java.lang.Object
-
- com.globalmentor.beans.BoundPropertyObject
-
- io.guise.framework.event.GuiseBoundPropertyObject
-
- io.guise.framework.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
,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 io.guise.framework.validator.Validator
INVALID_VALUE_MESSAGE_PROPERTY, VALUE_REQUIRED_MESSAGE_PROPERTY, VALUE_REQUIRED_PROPERTY
-
-
Constructor Summary
Constructors Constructor Description AbstractValidator()
Default constructor with no value required.AbstractValidator(boolean valueRequired)
Value required constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 io.guise.framework.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.globalmentor.beans.PropertyBindable
addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, hasPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
Methods inherited from interface io.guise.framework.validator.Validator
getSession
-
-
-
-
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 typeBoolean
.- 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 interfaceValidator<V>
- Returns:
- The invalid value message text, which may include a resource reference.
-
setInvalidValueMessage
public void setInvalidValueMessage(java.lang.String newInvalidValueMessage)
Description copied from interface:Validator
Sets the text of the invalid value message. This is a bound property.- Specified by:
setInvalidValueMessage
in interfaceValidator<V>
- Parameters:
newInvalidValueMessage
- The new text of the invalid value message, which may include a resource reference.- See Also:
Validator.INVALID_VALUE_MESSAGE_PROPERTY
-
getValueRequiredMessage
public java.lang.String getValueRequiredMessage()
- Specified by:
getValueRequiredMessage
in interfaceValidator<V>
- Returns:
- The value required message text, which may include a resource reference.
-
setValueRequiredMessage
public void setValueRequiredMessage(java.lang.String newValueRequiredMessage)
Description copied from interface:Validator
Sets the text of the value required message. This is a bound property.- Specified by:
setValueRequiredMessage
in interfaceValidator<V>
- Parameters:
newValueRequiredMessage
- The new text of the value required message, which may include a resource reference..- See Also:
Validator.VALUE_REQUIRED_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 callthrowValueRequiredValidationException(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
Validator.isValid(Object)
may be overridden to provide optimized fast-fail determinations, adding new logic toValidator.isValid(Object)
cannot be used in place of overriding this method.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 toisValid(Object)
cannot be used in place of overriding this method.- Specified by:
validate
in interfaceValidator<V>
- Parameters:
value
- The value to validate, which may benull
.- 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 callsValidator.validate(Object)
, returningfalse
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 overridingValidator.validate(Object)
.This convenience version calls
validate(Object)
, returningfalse
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 overridingvalidate(Object)
.
-
toString
protected java.lang.String toString(V value)
Retrieves a string representation of the given value appropriate for error messages. This implementation returns theObject.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.
-
-