Class DefaultGuavaValidators
There are three kinds of validators:
requireThat()for method preconditions.assert that()for class invariants, and method postconditions.checkIf()for returning multiple validation failures.
Thread Safety: This class is thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V, T extends Multimap<K, V>>
MultimapValidator<T, K, V> checkIf(T value) Validates the state of aMultimap.static <K,V, T extends Multimap<K, V>>
MultimapValidator<T, K, V> Validates the state of aMultimap.Returns the contextual information for validators created out by this factory.static GlobalConfigurationReturns the global configuration shared by all validators.static GuavaValidatorsremoveContext(String name) Removes the contextual information of validators created by this factory.static <K,V, T extends Multimap<K, V>>
MultimapValidator<T, K, V> requireThat(T value, String name) Validates the state of aMultimap.static <K,V, T extends Multimap<K, V>>
MultimapValidator<T, K, V> that(T value) Validates the state of aMultimap.static <K,V, T extends Multimap<K, V>>
MultimapValidator<T, K, V> Validates the state of aMultimap.static GuavaValidatorswithContext(Object value, String name) Sets the contextual information for validators created by this factory.
-
Method Details
-
requireThat
public static <K,V, MultimapValidator<T,T extends Multimap<K, V>> K, requireThatV> (T value, String name) Validates the state of aMultimap.The returned validator throws an exception immediately if a validation fails.
- Type Parameters:
K- the type of keys in theMultimapV- the type of values in theMultimapT- the type of theMultimap- Parameters:
value- the valuename- the name of the value- Returns:
- a validator for the value
- Throws:
NullPointerException- ifnameis nullIllegalArgumentException- ifnamecontains whitespace, or is empty
-
that
Validates the state of aMultimap.The returned validator captures exceptions on validation failure rather than throwing them immediately. The exceptions are converted into an
AssertionErrorand can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.This method is intended to be used with the
assertkeyword, like so:assert that(value, name).- Type Parameters:
K- the type of keys in the mapV- the type of values in the mapT- the type of theMultimap- Parameters:
value- the valuename- the name of the value- Returns:
- a validator for the value
- Throws:
NullPointerException- ifnameis nullIllegalArgumentException- ifnamecontains whitespace or is empty
-
that
Validates the state of aMultimap. Validation failures only throwAssertionError.- Type Parameters:
K- the type of keys in the mapV- the type of values in the mapT- the type of theMultimap- Parameters:
value- the value- Returns:
- a validator for the value
-
checkIf
public static <K,V, MultimapValidator<T,T extends Multimap<K, V>> K, checkIfV> (T value, String name) Validates the state of aMultimap.The returned validator captures exceptions on validation failure rather than throwing them immediately. These exceptions can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.
- Type Parameters:
K- the type of keys in theMultimapV- the type of values in theMultimapT- the type of theMultimap- Parameters:
value- the valuename- the name of the value- Returns:
- a validator for the value
- Throws:
NullPointerException- ifnameis nullIllegalArgumentException- ifnamecontains whitespace or is empty
-
checkIf
Validates the state of aMultimap.The returned validator captures exceptions on validation failure rather than throwing them immediately. These exceptions can be retrieved or thrown once the validation completes. Exceptions unrelated to validation failures are thrown immediately.
- Type Parameters:
K- the type of keys in theMultimapV- the type of values in theMultimapT- the type of theMultimap- Parameters:
value- the value- Returns:
- a validator for the value
-
getContext
Returns the contextual information for validators created out by this factory. The contextual information is a map of key-value pairs that can provide more details about validation failures. For example, if the message is "Password may not be empty" and the map contains the key-value pair{"username": "john.smith"}, the exception message would be:Password may not be empty username: john.smithNote that values are wrapped in an
Optionalbecause modern maps do not supportnullvalues.- Returns:
- an unmodifiable map from each entry's name to its value
-
withContext
Sets the contextual information for validators created by this factory.This method adds contextual information to exception messages. The contextual information is stored as key-value pairs in a map. Values set by this method may be overridden by
ValidatorComponent.withContext(Object, String)}.- Parameters:
value- the value of the entryname- the name of an entry- Returns:
- the underlying validator factory
- Throws:
NullPointerException- ifnameis nullIllegalArgumentException- ifname:- contains whitespace
- is empty
- is already in use by the value being validated or the validator context
-
removeContext
Removes the contextual information of validators created by this factory.- Parameters:
name- the parameter name- Returns:
- the underlying validator factory
- Throws:
NullPointerException- ifnameis nullIllegalArgumentException- ifname:- contains whitespace
- is empty
-
globalConfiguration
Returns the global configuration shared by all validators.NOTE: Updating this configuration affects existing and new validators.
- Returns:
- the global configuration updater
-