Class ValidatorUtils

java.lang.Object
org.netbeans.validation.api.ValidatorUtils

public class ValidatorUtils extends Object
Some utilities for manipulating and creating new Validators, useful in many situations.
Author:
Tim Boudreau
  • Constructor Details

    • ValidatorUtils

      public ValidatorUtils()
  • Method Details

    • merge

      @SafeVarargs public static <T> Validator<T> merge(Validator<T>... validators)
      Merge together a chain of validators (all working which work against the same type), using logical AND. The resulting validator reports success if all the merged validators report success. (If one or more of the merged validators report failure, the resulting merged validator reports failure.)
      Type Parameters:
      T - The type of model (Document, String, etc.) you want to work with
      Parameters:
      validators - A chain of validators which should be logically AND'd together, merged into a single validator
      Returns:
      a single validator which delegates to all of the passed ones
    • merge

      public static <T> Validator<T> merge(Validator<T> validator1, Validator<T> validator2)
      Merge together two validators (both of which work against the same type), using logical AND. The resulting validator reports success if both the merged validators report success. (If one or both of the merged validators report failure, the resulting merged validator reports failure.)

      Unlike merge(Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to merge more than two validators, simply merge the result of merging two with the next one.

      Type Parameters:
      T - The type of model (Document, String, etc.) you want to work with
      Parameters:
      validator1 - one validator
      validator2 - another validator of the same type
      Returns:
      a single validator which delegates to both of the passed ones
    • limitSeverity

      @SafeVarargs public static <T> Validator<T> limitSeverity(Severity maximum, Validator<T>... validators)
      Wrapper one or more validators in a validator which imposes a limit on the severity of the validators in use. This means that while the validators you pass in here will work normally, all Problems encountered which are of greater severity than the maximum passed here will be reset to the level you pass here.

      This makes it possible to use validators from the StringValidators enum, which mostly produce FATAL errors, for cases where only a warning or info message is actually to be displayed.

      Type Parameters:
      T - The type
      Parameters:
      maximum -
      validators -
    • limitSeverity

      public static <T> Validator<T> limitSeverity(Severity maximum, Validator<T> validator)
      Wraps a validator in a validator which imposes a limit on the severity of the validator in use. This means that while the validator you pass in here will work normally, all Problems encountered which are of greater severity than the maximum passed here will be reset to the level you pass here.

      This makes it possible to use validators from the StringValidators enum, which mostly produce FATAL errors, for cases where only a warning or info message is actually to be displayed.

      Unlike limitSeverity(Severity,Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to limit more than one validator, simply limit the result of merge(Validator,Validator).

      Type Parameters:
      T - The type
      Parameters:
      maximum -
      validator -
    • cast

      public static <T,R> Validator<T> cast(Class<T> type, Validator<R> other)
      Get a validator for a validator of a possibly unknown type. The resulting validator simply uses other.modelType().cast(model) and passes it on; this will result in a ClassCastException if the passed type argument is not the same as or a subtype of the passed Validator's modelType().

      This method exists principally to avoid compile-time warnings for cases where a validator type is not actually known at compile time. Most client code will use a specific type; this technique is mainly used for cases of factories for validators against multiple types, whose types cannot be determined at compile-time.

      Type Parameters:
      T - The type parameter of the desired validator
      R - The type of the actual validator
      Parameters:
      type - The model type of the desired validator
      other - A validator whose type should be a supertype or the same as the passed type argument
      Returns:
      A validator of the desired type
    • allowNull

      public static <T> Validator<T> allowNull(Validator<T> other)
      Returns a validator which wraps the passed validator, but treats null values as legal.
      Type Parameters:
      T - The type of the other validator
      Parameters:
      other - The other validator
      Returns:
      The validator
    • as

      public final <T,R> Validator<T> as(Class<T> t, Validator<R> v)
      Will do the following:
      • Check if t is equal to or assignable to this Validator's model type. If so, will return an adapter which casts this Validator as that type.
      • Check if there is a registered Converter that acts as a factory for Validators of the passed type from Validators of this type (e.g. produce a Validator for a javax.swing.text.Document from a Validator for a java.lang.String). If one is found, use that to manufacture a wrapper validator for this one, which converts arguments appropriately.
      Type Parameters:
      T -
      Parameters:
      t -
      Returns:
      A validator
      Throws:
      IllegalArgumentException - if no way is known to either cast or adapt this validator's model type to the requested one