Skip to content

Instantly share code, notes, and snippets.

@codingtim
Last active September 13, 2016 07:15
Show Gist options
  • Select an option

  • Save codingtim/37a82623dbfbe61a7bb264fe71522689 to your computer and use it in GitHub Desktop.

Select an option

Save codingtim/37a82623dbfbe61a7bb264fe71522689 to your computer and use it in GitHub Desktop.
Target with validation
public class Target {
private static final Pattern ALLOWED_REGEX = Pattern.compile("[a-zA-Z\\-_]*");
private String code;
private String name;
private String publicIdentifier;
public Target(String code, String name, String publicIdentifier) {
validate(code, "code");
validate(name, "name");
validate(publicIdentifier, "publicIdentifier");
validatePublicIdentifier(publicIdentifier);
this.code = code;
this.name = name;
this.publicIdentifier = publicIdentifier;
}
private void validatePublicIdentifier(String publicIdentifier) {
if(!ALLOWED_REGEX.matcher(publicIdentifier).matches()) {
throw new IllegalArgumentException("Public identifier should only contains valid characters.");
}
}
private void validate(String value, String parameterName) {
if(value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException(parameterName + " should not be empty.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment