interface @ConstraintDeclaration

@ConstraintDeclaration is used to create new custom Constraint validators.

Description

Description goes here...

Declaration

package org.qi4j

import

public interface
{

}

Example

Let's create a @Range constraint annotation, which can be given a maximum and minimum value that is allowed. First we will need the custom annotation @Range and we need a Constraint implementation.

@ConstraintDeclaration
@Retention( RetentionPolicy.RUNTIME )
@Target( { ElementType.PARAMETER, ElementType.ANNOTATION_TYPE } )
public @interface Range
{
    double min();

    double max();
}

public class RangeConstraint
    implements Constraint<Range, Numeric>
{
    public boolean isValid( Range annotation, Numeric object )
    {
        double argument = object.doubleValue();
        return argument >= annotation.min() &&
               argument <= annotation.max();        
    }
}

To use the custom annotation above, we can apply it to a method in a MixinType.
@Constraints( RangeConstraint.class )
public interface AccountCreation
{
    Account createAccount( @Range( min=18, max=65 ) int age );
}
Powered by SiteVisionexternal link.