com.osomapps.pt.tokenemail.EmailValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 86.36 %

Importance

Changes 0
Metric Value
wmc 5
eloc 18
dl 19
loc 22
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports(Class) 3 3 1
A validate(Object,Errors) 7 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package com.osomapps.pt.tokenemail;
2
3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5
import org.springframework.stereotype.Component;
6
import org.springframework.validation.Errors;
7
import org.springframework.validation.Validator;
8
9 View Code Duplication
@Component
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
public class EmailValidator implements Validator {
11
    private static final Pattern EMAIL_PATTERN =
12
            Pattern.compile(
13
                    "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@"
14
                            + "((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$");
15
16
    @Override
17
    public boolean supports(final Class<?> aClass) {
18
        return String.class.equals(aClass);
19
    }
20
21
    @Override
22
    public void validate(final Object obj, final Errors errors) {
23
        final String email = (String) obj;
24
        if (email == null || email.trim().isEmpty()) {
25
            errors.reject("email", "Invalid empty email address");
26
            return;
27
        }
28
        final Matcher matcher = EMAIL_PATTERN.matcher(email);
29
        if (!matcher.matches()) {
30
            errors.reject("email", "Invalid email address (" + email + ")");
31
        }
32
    }
33
}
34