Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String [] strings){
- ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
- Validator validator = validatorFactory.getValidator();
- Set<ConstraintViolation<User>> constraints = validator.validate(new User("Stas","StasPassword","Stas.Stanis88@gmail.com"));
- for (ConstraintViolation<User> constraint:constraints) {
- System.out.println(constraint.getMessage());
- }
- public class User {
- public String getFirstName() {
- return firstName;
- }
- private void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- private void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getPassword() {
- return password;
- }
- private void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- private void setEmail(String email) {
- this.email = email;
- }
- public String getCustomerType() {
- return customerType;
- }
- private void setCustomerType(String customerType) {
- this.customerType = customerType;
- }
- public String getNickName() {
- return nickName;
- }
- private void setNickName(String nickName) {
- this.nickName = nickName;
- }
- public User(){}
- public User(String nickName,String password, String email){
- setEmail(email);
- setNickName(nickName);
- setPassword(password);
- }
- public User(String firstName, String lastName, String nickName ,String email, String password, String customerType) {
- setCustomerType(customerType);
- setEmail(email);
- setFirstName(firstName);
- setLastName(lastName);
- setNickName(nickName);
- setPassword(password);
- }
- @Override
- public String toString() {
- return "User{" +
- "firstName='" + firstName + ''' +
- ", lastName='" + lastName + ''' +
- ", password='" + password + ''' +
- ", email='" + email + ''' +
- ", customerType='" + customerType + ''' +
- ", nickName='" + nickName + ''' +
- '}';
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- User user = (User) o;
- if (!getFirstName().equals(user.getFirstName())) return false;
- if (!getLastName().equals(user.getLastName())) return false;
- if (!getPassword().equals(user.getPassword())) return false;
- if (!getEmail().equals(user.getEmail())) return false;
- if (!getCustomerType().equals(user.getCustomerType())) return false;
- return getNickName().equals(user.getNickName());
- }
- @Override
- public int hashCode() {
- 107: int result = getFirstName().hashCode(); // Проблемма возникает здесь!
- result = 31 * result + getLastName().hashCode();
- result = 31 * result + getPassword().hashCode();
- result = 31 * result + getEmail().hashCode();
- result = 31 * result + getCustomerType().hashCode();
- result = 31 * result + getNickName().hashCode();
- return result;
- }
- @NotEmpty(message = "This field can't be empty")
- @NotBlank(message = "This field can't be empty")
- @Length(min = 0,max = 20,message = "The length of this field can be only 0-20")
- private String firstName;
- @NotEmpty(message = "This field can't be empty")
- @NotBlank(message = "This field can't be empty")
- @Length(min = 0,max = 20,message = "The length of this field can be only 0-20")
- private String lastName;
- @NotEmpty(message = "This field can't be empty")
- @NotBlank(message = "This field can't be empty")
- @Length(min = 8,max = 16,message = "Wrong password length: length of this field can be only 8-16")
- private String password;
- @NotEmpty(message = "This field can't be empty")
- @NotBlank(message = "This field can't be empty")
- @Email(message = "Wrong format for email field!" , regexp = "[A-Za-z.0-9]+@[A-Za-z.]+\.[a-z]{2,4}")
- private String email;
- @NotEmpty(message = "This field can't be empty")
- @NotBlank(message = "This field can't be empty")
- private String customerType;
- @NotEmpty(message = "This field can't be empty")
- @NotBlank(message = "This field can't be empty")
- @Length(min = 4,max = 10,message = "Wrong nickName: The length of this field can be only between 4-10")
- private String nickName;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement