public class ConstrainedProperty extends Object
applyConstraint(String constraintName, Object constrainingValue)in combination with a constraint constant. Example:
...
ConstrainedProperty cp = new ConstrainedProperty(owningClass, propertyName, propertyType);
if (cp.supportsConstraint(ConstrainedProperty.EmailConstraint.VALIDATION_DSL_NAME)) {
cp.applyConstraint(ConstrainedProperty.EmailConstraint.VALIDATION_DSL_NAME, new Boolean(true));
}
Alternatively constraints can be applied directly using the java bean getters/setters if a static (as opposed to dynamic)
approach to constraint creation is possible:
cp.setEmail(true)
Modifier and Type | Field and Description |
---|---|
protected Map<String,Constraint> |
appliedConstraints |
protected static Map<String,List<Object>> |
constraints |
static Map<String,String> |
DEFAULT_MESSAGES |
protected static org.slf4j.Logger |
LOG |
protected MessageSource |
messageSource |
protected Class<?> |
owningClass |
protected String |
propertyName |
protected Class<?> |
propertyType |
Constructor and Description |
---|
ConstrainedProperty(Class<?> clazz,
String propertyName,
Class<?> propertyType)
Constructs a new ConstrainedProperty for the given arguments.
|
Modifier and Type | Method and Description |
---|---|
void |
addMetaConstraint(String name,
Object value)
Adds a meta constraints which is a non-validating informational constraint.
|
void |
applyConstraint(String constraintName,
Object constrainingValue)
Applies a constraint for the specified name and constraint value.
|
Constraint |
getAppliedConstraint(String name)
Obtains an applied constraint by name.
|
Collection<Constraint> |
getAppliedConstraints() |
Map<String,Object> |
getAttributes() |
String |
getFormat() |
List |
getInList() |
String |
getMatches() |
Comparable |
getMax() |
Integer |
getMaxSize() |
Object |
getMetaConstraintValue(String name)
Obtains the value of the named meta constraint.
|
Comparable |
getMin() |
Integer |
getMinSize() |
Object |
getNotEqual() |
int |
getOrder() |
Class<?> |
getOwningClass() |
String |
getPropertyName() |
Class<?> |
getPropertyType() |
Range |
getRange() |
Integer |
getScale() |
Range |
getSize() |
String |
getWidget() |
boolean |
hasAppliedConstraint(String constraintName) |
static boolean |
hasRegisteredConstraint(String constraintName) |
boolean |
isBlank() |
boolean |
isCreditCard() |
boolean |
isDisplay() |
boolean |
isEditable() |
boolean |
isEmail() |
boolean |
isEnabled() |
boolean |
isNullable() |
boolean |
isPassword() |
boolean |
isUrl() |
boolean |
isValidator() |
static void |
registerNewConstraint(String name,
Class<?> constraintClass) |
static void |
registerNewConstraint(String name,
ConstraintFactory factory) |
static void |
removeConstraint(String name) |
static void |
removeConstraint(String name,
Class<?> constraintClass) |
void |
setAttributes(Map<String,Object> attributes) |
void |
setBlank(boolean blank) |
void |
setCreditCard(boolean creditCard) |
void |
setDisplay(boolean display) |
void |
setEditable(boolean editable) |
void |
setEmail(boolean email) |
void |
setEnabled(boolean enabled) |
void |
setFormat(String format) |
void |
setInList(List inList) |
void |
setMatches(String regex) |
void |
setMax(Comparable max) |
void |
setMaxSize(Integer maxSize) |
void |
setMessageSource(MessageSource source)
The message source used to evaluate error messages
|
void |
setMin(Comparable min) |
void |
setMinSize(Integer minSize) |
void |
setNotEqual(Object notEqual) |
void |
setNullable(boolean nullable) |
void |
setOrder(int order) |
void |
setPassword(boolean password) |
void |
setPropertyName(String propertyName) |
void |
setRange(Range range) |
void |
setSize(Range size) |
void |
setUrl(boolean url) |
void |
setValidator(Validator validator) |
void |
setWidget(String widget) |
boolean |
supportsContraint(String constraintName)
Checks with this ConstrainedProperty instance supports applying the specified constraint.
|
String |
toString() |
void |
validate(Object target,
Object propertyValue,
Errors errors)
Validate this constrained property against specified property value
|
protected static final org.slf4j.Logger LOG
protected String propertyName
protected Class<?> propertyType
protected Map<String,Constraint> appliedConstraints
protected Class<?> owningClass
protected MessageSource messageSource
public ConstrainedProperty(@Nonnull Class<?> clazz, @Nonnull String propertyName, @Nonnull Class<?> propertyType)
clazz
- The owning classpropertyName
- The name of the propertypropertyType
- The property typepublic static void removeConstraint(String name)
public static void registerNewConstraint(String name, Class<?> constraintClass)
public static void registerNewConstraint(String name, ConstraintFactory factory)
public static boolean hasRegisteredConstraint(String constraintName)
public Collection<Constraint> getAppliedConstraints()
public Constraint getAppliedConstraint(String name)
name
- The name of the constraintpublic boolean hasAppliedConstraint(String constraintName)
constraintName
- The name of the constraint to checkpublic Class<?> getPropertyType()
public Comparable getMax()
public void setMax(Comparable max)
max
- The max to set.public Comparable getMin()
public void setMin(Comparable min)
min
- The min to set.public List getInList()
public void setInList(List inList)
inList
- The inList to set.public Range getRange()
public void setRange(Range range)
range
- The range to set.public Integer getScale()
public Range getSize()
public void setSize(Range size)
size
- The size to set.public boolean isBlank()
public void setBlank(boolean blank)
blank
- The blank to set.public boolean isEmail()
public void setEmail(boolean email)
email
- The email to set.public boolean isCreditCard()
public void setCreditCard(boolean creditCard)
creditCard
- The creditCard to set.public String getMatches()
public void setMatches(String regex)
regex
- The matches to set.public Object getNotEqual()
public Integer getMaxSize()
public void setMaxSize(Integer maxSize)
maxSize
- The maxSize to set.public Integer getMinSize()
public void setMinSize(Integer minSize)
minSize
- The minLength to set.public void setNotEqual(Object notEqual)
notEqual
- The notEqual to set.public boolean isNullable()
public void setNullable(boolean nullable)
nullable
- The nullable to set.public Class<?> getOwningClass()
public String getPropertyName()
public void setPropertyName(String propertyName)
propertyName
- The propertyName to set.public boolean isUrl()
public void setUrl(boolean url)
url
- The url to set.public boolean isValidator()
public void setValidator(Validator validator)
validator
- The validator to set.public void setMessageSource(MessageSource source)
source
- The MessageSource instance to use to resolve messagespublic void validate(Object target, Object propertyValue, Errors errors)
target
- The target object to validatepropertyValue
- The value of the property to validateerrors
- The Errors instances to report errors topublic boolean supportsContraint(String constraintName)
constraintName
- The name of the constraintpublic void applyConstraint(String constraintName, Object constrainingValue)
constraintName
- The name of the constraintconstrainingValue
- The constraining valueConstraintException
- Thrown when the specified constraint is not supported by this ConstrainedProperty. Use supportsContraint(String constraintName)
to check before callingpublic void addMetaConstraint(String name, Object value)
name
- The name of the constraintvalue
- The valuepublic Object getMetaConstraintValue(String name)
name
- The name of the constraintpublic boolean isDisplay()
public void setDisplay(boolean display)
display
- The display to set.public boolean isEditable()
public void setEditable(boolean editable)
editable
- The editable to set.public boolean isEnabled()
public void setEnabled(boolean enabled)
enabled
- The enabled to set.public String getFormat()
public void setFormat(String format)
public boolean isPassword()
public void setPassword(boolean password)
public String getWidget()
public void setWidget(String widget)
public int getOrder()
public void setOrder(int order)