org.apache.bval
Class BeanValidationContext<T extends ValidationListener>

java.lang.Object
  extended by org.apache.bval.BeanValidationContext<T>
All Implemented Interfaces:
ValidationContext<T>
Direct Known Subclasses:
GroupValidationContextImpl

public class BeanValidationContext<T extends ValidationListener>
extends Object
implements ValidationContext<T>

Description: Context during validation to help the Validation and the BeanValidator do their jobs. Used to bundle BeanValidationContext and ValidationListener together
This class is NOT thread-safe: a new instance will be created for each validation processing per thread.


Field Summary
protected  Map validatedObjects
          set of objects already validated to avoid endless loops.
 
Constructor Summary
  BeanValidationContext(T listener)
          Create a new BeanValidationContext instance.
protected BeanValidationContext(T listener, Map validatedMap)
          Create a new BeanValidationContext instance.
 
Method Summary
 boolean collectValidated()
          Avoid recursion by recording the current state of this context as having been validated.
 AccessStrategy getAccess()
          Get the current access strategy.
 Object getBean()
          Get the bean.
 T getListener()
          Get the ValidationListener.
 FeaturesCapable getMeta()
          Depending on whether we have a metaProperty or not, this returns the metaProperty or otherwise the metaBean.
 MetaBean getMetaBean()
          Get the model meta-bean.
 MetaProperty getMetaProperty()
          Get the model meta-property.
 String getPropertyName()
          Convenience method to access metaProperty.name
 Object getPropertyValue()
          Get the cached value or access it somehow (via field or method)
you should prefer getPropertyValue(AccessStrategy) instead of this method
 Object getPropertyValue(AccessStrategy access)
          Get the value by using the given access strategy.
 boolean isFixed()
          Learn whether the current property value is "fixed."
 boolean isValidated(Object object)
          Learn whether a particular object has been validated.
 void moveDown(MetaProperty prop, AccessStrategy access)
          Step deeper into association at 'prop'
 void moveUp(Object bean, MetaBean aMetaBean)
          Step out from a validation of associated objects.
 void resetValidated()
          Clear map of validated objects (invoke when you want to 'reuse' the context for different validations)
 void setBean(Object bean)
          Set the bean.
 void setBean(Object aBean, MetaBean aMetaBean)
          Set the current bean/metabean.
 void setCurrentIndex(Integer index)
          Set the index of the object currently validated into the context.
 void setCurrentKey(Object key)
          set the key of the object in a map currently validated into the context.
 void setFixed(boolean fixed)
          Potentially declare the current property value as being "fixed." If true, the context will reuse any not-UNKNOWN propertyValue regardless of the AccessStrategy by which it is requested.
 void setFixedValue(Object value)
          Set the property value, fixed.
 void setListener(T listener)
          Set the listener.
 void setMetaBean(MetaBean metaBean)
          Set the model meta-bean.
 void setMetaProperty(MetaProperty metaProperty)
          Set the current meta-property.
 void setPropertyValue(Object propertyValue)
          Set the current property value.
 String toString()
          
 void unknownValue()
          Drop cached value, marking the internal cachedValue as UNKNOWN.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

validatedObjects

protected Map validatedObjects
set of objects already validated to avoid endless loops.

Constructor Detail

BeanValidationContext

public BeanValidationContext(T listener)
Create a new BeanValidationContext instance.

Parameters:
listener -

BeanValidationContext

protected BeanValidationContext(T listener,
                                Map validatedMap)
Create a new BeanValidationContext instance.

Parameters:
listener -
validatedMap -
Method Detail

getListener

public T getListener()
Get the ValidationListener.

Specified by:
getListener in interface ValidationContext<T extends ValidationListener>
Returns:
T

setListener

public void setListener(T listener)
Set the listener.

Parameters:
listener - T

collectValidated

public boolean collectValidated()
Avoid recursion by recording the current state of this context as having been validated.

Here, state equates to a given bean reference.

Specified by:
collectValidated in interface ValidationContext<T extends ValidationListener>
Returns:
true when this state had not already been recorded

isValidated

public boolean isValidated(Object object)
Learn whether a particular object has been validated.

Parameters:
object -
Returns:
true when the object has already been validated in this context

resetValidated

public void resetValidated()
Clear map of validated objects (invoke when you want to 'reuse' the context for different validations)


setBean

public void setBean(Object aBean,
                    MetaBean aMetaBean)
Set the current bean/metabean.

Specified by:
setBean in interface ValidationContext<T extends ValidationListener>

getPropertyValue

public Object getPropertyValue()
Get the cached value or access it somehow (via field or method)
you should prefer getPropertyValue(AccessStrategy) instead of this method

Specified by:
getPropertyValue in interface ValidationContext<T extends ValidationListener>
Returns:
the current value of the property accessed by reflection
Throws:
IllegalArgumentException - - error accessing attribute (config error, reflection problem)
IllegalStateException - - when no property is currently set in the context (application logic bug)

getPropertyValue

public Object getPropertyValue(AccessStrategy access)
                        throws IllegalArgumentException,
                               IllegalStateException
Get the value by using the given access strategy. Caches retrieved value.

Specified by:
getPropertyValue in interface ValidationContext<T extends ValidationListener>
Returns:
Object
Throws:
IllegalArgumentException
IllegalStateException

getPropertyName

public String getPropertyName()
Convenience method to access metaProperty.name

Specified by:
getPropertyName in interface ValidationContext<T extends ValidationListener>
Returns:
null or the name of the current property

setPropertyValue

public void setPropertyValue(Object propertyValue)
Set the current property value.

Parameters:
propertyValue -

setFixedValue

public void setFixedValue(Object value)
Set the property value, fixed.

Parameters:
value -

isFixed

public boolean isFixed()
Learn whether the current property value is "fixed."

Returns:
boolean

setFixed

public void setFixed(boolean fixed)
Potentially declare the current property value as being "fixed." If true, the context will reuse any not-UNKNOWN propertyValue regardless of the AccessStrategy by which it is requested.

Parameters:
fixed -

getMeta

public FeaturesCapable getMeta()
Depending on whether we have a metaProperty or not, this returns the metaProperty or otherwise the metaBean. This is used to have a simple way to request features in the Validation for both bean- and property-level validations.

Returns:
something that is capable to deliver features

unknownValue

public void unknownValue()
Drop cached value, marking the internal cachedValue as UNKNOWN. This forces the BeanValidationContext to recompute the value the next time it is accessed. Use this method inside tests or when the propertyValue has been changed during validation.


getMetaBean

public MetaBean getMetaBean()
Get the model meta-bean.

Specified by:
getMetaBean in interface ValidationContext<T extends ValidationListener>
Returns:
MetaBean

getBean

public Object getBean()
Get the bean.

Specified by:
getBean in interface ValidationContext<T extends ValidationListener>
Returns:
Object

getMetaProperty

public MetaProperty getMetaProperty()
Get the model meta-property.

Specified by:
getMetaProperty in interface ValidationContext<T extends ValidationListener>
Returns:
MetaProperty

setMetaBean

public void setMetaBean(MetaBean metaBean)
Set the model meta-bean.

Specified by:
setMetaBean in interface ValidationContext<T extends ValidationListener>

setBean

public void setBean(Object bean)
Set the bean.

Specified by:
setBean in interface ValidationContext<T extends ValidationListener>

setMetaProperty

public void setMetaProperty(MetaProperty metaProperty)
Set the current meta-property.

Specified by:
setMetaProperty in interface ValidationContext<T extends ValidationListener>

toString

public String toString()

Overrides:
toString in class Object

moveDown

public void moveDown(MetaProperty prop,
                     AccessStrategy access)
Step deeper into association at 'prop'

Specified by:
moveDown in interface ValidationContext<T extends ValidationListener>

moveUp

public void moveUp(Object bean,
                   MetaBean aMetaBean)
Step out from a validation of associated objects.

Specified by:
moveUp in interface ValidationContext<T extends ValidationListener>

setCurrentIndex

public void setCurrentIndex(Integer index)
Set the index of the object currently validated into the context. used to create the propertyPath with [index] information for collections.

Specified by:
setCurrentIndex in interface ValidationContext<T extends ValidationListener>

setCurrentKey

public void setCurrentKey(Object key)
set the key of the object in a map currently validated into the context. used to create the propertyPath with [key] information for maps.

Specified by:
setCurrentKey in interface ValidationContext<T extends ValidationListener>

getAccess

public AccessStrategy getAccess()
Get the current access strategy.

Specified by:
getAccess in interface ValidationContext<T extends ValidationListener>
Returns:
AccessStrategy


Copyright © 2010-2012 Apache Software Foundation. All Rights Reserved.