private String getMinAndMaxBody(final FieldMetadata field,
final String suffix) {
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final AnnotationMetadata minAnnotation = MemberFindingUtils
.getAnnotationOfType(field.getAnnotations(), MIN);
final AnnotationMetadata maxAnnotation = MemberFindingUtils
.getAnnotationOfType(field.getAnnotations(), MAX);
if (minAnnotation != null && maxAnnotation == null) {
final Number minValue = (Number) minAnnotation.getAttribute(VALUE)
.getValue();
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName
+ ".compareTo(new " + BIG_INTEGER.getSimpleTypeName()
+ "(\"" + minValue + "\")) == -1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new "
+ BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue
+ "\");");
}
else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < "
+ minValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + minValue
+ suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
else if (minAnnotation == null && maxAnnotation != null) {
final Number maxValue = (Number) maxAnnotation.getAttribute(VALUE)
.getValue();
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName
+ ".compareTo(new " + BIG_INTEGER.getSimpleTypeName()
+ "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new "
+ BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue
+ "\");");
}
else {
bodyBuilder.appendFormalLine("if (" + fieldName + " > "
+ maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue
+ suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
else if (minAnnotation != null && maxAnnotation != null) {
final Number minValue = (Number) minAnnotation.getAttribute(VALUE)
.getValue();
final Number maxValue = (Number) maxAnnotation.getAttribute(VALUE)
.getValue();
Validate.isTrue(
maxValue.longValue() >= minValue.longValue(),
"The value of @Max must be greater or equal to the value of @Min for field %s",
fieldName);
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName
+ ".compareTo(new " + BIG_INTEGER.getSimpleTypeName()
+ "(\"" + minValue + "\")) == -1 || " + fieldName
+ ".compareTo(new " + BIG_INTEGER.getSimpleTypeName()
+ "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new "
+ BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue
+ "\");");
}
else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < "
+ minValue + suffix + " || " + fieldName + " > "
+ maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue
+ suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
return bodyBuilder.getOutput();
}