What is the best way in Java to enforce class instance invariants (i.e. to ensure that certain statements are true right before and after calling any public method)?
I will include below an example (the one that has made me wonder about this question), but I am in fact more interested in a general solution for imposing class-instance invariants.
Let's say I have the following classes:
public class Constraint
{
private int cardinality;
// constructor and getter/setter omitted
}
public class Edge
{
private int minCardinality = 1;
private int maxCardinality = Integer.MAX_VALUE;
private Constraint constraint = null;
// constructors, getters/setters and other methods omitted
}
and that I'd like to enforce the following statements on the Edge class:
- 0 <= minCardinality <= maxCardinality
- if (constraint != null) then (minCardinality <= constraint.getCardinality() <= maxCardinality)
Here Edge instances are created by a parser with (obligatory) attributes minCardinality and maxCardinality set. The (optional) constraint attribute is left blank. The instances are then handed over to another module (a test frame generator) which might or might not set the constraint via a setter method.