Class Validator

Object
Validator
Direct Known Subclasses:
GeometryValidator, ImageValidator, MetadataValidator, NameValidator, ReferencingValidator

public abstract class Validator extends Object
Base class of all GeoAPI validators. Validators can be configured on a case-by-case basis by changing the values of non-final public fields. If the same configuration needs to be applied on all validators, then ValidatorContainer.all provides a convenient way to make such change in a loop.

Configurations available in this class and some subclasses are:

Once the configuration is finished, all validators provided in GeoAPI are thread-safe provided that their configuration is not modified.

Since:
2.2
  • Field Details

    • DEFAULT_TOLERANCE

      public static final double DEFAULT_TOLERANCE
      The default tolerance value for comparisons of floating point numbers in validators. The current value is 1.0E-6. This value is relatively large because some implementations may store their values as float numbers instead of double.

      Note that TestCase subclasses use smaller tolerance thresholds, typically centimetric. Test cases are stricter then validators because the tests control the objects they create, while validators need to work reasonably well for arbitrary objects.

      See Also:
    • container

      protected final ValidatorContainer container
      The validators to use for every validations not defined in the concrete subclass. For example if CRSValidator needs to validate a datum, it will use the DatumValidator instance defined in this container.

      The container may contain this validator instance. For example if this validator is an instance of CRSValidator, then the ValidatorContainer.crs field may be set to this. Doing so ensure that the proper validate(…) methods will be invoked in case of callback.

      Tip: if the other validators are not expected to callback the validate methods defined in this Validator instance (for example a datum has no reason to validate a CRS), then it is safe to set this field to Validators.DEFAULT.

    • logger

      protected final Logger logger
      The logger for reporting non-fatal warnings. This logger is determined by the package name given at construction time.
    • requireMandatoryAttributes

      public boolean requireMandatoryAttributes
      true if mandatory attributes are required to be non-null, or false for tolerating null values. ISO specifications flags some attributes as mandatory, while some other are optional. Optional attributes are allowed to be null at any time, but mandatory attributes shall never be null - in theory. However, implementers may choose to returns null on a temporary basis while they are developing their library. If this field is set to false, then missing mandatory attributes will be logged as warnings instead of causing a failure.

      The default value is true.

      See Also:
    • enforceForbiddenAttributes

      public boolean enforceForbiddenAttributes
      true if forbidden attributes are required to be null, or false for tolerating non-null values. In ISO specifications, some attributes are declared as optional in parent class and specialized in subclasses, either as mandatory or as forbidden. If this field is set to false, then forbidden attributes will be logged as warnings instead of causing a failure.

      The default value is true.

      See Also:
  • Constructor Details

    • Validator

      protected Validator(ValidatorContainer container, String packageName)
      Creates a new validator instance.
      Parameters:
      container - the set of validators to use for validating other kinds of objects (see field javadoc).
      packageName - the name of the package containing the classes to be validated.
  • Method Details

    • mandatory

      protected void mandatory(String message, Object value)
      Invoked when the existence of a mandatory attribute needs to be verified. If the given value is null or is an empty collection, then there is a choice:
      • If requireMandatoryAttributes is true (which is the default), then the test fails with the given message.
      • Otherwise, the message is logged as a warning and the test continues.
      Subclasses can override this method if they want more control.
      Parameters:
      message - the message to send in case of failure.
      value - the value to test for non-nullity.
      See Also:
    • forbidden

      protected void forbidden(String message, Object value)
      Invoked when the existence of a forbidden attribute needs to be checked. If the given value is non-null and is not an empty collection, then there is a choice:
      • If enforceForbiddenAttributes is true (which is the default), then the test fails with the given message.
      • Otherwise, the message is logged as a warning and the test continues.
      Subclasses can override this method if they want more control.
      Parameters:
      message - the message to send in case of failure.
      value - the value to test for nullity.
      See Also:
    • conditional

      protected void conditional(String message, Object value, boolean condition)
      Delegates to mandatory(String, Object) or forbidden(String, Object) depending on a condition.
      Parameters:
      message - the message to send in case of failure.
      value - the value to test for (non)-nullity.
      condition - true if the given value is mandatory, or false if it is forbidden.
      See Also:
    • validate

      protected void validate(Collection<?> collection)
      Ensures that the elements in the given collection are compliant with the Object equals(Object) and hashCode() contract. This method ensures that the equals(Object) methods implement reflexive, symmetric and transitive relations. It also ensures that if A.equals(B), then A.hashCode() == B.hashCode().

      If the given collection is null, then this method does nothing. If the given collection contains null elements, then those elements are ignored.

      This method does not invoke any other validate method on collection elements. It is caller responsibility to validates elements according their types.

      Parameters:
      collection - the collection of elements to validate, or null.
      Since:
      3.1