Created
May 7, 2012 22:18
-
-
Save krisskross/2630916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Config(desc = "A binary tree") | |
@BinaryTreeConstraint | |
public class BinaryTree { | |
@Id(desc = "id of current node") | |
private String id; | |
@Config(desc = "value of current node") | |
@NotNull @Min(1) | |
private Integer value; | |
@Config(desc = "left child") | |
private BinaryTree left; | |
@Config(desc = "right child") | |
private BinaryTree right; | |
public Integer getValue() { return value; } | |
public BinaryTree getLeft() { return left; } | |
public BinaryTree getRight() { return right; } | |
public String toString() { return id + "=" + value; } | |
} | |
public class BinaryTreeValidator implements | |
ConstraintValidator<BinaryTreeConstraint, BinaryTree> { | |
public boolean isValid(BinaryTree n, ConstraintValidatorContext c) { | |
if (n.getLeft() != null && n.getValue() < n.getLeft().getValue()) { | |
String msg = n.getLeft() + " must be to right of " + n; | |
c.buildConstraintViolationWithTemplate(msg).addConstraintViolation(); | |
return false; | |
} | |
if (n.getRight() != null && n.getValue() > n.getRight().getValue()) { | |
String msg = n.getRight() + " must be to left of " + n; | |
c.buildConstraintViolationWithTemplate(msg).addConstraintViolation(); | |
return false; | |
} | |
return true; | |
} | |
public void initialize(BinaryTreeConstraint constraintAnnotation) { } | |
} | |
@Target({ TYPE }) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Constraint(validatedBy = BinaryTreeValidator.class) | |
public @interface BinaryTreeConstraint { | |
String message() default ""; | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment