final AnnotationMetadata decimalMinAnnotation,
final AnnotationMetadata decimalMaxAnnotation, final String suffix) {
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
if (decimalMinAnnotation != null && decimalMaxAnnotation == null) {
final String minValue = (String) decimalMinAnnotation.getAttribute(
VALUE).getValue();
if (fieldType.equals(BIG_DECIMAL)) {
bodyBuilder.appendFormalLine("if (" + fieldName
+ ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName()
+ "(\"" + minValue + "\")) == -1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new "
+ BIG_DECIMAL.getSimpleTypeName() + "(\"" + minValue
+ "\");");
}
else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < "
+ minValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + minValue
+ suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
else if (decimalMinAnnotation == null && decimalMaxAnnotation != null) {
final String maxValue = (String) decimalMaxAnnotation.getAttribute(
VALUE).getValue();
if (fieldType.equals(BIG_DECIMAL)) {
bodyBuilder.appendFormalLine("if (" + fieldName
+ ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName()
+ "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new "
+ BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue
+ "\");");
}
else {
bodyBuilder.appendFormalLine("if (" + fieldName + " > "
+ maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue
+ suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
else if (decimalMinAnnotation != null && decimalMaxAnnotation != null) {
final String minValue = (String) decimalMinAnnotation.getAttribute(
VALUE).getValue();
final String maxValue = (String) decimalMaxAnnotation.getAttribute(
VALUE).getValue();
Validate.isTrue(
Double.parseDouble(maxValue) >= Double
.parseDouble(minValue),
"The value of @DecimalMax must be greater or equal to the value of @DecimalMin for field %s",
fieldName);
if (fieldType.equals(BIG_DECIMAL)) {
bodyBuilder.appendFormalLine("if (" + fieldName
+ ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName()
+ "(\"" + minValue + "\")) == -1 || " + fieldName
+ ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName()
+ "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new "
+ BIG_DECIMAL.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();
}