PageRenderTime 460ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/net/some/kruglov/hiring/validator/Validator.java

https://bitbucket.org/ikonstantine/somehiringproject
Java | 45 lines | 35 code | 10 blank | 0 comment | 20 complexity | a04fdd2925ceea86b4e8898d52e20172 MD5 | raw file
  1. package net.some.kruglov.hiring.validator;
  2. import java.util.regex.Pattern;
  3. public class Validator {
  4. private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  5. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  6. private static final Pattern VALID_PASSWORD_REGEX =
  7. Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$", Pattern.CASE_INSENSITIVE);
  8. public static void validateEmail(String email) throws ValidatorException {
  9. if (email == null) throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_EMAIL);
  10. if (email.equals("") || !VALID_EMAIL_ADDRESS_REGEX.matcher(email).find())
  11. throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_EMAIL);
  12. }
  13. public static void validatePassword(String password) throws ValidatorException {
  14. if (password == null) throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_PASSWORD);
  15. if (password.equals("")) throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_PASSWORD);
  16. if (!VALID_PASSWORD_REGEX.matcher(password).find())
  17. throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_PASSWORD);
  18. }
  19. public static void validateName(String fullName) throws ValidatorException {
  20. if (fullName == null) throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_NAME);
  21. if (fullName.equals("") || fullName.length() <= 1) throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_NAME);
  22. }
  23. public static void validateLogin(String login) throws ValidatorException {
  24. if (login == null) throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_LOGIN);
  25. if (login.equals("") || login.length() < 5)
  26. throw new ValidatorException(ValidatorErrorCode.NOT_VALIDATE_LOGIN);
  27. }
  28. public static void equalsHashKeys(String hashKey1, String hashKey2) throws ValidatorException {
  29. if (!hashKey1.equals(hashKey2)) throw new ValidatorException(ValidatorErrorCode.WRONG_PASSWORD);
  30. }
  31. public static void validateToken(String token) throws ValidatorException {
  32. if (token == null) throw new ValidatorException(ValidatorErrorCode.TOKEN_IS_EMPTY);
  33. if (token.equals("")) throw new ValidatorException(ValidatorErrorCode.TOKEN_IS_EMPTY);
  34. }
  35. }