net.sf.aguacate.function.spi.impl.FunctionZero Maven / Gradle / Ivy
package net.sf.aguacate.function.spi.impl;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.Collection;
import net.sf.aguacate.function.FunctionEvalResult;
import net.sf.aguacate.function.Parameter;
import net.sf.aguacate.function.spi.AbstractFunction1;
public class FunctionZero extends AbstractFunction1 {
public FunctionZero(Collection methods, String name, String message, Parameter parameter) {
super(methods, name, message, parameter, null);
}
@Override
protected FunctionEvalResult evaluate(Object value) {
String msg = MessageFormat.format(getMessage(), value);
if (value.getClass() == BigInteger.class) {
if (BigInteger.ZERO.equals(value)) {
logSuccess(msg);
return new FunctionEvalResult(null, null);
} else {
logFailure(msg);
return new FunctionEvalResult("Not Zero", null);
}
} else {
if (value.getClass() == BigDecimal.class) {
BigDecimal val = (BigDecimal) value;
BigDecimal zero = BigDecimal.ZERO.setScale(val.scale());
if (zero.equals(val)) {
logSuccess(msg);
return new FunctionEvalResult(null, null);
} else {
logFailure(msg);
return new FunctionEvalResult("Not Zero", null);
}
} else {
logFailure(msg);
return new FunctionEvalResult("Not implemented", null);
}
}
}
}