Class ValidatorUtils
java.lang.Object
org.netbeans.validation.api.ValidatorUtils
Some utilities for manipulating and creating new Validators, useful in many situations.
- Author:
- Tim Boudreau
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Validator<T> Returns a validator which wraps the passed validator, but treats null values as legal.final <T,R> Validator <T> Will do the following: Check iftis equal to or assignable to this Validator's model type.static <T,R> Validator <T> Get a validator for a validator of a possibly unknown type.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.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.static <T> Validator<T> Merge together a chain of validators (all working which work against the same type), using logicalAND.static <T> Validator<T> Merge together two validators (both of which work against the same type), using logicalAND.
-
Constructor Details
-
ValidatorUtils
public ValidatorUtils()
-
-
Method Details
-
merge
Merge together a chain of validators (all working which work against the same type), using logicalAND. 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
Merge together two validators (both of which work against the same type), using logicalAND. 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 validatorvalidator2- 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
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 ofmerge(Validator,Validator).- Type Parameters:
T- The type- Parameters:
maximum-validator-
-
cast
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'smodelType(). 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 validatorR- The type of the actual validator- Parameters:
type- The model type of the desired validatorother- A validator whose type should be a supertype or the same as the passedtypeargument- Returns:
- A validator of the desired type
-
allowNull
-
as
Will do the following:- Check if
tis 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
Converterthat acts as a factory for Validators of the passed type from Validators of this type (e.g. produce a Validator for ajavax.swing.text.Documentfrom a Validator for ajava.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
- Check if
-