I have the following code:
public interface Person {
/***
*@Throws Exception x must be greater than 0 ****/
setAge(int x);
}
public class Man implements Person {
setAge(int x) {
if(x <= 0) thrown new Exception("x <= ");
}
}
I broke the DRY principle because I repeat the check in every single implementation and the documentation repeats it too. What's the best way to check the arguments in this situation?
Given that it's an interface, and interfaces in Java 7 and lower can't contain any implementation, then you've got a design decision to make. Do you require the rigidity of the interface, or can you do with an abstract class?
If you can go with an abstract class for this method, then I'd recommend doing so, as it will make implementing (and retrieving) the value you care about a bit simpler.
With the above implementation, you don't have to implement the method in child classes, and you won't run the risk of repeating yourself.