Please review. Also we need to implement the // group validation in HV to avoid to many passes.

Begin forwarded message:

From: Emmanuel Bernard <emmanuel.bernard@JBOSS.COM>
Date:  May 26, 2009 16:43:53  CEDT
To: JSR-303-EG@JCP.ORG
Subject: Support for constraints by groups
Reply-To: Java Community Process JSR #303 Expert List <JSR-303-EG@JCP.ORG>

Hello,
following a subject dear to Gerhard's heart :)
I could think of two approaches, let's beat them to death and if possible find a better alternative :)


# background
When validating a specific group(s), Bean Validation expends the group into a sequence of groups to be validated (because a group can be a group sequence really). Depending on how clever the implementation is, it could sequencialize all group execution or try to validate several non sequenced group in parallel.

The concept of discovering which group is validated based on the requested groups is complex enough so an API at the metadata level is required.

examples
public interface Minimal {}

@GroupSequence(Minimal.class, Default.class)
public interface Complete {}


#1 define the API at the ElementDescriptor level

BeanDescriptor bd = validator.getConstraintsForClass(Address.class);
PropertyDescriptor pd = bd.getConstraintsForProperty("street1");
List<Set<ConstraintDescriptor>> sequencedConstraints = pd.getConstraintDescriptorsForGroups(Complete.class);
//all ConstraintDescriptor in a given set can be validated in the same pass
//two sets of constraints must be validated sequencially. If one constraint fails, the following sets must be ignored

for (Set<ConstraintDescriptor> subConstraints :  sequencedConstraints) {
  if ( validate(subConstraints) == true ) break;
}

The API added is 
ElementDescriptor#getConstraintDescriptorsForGroups(Class<?>... groups)

Optionally we can add
ElementDescriptor#hasConstraints(Class<?>... groups)

Pro: The metadata is never "contextual" hence has less risk of error.
Con: Needs to pass the groups at several levels

#2 define the API at the validator level (more aligned with Gerhard's proposal but supporting sequences)

List<BeanDescriptor> sequencedDescriptors = validator.getConstraintsForClassAndGroups(Address.class, Complete.class);
for (BeanDescriptor bd : sequencedDescriptor) {
    PropertyDescriptor pd = bd. getConstraintsForProperty("street1");
    Set<ConstraintDescriptor>  subConstraints =  pd.getConstraintDescriptors();
    if ( validate(subConstraints) == true ) break;
}

Pro: The metadata is "contextual" hence people can become confused
Con: once contextualized, the API looks the same as the non contextual one

Questions:
 - should we use List to represent a sequence r should we have a dedicated object implementing Iterable
 - we need to ensure that a given group always return the same sequencing regardless of the element (bean or property). This is necessary to ensure that one can validate all bean and property level constraints (and potentially the associated object) before going to the next sequence. How can we formalize that?

WDYT of all that?