| 1 | package io.github.marcopaglio.booking.validator.restricted; | |
| 2 | ||
| 3 | import java.util.regex.Pattern; | |
| 4 | ||
| 5 | import io.github.marcopaglio.booking.validator.ClientValidator; | |
| 6 | ||
| 7 | /** | |
| 8 | * An implementation of validator for client entities that verifies if parameters are not null | |
| 9 | * or empty string neither they contain non-alphabetic characters, and fixes them if possible. | |
| 10 | * | |
| 11 | * @see <a href="../../model/Client.html">Client</a> | |
| 12 | */ | |
| 13 | public class RestrictedClientValidator implements ClientValidator { | |
| 14 | /** | |
| 15 | * Regular expression for stating other characters except the alphabetic ones and horizontal spaces. | |
| 16 | */ | |
| 17 | private static final Pattern notOnlyAlphabetic = Pattern.compile("[^\\p{IsAlphabetic}\\h]"); | |
| 18 | ||
| 19 | /** | |
| 20 | * Checks if firstName is valid as name for a client entity, and returns | |
| 21 | * a fixed and valid alternative for it. | |
| 22 | * | |
| 23 | * @param firstName the string parameter to evaluate. | |
| 24 | * @return a fixed and valid {@code String} of {@code firstName}. | |
| 25 | * @throws IllegalArgumentException if {@code firstName} is null or not valid. | |
| 26 | */ | |
| 27 | @Override | |
| 28 | public String validateFirstName(String firstName) throws IllegalArgumentException { | |
| 29 |
1
1. validateFirstName : replaced return value with "" for io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::validateFirstName → KILLED |
return checkAndFixNameValidity(firstName, "name"); |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * Checks if lastName is valid as surname for a client entity, and returns | |
| 34 | * a fixed and valid alternative for it. | |
| 35 | * | |
| 36 | * @param lastName the string parameter to evaluate. | |
| 37 | * @return a fixed and valid {@code String} of {@code lastName}. | |
| 38 | * @throws IllegalArgumentException if {@code lastName} is null or not valid. | |
| 39 | */ | |
| 40 | @Override | |
| 41 | public String validateLastName(String lastName) throws IllegalArgumentException { | |
| 42 |
1
1. validateLastName : replaced return value with "" for io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::validateLastName → KILLED |
return checkAndFixNameValidity(lastName, "surname"); |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Checks if name is valid as name/surname for a client entity, and returns | |
| 47 | * a fixed and valid alternative for it. | |
| 48 | * | |
| 49 | * @param name the string parameter to evaluate. | |
| 50 | * @param inputName the role of {@code name} in the client's context. | |
| 51 | * @return a fixed and valid {@code String} of {@code name}. | |
| 52 | * @throws IllegalArgumentException if {@code name} is null or not valid. | |
| 53 | */ | |
| 54 | private String checkAndFixNameValidity(String name, String inputName) | |
| 55 | throws IllegalArgumentException { | |
| 56 |
1
1. checkAndFixNameValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::checkNameValidity → KILLED |
checkNameValidity(name, inputName); |
| 57 |
1
1. checkAndFixNameValidity : replaced return value with "" for io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::checkAndFixNameValidity → KILLED |
return removeExcessedSpaces(name); |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Checks if the string is a valid name that is not null neither empty, | |
| 62 | * and contains only alphabetic and horizontal whitespace characters. | |
| 63 | * | |
| 64 | * @param name the string to evaluate. | |
| 65 | * @param inputName the role of the string in the client's context. | |
| 66 | * @throws IllegalArgumentException if {@code name} is a null or empty string, | |
| 67 | * or it contains non-alphabetic characters. | |
| 68 | */ | |
| 69 | private void checkNameValidity(String name, String inputName) throws IllegalArgumentException { | |
| 70 |
1
1. checkNameValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::checkNotNull → KILLED |
checkNotNull(name, inputName); |
| 71 |
1
1. checkNameValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::checkNotEmpty → KILLED |
checkNotEmpty(name, inputName); |
| 72 |
1
1. checkNameValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::checkOnlyAlphabetic → KILLED |
checkOnlyAlphabetic(name, inputName); |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Checks if the object is not null. | |
| 77 | * | |
| 78 | * @param o the object to evaluate. | |
| 79 | * @param inputName the role of the object in the client's context. | |
| 80 | * @throws IllegalArgumentException if {@code o} is null. | |
| 81 | */ | |
| 82 | private void checkNotNull(Object o, String inputName) throws IllegalArgumentException { | |
| 83 |
1
1. checkNotNull : removed conditional - replaced equality check with false → KILLED |
if (o == null) |
| 84 | throw new IllegalArgumentException( | |
| 85 | "Client needs a not null " + inputName + "."); | |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Checks if the string is not empty. | |
| 90 | * | |
| 91 | * @param str the string to evaluate. | |
| 92 | * @param inputName the role of the string in the client's context. | |
| 93 | * @throws IllegalArgumentException if {@code str} is empty. | |
| 94 | */ | |
| 95 | private void checkNotEmpty(String str, String inputName) { | |
| 96 |
1
1. checkNotEmpty : removed conditional - replaced equality check with false → KILLED |
if (str.trim().isEmpty()) |
| 97 | throw new IllegalArgumentException( | |
| 98 | "Client needs a non-empty " + inputName + "."); | |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Checks if the string contains only accepted characters that is | |
| 103 | * (lower and upper) alphabetic and accented letters, and horizontal whitespace character. | |
| 104 | * | |
| 105 | * @param str the string to evaluate. | |
| 106 | * @param inputName the role of the string in the client's context. | |
| 107 | * @throws IllegalArgumentException if {@code str} contains non-valid characters. | |
| 108 | */ | |
| 109 | private void checkOnlyAlphabetic(String str, String inputName) | |
| 110 | throws IllegalArgumentException { | |
| 111 |
1
1. checkOnlyAlphabetic : removed conditional - replaced equality check with false → KILLED |
if (notOnlyAlphabetic.matcher(str).find()) |
| 112 | throw new IllegalArgumentException( | |
| 113 | "Client's " + inputName + " can contain only alphabet letters."); | |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Removes side spaces and reduces multiple spaces into a single whitespace. | |
| 118 | * | |
| 119 | * @param name the name to modify. | |
| 120 | * @return the {@code String} name modified. | |
| 121 | */ | |
| 122 | private String removeExcessedSpaces(String name) { | |
| 123 |
1
1. removeExcessedSpaces : replaced return value with "" for io/github/marcopaglio/booking/validator/restricted/RestrictedClientValidator::removeExcessedSpaces → KILLED |
return name.trim().replaceAll("\\s+", " "); |
| 124 | } | |
| 125 | } | |
Mutations | ||
| 29 |
1.1 |
|
| 42 |
1.1 |
|
| 56 |
1.1 |
|
| 57 |
1.1 |
|
| 70 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |
|
| 83 |
1.1 |
|
| 96 |
1.1 |
|
| 111 |
1.1 |
|
| 123 |
1.1 |