Consider you got a class with a constructor like the follwing
public class Clazz {
protected BigDecimal bigDecimal;
public Clazz(BigDecimal bigDecimal) {
this.bigDecimal= bigDecimal;
}
public BigDecimal getBigDecimal() {
return bigDecimal;
}
}
Clazz takes only BigDecimals which are at least zero, that is
new Clazz(BigDecimalUtils.minZero(someBigDecimal));
Who is responsible to ensure that the class only gets instantiated with BigDecimals greater than zero? Is it the class itself (e.g. in the constructor)?
It's whomever you want to be, generally I'd have something akin to:
public void setBigDecimal(BigDecimal aBigDecimal) {
if (aBigDecimal.longValue() > 0l) {
this.bigDecimal = aBigDecimal;
} else { throw new IllegalArgumentException("positive numbers only!");
}
And then have your constructor leverage the above method:
public Clazz(BigDecimal b) {
this.setBigDecimal(b);
}
I hope this is clear, do leave a comment if you have further questions.