public class Validator<T> extends Object
Validator
instances should be immutable and obtained from
ValidatorFactory
.
Modifier and Type | Class and Description |
---|---|
protected class |
Validator.Context
This context holds current validation state.
|
class |
Validator.ContextBuilder
This builder allows to perform validation operations with custom
parameters.
|
Modifier and Type | Field and Description |
---|---|
static String[] |
ALL_GROUPS
Array of constraint groups to be used for validation all the constraints.
|
static String |
DEFAULT_GROUP
Default constraint group.
|
Modifier | Constructor and Description |
---|---|
protected |
Validator(ValidatorFactory factory,
EntityMetaData<T> entityMeta,
MessageResolver messageResolver,
MessageBuilder messageBuilder,
String integerPattern,
String decimalPattern,
String datePattern,
int dateStyle,
int timeStyle)
Constructs a new
Validator with the specified factory,
entity metadata, message resolver and error message builder. |
Modifier and Type | Method and Description |
---|---|
ValidatorFactory |
getFactory()
Returns factory that was used to create this validator.
|
String |
getLocalizedValue(T entity,
String property)
Returns localized string representation of property value for the
specified entity using default locale.
|
Map<String,String> |
getLocalizedValues(T entity)
Returns localized string representations of property values for the
specified entity using default locale.
|
MessageBuilder |
getMessageBuilder()
Returns message builder to be used for building error messages.
|
MessageResolver |
getMessageResolver()
Returns message resolver to be used for resolving localized messages.
|
EntityMetaData<T> |
getMetaData()
Returns entity metadata.
|
String |
getRawValue(T entity,
String property)
Returns default string representation of property value for the
specified entity.
|
Map<String,String> |
getRawValues(T entity)
Returns default string representations of property values for the
specified entity.
|
<V> V |
getValue(T entity,
String property)
Returns property value for the specified entity.
|
Map<String,?> |
getValues(T entity)
Returns property values for the specified entity.
|
Validator.ContextBuilder |
newContext()
Creates a new context builder.
|
Validator.ContextBuilder |
newContext(ValidationContext<?> parent)
Creates a new context builder for cascade validation.
|
void |
setLocalizedValue(T entity,
String property,
String value)
Assigns property value for the specified entity using default locale.
|
void |
setLocalizedValues(T entity,
Map<String,String> values)
Assigns property values for the specified entity using default locale.
|
void |
setRawValue(T entity,
String property,
String value)
Assigns property value for the specified entity.
|
void |
setRawValues(T entity,
Map<String,String> values)
Assigns property values for the specified entity.
|
void |
setValue(T entity,
String property,
Object value)
Assigns property value for the specified entity.
|
void |
setValues(T entity,
Map<String,?> values)
Assigns property values for the specified entity.
|
T |
validateEntity(T entity,
String... groups)
Validates the specified entity and returns possibly modified entity.
|
<V> V |
validateProperty(T entity,
String property,
String... groups)
Validates property value for the specified entity and assigns modified
value to that property.
|
<V> V |
validateValue(String property,
Object value,
String... groups)
Validates the specified property value and returns possibly modified
value.
|
public static final String DEFAULT_GROUP
public static final String[] ALL_GROUPS
protected Validator(ValidatorFactory factory, EntityMetaData<T> entityMeta, MessageResolver messageResolver, MessageBuilder messageBuilder, String integerPattern, String decimalPattern, String datePattern, int dateStyle, int timeStyle)
Validator
with the specified factory,
entity metadata, message resolver and error message builder.factory
- Factory that was used to create this validator.entityMeta
- Entity metadata.messageResolver
- Message resolver to be used for resolving
localized messages.messageBuilder
- Message builder to be used for building error
messages.integerPattern
- Default pattern for integer numbers.decimalPattern
- Default pattern for decimal numbers.datePattern
- Default pattern for dates.dateStyle
- Default date style.timeStyle
- Default time style.public final ValidatorFactory getFactory()
public final EntityMetaData<T> getMetaData()
public final MessageResolver getMessageResolver()
public final MessageBuilder getMessageBuilder()
public <V> V getValue(T entity, String property)
V
- The property value type.entity
- Entity whose property value should be returned.property
- Property name.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.UnsupportedOperationException
- if the specified property is not
readable.public void setValue(T entity, String property, Object value)
entity
- Entity whose property value should be assigned.property
- Property name.value
- New property value.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.UnsupportedOperationException
- if the specified property is not
writeable.public Map<String,?> getValues(T entity)
entity
- Entity whose property values should be returned.IllegalArgumentException
- if the specified entity is
null
.Validator.ContextBuilder.getValues(Object)
public void setValues(T entity, Map<String,?> values)
entity
- Entity whose property values should be assigned.values
- New property values.IllegalArgumentException
- if the specified entity is
null
.Validator.ContextBuilder.setValues(Object, Map)
public String getRawValue(T entity, String property)
entity
- Entity whose string representation of property value
should be returned.property
- Property name.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.UnsupportedOperationException
- if the specified property is
not readable.Validator.ContextBuilder.getEncodedValue(Object, String)
public void setRawValue(T entity, String property, String value)
entity
- Entity whose property value should be assigned.property
- Property name.value
- Default string representation of property value.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.UnsupportedOperationException
- if the specified property is
not writeable.MalformedValueException
- if conversion fails.Validator.ContextBuilder.setEncodedValue(Object, String, String)
public Map<String,String> getRawValues(T entity)
entity
- Entity whose string representations of property values
should be returned.IllegalArgumentException
- if the specified entity is
null
.Validator.ContextBuilder.getEncodedValues(Object)
public void setRawValues(T entity, Map<String,String> values)
entity
- Entity whose property values should be assigned.values
- Default string representations of property values.IllegalArgumentException
- if the specified entity is
null
.ValidationException
- if conversion fails.Validator.ContextBuilder.setEncodedValues(Object, Map)
public String getLocalizedValue(T entity, String property)
entity
- Entity whose string representation of property value
should be returned.property
- Property name.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.UnsupportedOperationException
- if the specified property is
not readable.Validator.ContextBuilder.getEncodedValue(Object, String)
public void setLocalizedValue(T entity, String property, String value)
entity
- Entity whose property value should be assigned.property
- Property name.value
- Localized string representation of property value.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.UnsupportedOperationException
- if the specified property is
not writeable.MalformedValueException
- if conversion fails.Validator.ContextBuilder.setEncodedValue(Object, String, String)
public Map<String,String> getLocalizedValues(T entity)
entity
- Entity whose string representations of property values
should be returned.IllegalArgumentException
- if the specified entity is
null
.Validator.ContextBuilder.getEncodedValues(Object)
public void setLocalizedValues(T entity, Map<String,String> values)
entity
- Entity whose property values should be assigned.values
- Localized string representations of property values.IllegalArgumentException
- if the specified entity is
null
.ValidationException
- if conversion fails.Validator.ContextBuilder.setEncodedValues(Object, Map)
public T validateEntity(T entity, String... groups)
entity
- Entity to be validated.groups
- Array of constraint groups to be validated.IllegalArgumentException
- if the specified entity is
null
.ValidationException
- if validation fails.Validator.ContextBuilder.validateEntity(Object)
public <V> V validateProperty(T entity, String property, String... groups)
V
- The property value type.entity
- Entity whose property value should be validated.property
- Property name.groups
- Array of constraint groups to be validated.IllegalArgumentException
- if the specified entity is
null
or the specified property not exists.ConstraintViolationException
- if validation fails.Validator.ContextBuilder.validateProperty(Object, String)
public <V> V validateValue(String property, Object value, String... groups)
V
- The property value type.property
- Property name.value
- Property value to be validated.groups
- Array of constraint groups to be validated.IllegalArgumentException
- if the specified property not exists.ConstraintViolationException
- if validation fails.Validator.ContextBuilder.validateValue(String, Object)
public Validator.ContextBuilder newContext()
public Validator.ContextBuilder newContext(ValidationContext<?> parent)
parent
- Parent context.IllegalStateException
- if the specified context state is invalid
for cascade validation.Copyright © 2011–2020 FoxLabs. All rights reserved.