if (expression.trim().startsWith(TRIGGER) == false) {
return null;
}
Matcher matcher = PATTERN.matcher(expression);
if (matcher.matches() == false) {
throw new FormatException(MessageFormat.format(
"Invalid approx(~) expression: {0}",
name));
}
String sign = matcher.group(1);
boolean plus = sign == null || sign.equals(SIGN_PLUS);
boolean minus = sign == null || sign.equals(SIGN_MINUS);
String magnitude = matcher.group(2).trim();
switch (type) {
case BYTE:
case SHORT:
case INT:
case LONG:
try {
long value = Long.parseLong(magnitude);
return Predicates.integerRange(minus ? -value : 0, plus ? +value : 0);
} catch (NumberFormatException e) {
throw new FormatException(MessageFormat.format(
"Invalid approx(~) error \"{1}\": {0}",
name,
magnitude), e);
}
case FLOAT:
case DOUBLE:
try {
double value = Double.parseDouble(magnitude);
return Predicates.floatRange(minus ? -value : 0, plus ? +value : 0);
} catch (NumberFormatException e) {
throw new FormatException(MessageFormat.format(
"Invalid approx(~) value \"{1}\": {0}",
name,
magnitude), e);
}
case DECIMAL:
try {
BigDecimal value = new BigDecimal(magnitude);
return Predicates.decimalRange(
minus ? value.negate() : BigDecimal.ZERO,
plus ? value : BigDecimal.ZERO);
} catch (NumberFormatException e) {
throw new FormatException(MessageFormat.format(
"Invalid approx(~) error \"{1}\": {0}",
name,
magnitude), e);
}
case DATE:
case DATETIME:
try {
int value = Integer.parseInt(magnitude);
if (type == PropertyType.DATE) {
return Predicates.dateRange(minus ? -value : 0, plus ? +value : 0);
} else {
return Predicates.timeRange(minus ? -value : 0, plus ? +value : 0);
}
} catch (NumberFormatException e) {
throw new FormatException(MessageFormat.format(
"Invalid approx(~) error \"{1}\": {0}",
name,
magnitude), e);
}
default:
throw new FormatException(MessageFormat.format(
"Property does not support approx(~) expression: {0}",
name));
}
}