package com.sun.aus.constraints.samples; import java.util.*; import com.sun.aus.constraints.annotation.*; /** * This class contains example usage of different constraints. * * @author Anders Holmgren */ public class Samples { /** * A simple mandatory string property. I.e. the String cannot be null */ @Mandatory public void setStringPropertyOne(String stringPropertyOne) { this.stringPropertyOne = stringPropertyOne; } /** * A optional string property that if not null must be between 5 & 40 * characters in length. */ @MinLength(5) @MaxLength(40) public void setStringPropertyTwo(String stringPropertyTwo) { this.stringPropertyTwo = stringPropertyTwo; } /** * A mandatory string property that must be non null and between 0 & 900 * characters in length. Note the MinLength of 0 is implicit in this case */ @Mandatory @MaxLength(900) public void setStringPropertyThree(String stringPropertyThree) { this.stringPropertyThree = stringPropertyThree; } /** * A mandatory credit card property that must match a pattern like * 1234-5678-9012-3456 (i.e. 4 blocks of 4 digits separated by dashes). * Note that special characters in regular expressions need to be escaped * again in Java. */ @Mandatory @Pattern("(\\d{4}\\-){3}\\d{4}") public void setCreditCard(String creditCard) { this.creditCard = creditCard; } /** * The age property must be in the range 19 <= age <= 25. * Note because age is a primitive type there is no notion of null. * Therefore adding a Mandatory constraint would not make sense. */ @MinInclusive(19) @MaxInclusive(25) public void setAge(int age) { this.age = age; } /** * An example property with a upper bound constraint. i.e. aDouble <= 23.45692. * Note that if this property is never set then it will default to 0.0 which * does not violate the constraint. */ @MaxInclusive(23.45692) public void setADouble(double aDouble) { this.aDouble = aDouble; } /** * This is similar to the aDouble property except that this time it is not a * primitive type. Therefore it could be null and the addition of a Mandatory * constraint has an effect. * In contrast to the aDouble property, not setting the aBigDouble property * will cause a constraint violation (because it is mandatory). If the * property was explicitly set to 0 then this would behave the same as the * aDouble property. */ @Mandatory @MaxInclusive(23.45692) public void setABigDouble(Double aBigDouble) { this.aBigDouble = aBigDouble; } /** * A mandatory property of a complex type. In this case not only have we * put a Mandatory constraint on the property but the * ClassWithConstrainedProperties class has constraints on the aProperty * property. * This complexProperty property, therefore, is valid if it is non null and * all the properties defined in ClassWithConstrainedProperties are valid. */ @Mandatory public void setComplexProperty(ClassWithConstrainedProperties complexProperty) { this.complexProperty = complexProperty; } /** * An example of constraints applied to collections. In this case we are * enforcing that there are between 3 and 8 items in the list. */ @MinOccurs(3) @MaxOccurs(8) public void setStringList(List stringList) { this.stringList = stringList; } /** * This is the same as the previous example, but the constraints are on the * getter method rather than the setter. * Note, in the case of collection type properties, it is useful to * support constraints on the getter method, because you may not have a setter * but instead have a getter that exposes a mutable collection. */ @MinOccurs(3) @MaxOccurs(8) public List getStringList2() { return stringList2; } /** * Another example of constraints applied to collections. Once again we are * enforcing that there are between 3 and 8 items in the list. * However this time the list contains complex objects which also have * constrained properties. * This complexList property, therefore, is valid if it has between 3 & 8 * members, each of which has all its properties (as defined in * ClassWithConstrainedProperties) being valid. */ @MinOccurs(3) @MaxOccurs(8) public void setComplexList(List complexList) { this.complexList = complexList; } /** * A helper class that also has constrained properties */ public static class ClassWithConstrainedProperties { @Mandatory @MaxLength(20) public void setAProperty(String aProperty) { this.aProperty = aProperty; } private String aProperty; public String getAProperty() { return aProperty; } } // keep fields and getters out of the way private String stringPropertyOne; public String getStringPropertyOne() { return stringPropertyOne; } private String stringPropertyTwo; public String getStringPropertyTwo() { return stringPropertyTwo; } private String stringPropertyThree; public String getStringPropertyThree() { return stringPropertyThree; } private String creditCard; public String getCreditCard() { return creditCard; } private int age; public int getAge() { return age; } private double aDouble; public double getADouble() { return aDouble; } private Double aBigDouble; public Double getABigDouble() { return aBigDouble; } private ClassWithConstrainedProperties complexProperty; public ClassWithConstrainedProperties getComplexProperty() { return complexProperty; } private List stringList; public List getStringList() { return stringList; } private List complexList; public List getComplexList() { return complexList; } private List stringList2; public void setStringList2(List stringList2) { this.stringList2 = stringList2; } }