Reputation: 1347
I'm building a library, and some of my methods have parameters that should be within a specific range. For example, in this method, button
should be between 1 and 3 inclusive:
public static void pressMouse(int button){
try{
Robot robot = new Robot();
if(button == 1){
robot.mousePress(InputEvent.BUTTON1_MASK);
}else if(button == 2){
robot.mousePress(InputEvent.BUTTON3_MASK);
}else{
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
}
}catch(Exception e){
}
}
Is there a way I can throw a compile-time error if the value isn't valid? E.g.
TestLib.pressMouse(4);
I could just throw an exception inside the method, but if theres a nicer way that would be cool.
Upvotes: 1
Views: 906
Reputation: 95598
You cannot throw a compile-time error for things which can only be known at runtime; in this particular case the values of your method's arguments are only known at runtime*. What you can do is establish method preconditions. Something like Preconditions
from Google Guava can do this for you:
Preconditions.checkArgument(button > 0 && button <= 3, "Button number must be between 1 and 3");
Elliott Frisch's answer is another way you can accomplish this, however it still depends on the your button
value coming from a known source. That is, whoever is supplying the button
value must also return it as the ButtonState
enum
. If it's not, you will have to convert it over to ButtonState
which gives rise to the same problem as before: you cannot guarantee that you will get a legal value (unless your source specifies that it will always return legal values).
* This is theoretically possible in certain languages. For example, in Ada you can specify a sub-type of integer that is only valid for certain ranges. The compiler can then emit a compile-type error if it turns out that this method is being called using an unconstrained integer-type. Java's type-system is not expressive enough to do this (at compile-time anyway). However, it is required that you are working with a value that is known to be within this range. If you are getting the value from a random source, there is no guarantee that it will be within the range you specify. Its value can only be known at runtime. However the compiler can at least warn you that this is a possibility. In Java, the best way to guard against this is just to use method preconditions. However, if you are working with a known-subset of values and you control both the source and destination of those values, then an enum
is perfect for this.
Upvotes: 1
Reputation: 201537
Not as you have written it. However, one way to add that kind of compile time type checking is with an Enum
Type
public enum ButtonState {
ONE, TWO, THREE;
public void update(Robot robot) {
if (robot == null) return;
if (this == ONE) {
robot.mousePress(InputEvent.BUTTON1_MASK);
} else if (this == TWO) {
robot.mousePress(InputEvent.BUTTON3_MASK);
} else {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
}
}
}
public static void pressMouse(ButtonState button) {
try {
Robot robot = new Robot();
button.update(robot);
} catch(Exception e) {
e.printStackTrace();
}
}
Upvotes: 3