I want to add two conditions in Invariant:
- All Natural methods must guarantee (via PreconditionErrors or InvariantErrors) that operations resulting in an overflow are blocked
- Ensure that no method throws an exception other than PreconditionError or InvariantError.
Following is my code so far for the implementation. I am new to these java contracts so struggling to make it through.
//Add an invariant here.
@Invariant({
"data >= 0",
"data <= Integer.MAX_VALUE",
"noUnexpectedExceptions()"
})
public class Natural implements Comparable<Natural> {
private int data;
private boolean noUnexpectedExceptions() {
try {
assert true;
return true;
} catch (PreconditionError | InvariantError e) {
return true;
} catch (Throwable t) {
return false;
}
}
public void increment() {
data++;
}
public void decrement() {
data--;
}
}