principal-ideal-domain :
I want my constructor to throw an IllegalArgumentException
with a self written error message when it is called with wrong arguments.
public Card(int top, int left, int right) {
this(TOP.get(top) + 3 * LEFT.get(left) + 9 * RIGHT.get(right));
}
Wrong arguments mean that TOP.get(top)
(resp. LEFT, RIGHT) is null since the Map<Integer, Integer>
doesn't contain such an element. The problem is that I can't test for that first since this()
doesn't allow code before.
Stefan :
Add a helper method that checks the input for you:
public Card(int top, int left, int right) {
this(helper(top, left, right));
}
private static int helper(int top, int left, int right) {
if (TOP.get(top) == null || LEFT.get(left) == null || RIGHT.get(right) == null)) {
throw new IllegalArgumentException();
}
return TOP.get(top) + 3 * LEFT.get(left) + 9 * RIGHT.get(right);
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=355517&siteId=1