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
    Default constructor with no value required.
    AbstractValidator(boolean valueRequired)
    Value required constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
     
     
    boolean
    isValid(V value)
    Determines whether a given value is valid.
    boolean
     
    void
    setInvalidValueMessage(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(String newValueRequiredMessage)
    Sets the text of the value required message.
    void
    Throws a validation exception with a message indicating that the given value is invalid.
    void
    Throws a validation exception with a message indicating that a valid is required.
    protected 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
  • Constructor Details

    • 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 Details

    • 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:
    • getInvalidValueMessage

      public 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(String newInvalidValueMessage)
      Description copied from interface: Validator
      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.
      See Also:
    • getValueRequiredMessage

      public 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(String newValueRequiredMessage)
      Description copied from interface: Validator
      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..
      See Also:
    • 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:
    • 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:
    • 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 Validator.isValid(Object) may be overridden to provide optimized fast-fail determinations, adding new logic to Validator.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 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:
    • isValid

      public boolean isValid(V value)
      Determines whether a given value is valid. This convenience version calls Validator.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 Validator.validate(Object).

      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 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.