«constantsDeclarations»
«generateFields»
- «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty ||
- !restrictions.lengthConstraints.nullOrEmpty)»
- «generateConstraints»
-
- «IF !restrictions.rangeConstraints.nullOrEmpty»
- «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraints)»
- «ENDIF»
+ «IF restrictions != null»
+ «IF !restrictions.lengthConstraints.nullOrEmpty»
+ «LengthGenerator.generateLengthChecker("_value", findProperty(genTO, "value").returnType, restrictions.lengthConstraints)»
+ «ENDIF»
+ «IF !restrictions.rangeConstraints.nullOrEmpty»
+ «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraints)»
+ «ENDIF»
«ENDIF»
«constructors»
«generateToString(genTO.toStringIdentifiers)»
- «generateLengthMethod("length", "_length")»
+ «generateLengthMethod()»
«generateRangeMethod()»
}
'''
- def private generateLengthMethod(String methodName, String varName) '''
+ @Deprecated
+ def private generateLengthMethod() '''
«IF restrictions != null && !(restrictions.lengthConstraints.empty)»
«val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
/**
* @deprecated This method is slated for removal in a future release. See BUG-1485 for details.
*/
@Deprecated
- public static «List.importedName»<«Range.importedName»<«numberClass.importedNumber»>> «methodName»() {
- return «varName»;
+ public static «List.importedName»<«Range.importedName»<«numberClass.importedNumber»>> length() {
+ «List.importedName»<«Range.importedName»<«numberClass.importedName»>> ret = new java.util.ArrayList<>(«restrictions.lengthConstraints.size»);
+ «FOR r : restrictions.lengthConstraints»
+ ret.add(«Range.importedName».closed(«numericValue(numberClass, r.min)», «numericValue(numberClass, r.max)»));
+ «ENDFOR»
+ return ret;
}
«ENDIF»
'''
+ @Deprecated
private def rangeBody(List<RangeConstraint> restrictions, Class<? extends Number> numberClass) '''
«List.importedName»<«Range.importedName»<«numberClass.importedName»>> ret = new java.util.ArrayList<>(«restrictions.size»);
«FOR r : restrictions»
«ENDFOR»
'''
+ @Deprecated
def private generateRangeMethod() '''
«IF restrictions != null && !(restrictions.rangeConstraints.empty)»
«val returnType = allProperties.iterator.next.returnType»
«ENDIF»
'''
- def private generateConstraints() '''
- static {
- «IF !restrictions.lengthConstraints.nullOrEmpty»
- «generateLengthConstraints»
- «ENDIF»
- }
- '''
-
- private def lengthBody(Restrictions restrictions, Class<? extends Number> numberClass, String className, String varName) '''
- «ImmutableList.importedName».Builder<«Range.importedName»<«numberClass.importedName»>> builder = «ImmutableList.importedName».builder();
- «FOR r : restrictions.lengthConstraints»
- builder.add(«Range.importedName».closed(«numericValue(numberClass, r.min)», «numericValue(numberClass, r.max)»));
- «ENDFOR»
- «varName» = builder.build();
- '''
-
- private def generateLengthConstraints() '''
- «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
- «val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
- «IF numberClass.equals(typeof(BigDecimal))»
- «lengthBody(restrictions, numberClass, genTO.importedName, "_length")»
- «ELSE»
- «lengthBody(restrictions, typeof(BigInteger), genTO.importedName, "_length")»
- «ENDIF»
- «ENDIF»
- '''
-
def protected allValuesConstructor() '''
«IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
@«ConstructorProperties.importedName»("value")
«FOR c : consts»
«IF c.name == TypeConstants.PATTERN_CONSTANT_NAME && c.value instanceof List<?>»
- for (Pattern p : patterns) {
- «Preconditions.importedName».checkArgument(p.matcher(_value).matches(), "Supplied value \"%s\" does not match any of the permitted patterns %s", _value, «TypeConstants.PATTERN_CONSTANT_NAME»);
- }
-
+ for (Pattern p : patterns) {
+ «Preconditions.importedName».checkArgument(p.matcher(_value).matches(), "Supplied value \"%s\" does not match any of the permitted patterns %s", _value, «TypeConstants.PATTERN_CONSTANT_NAME»);
+ }
«ENDIF»
«ENDFOR»
«ENDIF»
}
'''
+ def private static paramValue(Type returnType, String paramName) {
+ if (returnType instanceof ConcreteType) {
+ return paramName
+ } else {
+ return paramName + ".getValue()"
+ }
+ }
+
def private generateRestrictions(Type type, String paramName, Type returnType) '''
«val restrictions = type.getRestrictions»
«IF restrictions !== null»
- «val boolean isNestedType = !(returnType instanceof ConcreteType)»
- «IF !restrictions.lengthConstraints.empty»
- «generateLengthRestriction(returnType, restrictions, paramName, isNestedType)»
- «ENDIF»
- «IF !restrictions.rangeConstraints.empty»
- if («paramName» != null) {
- «IF isNestedType»
- «rangeGenerator.generateRangeCheckerCall(paramName, paramName + ".getValue()")»
- «ELSE»
- «rangeGenerator.generateRangeCheckerCall(paramName, paramName)»
- «ENDIF»
+ «IF !restrictions.lengthConstraints.empty || !restrictions.rangeConstraints.empty»
+ if («paramName» != null) {
+ «IF !restrictions.lengthConstraints.empty»
+ «LengthGenerator.generateLengthCheckerCall(paramName, paramValue(returnType, paramName))»
+ «ENDIF»
+ «IF !restrictions.rangeConstraints.empty»
+ «rangeGenerator.generateRangeCheckerCall(paramName, paramValue(returnType, paramName))»
+ «ENDIF»
}
«ENDIF»
«ENDIF»
'''
- def private generateLengthRestriction(Type returnType, Restrictions restrictions, String paramName, boolean isNestedType) '''
- «val clazz = restrictions.lengthConstraints.iterator.next.min.class»
- if («paramName» != null) {
- «printLengthConstraint(returnType, clazz, paramName, isNestedType, returnType.name.contains("["))»
- boolean isValidLength = false;
- for («Range.importedName»<«clazz.importedNumber»> r : «IF isNestedType»«returnType.importedName».«ENDIF»length()) {
- if (r.contains(_constraint)) {
- isValidLength = true;
- }
- }
- if (!isValidLength) {
- throw new IllegalArgumentException(String.format("Invalid length: %s, expected: %s.", «paramName», «IF isNestedType»«returnType.importedName».«ENDIF»length()));
- }
- }
- '''
-
def protected copyConstructor() '''
/**
* Creates a copy from Source Object.
* @return string with the class attributes in JAVA format
*/
def protected generateFields() '''
- «IF restrictions != null»
- «val prop = getPropByName("value")»
- «IF prop != null»
- «IF !(restrictions.lengthConstraints.empty)»
- private static final «List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _length;
- «ENDIF»
- «ENDIF»
- «ENDIF»
«IF !properties.empty»
«FOR f : properties»
private«IF f.readOnly» final«ENDIF» «f.returnType.importedName» «f.fieldName»;