Reputation:
I was doing some reading here, about creating immutable classes, and I noticed they use a private method to check to make sure that the parameters being passed are indeed correct, like this:
private void check(int red,
int green,
int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
}
My issue is, I have the following class, but when I throw an exception, I want to specify to the user which argument was invalid. Do I have to write a private method for each parameter and check that way?
final public class AnonymousCilent {
final private String anonymousCilentID;
final private String anonymousCilentFirstName;
final private String anonymousCilentLastName;
public AnonymousCilent(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName) {
this.anonymousCilentID = anonymousCilentID;
this.anonymousCilentFirstName = anonymousCilentFirstName;
this.anonymousCilentLastName = anonymousCilentLastName;
}
/*
private void checkParameter(String check){
if(check == null || check.length() < 0 ){
throw new IllegalArgumentException ("Please ensure all values are provided");
}
}
*/
public String getAnonymousCilentFirstName() {
return anonymousCilentFirstName;
}
public String getAnonymousCilentLastName() {
return anonymousCilentLastName;
}
Upvotes: 1
Views: 88
Reputation: 207006
You could create a helper method:
private String checkNotNullOrEmpty(String s, String name) {
if (s == null || s.isEmpty()) {
throw new IllegalArgumentException(name + " must not be null or empty");
}
return s;
}
And then use it like this in your constructor:
public AnonymousCilent(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName,
String gender, Date arrivalDate, String immStatus, Date registrationDate,
String registrationSite, String siteName, String comments) {
this.anonymousCilentID = checkNotNullOrEmpty(anonymousCilentID, "anonymousCilentID");
this.anonymousCilentFirstName = checkNotNullOrEmpty(anonymousCilentFirstName, "anonymousCilentFirstName");
this.anonymousCilentLastName = checkNotNullOrEmpty(anonymousCilentLastName, "anonymousCilentLastName");
// etc.
}
Note that the class java.util.Objects
(Java 8) already contains similar helper methods.
Also the Google Guava library has a class named Preconditions
with similar helper methods.
Similar for the example with color values:
public int checkColorValue(int value, String name) {
if (value < 0 || value > 255) {
throw new IllegalArgumentException(name + " must be between 0 and 255");
}
return value;
}
public Color(int red, int green, int blue) {
this.red = checkColorValue(red, "red");
this.green = checkColorValue(green, "green");
this.blue = checkColorValue(blue, "blue");
}
Upvotes: 2
Reputation: 8137
You have 2 options and neither is easier than doing it manually in my opinion.
you can use a framework were you annotate your class data members with rules and use a validator to validate the input. You can do this with hibernate: http://hibernate.org/validator/documentation/getting-started/ (Good for very simple rules)
If you have really complex business logic and other stuff going on you can do a really heavy solution and embedd a prolog interpretor or other logic engine they are sometimes called. (Good for very very complex rules)
99.9% of the time you have neither and you are best of minimizing the chance of users entering invalid input by using the apropriate GUI components and code in you View rather than having exceptions in your code, or writting the functionality yourself. I find it easier to make the asumption a user always enters in valid input and make sure that happens in the GUI, however this is not always possible. If you have multiple cases sometimes an enum switch statment can do wonders:
enum CASE{ VALID, DUPLICATENAME, INVALIDADDRESS, ECT }
Then have a switch statement to handle each case.
Upvotes: 0
Reputation: 77234
You don't have to have separate methods, but you need to separate out the different reasons you might throw the exceptions. For example, you might have multiple if
blocks:
if(red < 0 || red > 255) {
throw new IllegalArgumentException("value for red must be in the range 0-255");
}
if(blue < 0 || blue > 255) {
...
Note that depending on whether your program is already complex enough to be using external libraries, it may make more sense to use the JSR-303 Validation API:
public final class ImmutableClass {
@NotEmpty
final String clientID;
@Min(0)
@Max(255)
final int red;
}
Upvotes: 1