+ def private constantsDeclarations() '''
+ «FOR c : type.getConstantDefinitions»
+ «IF c.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
+ «val cValue = c.value as Map<String, String>»
+ «val String fieldSuffix = c.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length)»
+ «IF cValue.size == 1»
+ private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST»«fieldSuffix» = «Pattern.importedName».compile("«cValue.keySet.get(0).escapeJava»");
+ private static final String «Constants.MEMBER_REGEX_LIST»«fieldSuffix» = "«cValue.values.get(0).escapeJava»";
+ «ELSE»
+ private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST»«fieldSuffix» = «CodeHelpers.importedName».compilePatterns(«ImmutableList.importedName».of(
+ «FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»));
+ private static final String[] «Constants.MEMBER_REGEX_LIST»«fieldSuffix» = { «
+ FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
+ «ENDIF»
+ «ELSE»
+ «emitConstant(c)»
+ «ENDIF»
+ «ENDFOR»
+ '''
+
+ def private generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
+ «IF restrictions.rangeConstraint.present»
+ «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
+ restrictions.rangeConstraint.get, this)»
+ «ENDIF»
+ «IF restrictions.lengthConstraint.present»
+ «LengthGenerator.generateLengthChecker(field.fieldName.toString, actualType, restrictions.lengthConstraint.get, this)»
+ «ENDIF»
+ '''
+
+ def private checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType) '''
+ «IF restrictions.getRangeConstraint.isPresent»
+ «IF actualType instanceof ConcreteType»
+ «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, "value")»
+ «ELSE»
+ «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, "value.getValue()")»
+ «ENDIF»
+ «ENDIF»
+ «IF restrictions.getLengthConstraint.isPresent»
+ «IF actualType instanceof ConcreteType»
+ «LengthGenerator.generateLengthCheckerCall(property.fieldName.toString, "value")»
+ «ELSE»
+ «LengthGenerator.generateLengthCheckerCall(property.fieldName.toString, "value.getValue()")»
+ «ENDIF»
+ «ENDIF»
+
+ «val fieldUpperCase = property.fieldName.toString.toUpperCase()»
+ «FOR currentConstant : type.getConstantDefinitions»
+ «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
+ && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
+ «CodeHelpers.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«property.fieldName», «Constants.MEMBER_REGEX_LIST»«property.fieldName»);
+ «ENDIF»
+ «ENDFOR»
+ '''
+
+ def private Restrictions restrictionsForSetter(Type actualType) {
+ if (actualType instanceof GeneratedType) {
+ return null;
+ }
+ return actualType.restrictions;
+ }
+
+ def private generateListSetter(GeneratedProperty field, Type actualType) '''
+ «val restrictions = restrictionsForSetter(actualType)»
+ «IF restrictions !== null»
+ «generateCheckers(field, restrictions, actualType)»
+ «ENDIF»
+ public «type.getName»Builder set«field.getName.toFirstUpper»(final «field.returnType.importedName» values) {
+ «IF restrictions !== null»
+ if (values != null) {
+ for («actualType.getFullyQualifiedName» value : values) {
+ «checkArgument(field, restrictions, actualType)»
+ }
+ }
+ «ENDIF»
+ this.«field.fieldName.toString» = values;
+ return this;
+ }
+
+ '''
+
+ def private generateSetter(GeneratedProperty field, Type actualType) '''
+ «val restrictions = restrictionsForSetter(actualType)»
+ «IF restrictions !== null»
+ «generateCheckers(field, restrictions, actualType)»
+ «ENDIF»
+
+ public «type.getName»Builder set«field.getName.toFirstUpper»(final «field.returnType.importedName» value) {
+ «IF restrictions !== null»
+ if (value != null) {
+ «checkArgument(field, restrictions, actualType)»
+ }
+ «ENDIF»
+ this.«field.fieldName.toString» = value;
+ return this;
+ }
+ '''
+
+ private def Type getActualType(ParameterizedType ptype) {
+ return ptype.getActualTypeArguments.get(0)
+ }
+