HoseinPanahi
HoseinPanahi

Reputation: 591

Java annotation validation

How to validate a property that should be grater than zero?and not zero.I used built in annotation @Min(0) but how can i ignore the zero?Is there any other built in annotation for this case?

@Min(0)
default public Double getAmd1() {
    return (Double) get("amd1");
}

Upvotes: 0

Views: 650

Answers (1)

sinclair
sinclair

Reputation: 2861

Check out the @DecimalMin annotation It provides a boolean parameter inclusive which fits your needs. Sadly Double is not supported because of rounding issues, but BigDecimal is. You can then get the value as double by using getDouble().

So you can try something like this:

@DecimalMin(value = "0.0" ,inclusive = false)
private BigDecimal amd1;

public BigDecimal getAmd1() {
    Double d = (Double) get("amd1");
    return BigDecimal.valueOf(d);
}

Upvotes: 3

Related Questions