Last active
September 13, 2016 07:15
-
-
Save codingtim/37a82623dbfbe61a7bb264fe71522689 to your computer and use it in GitHub Desktop.
Target with validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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