-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLengthRequirement.java
64 lines (51 loc) · 2.11 KB
/
LengthRequirement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.jcp.automation.common.requirements;
import com.jcp.automation.common.ErrorCodes;
/**
* @author ykrasnopolskiy
* @since 3/19/17.
*/
public class LengthRequirement extends AbstractRequirement {
private Integer minNameLength;
private Integer maxNameLength;
public int getMinNameLength() {
return minNameLength;
}
public void setMinNameLength(Integer minNameLength) {
validateArguments(minNameLength, maxNameLength);
this.minNameLength = minNameLength;
}
public int getMaxNameLength() {
return maxNameLength;
}
public void setMaxNameLength(int maxNameLength) {
validateArguments(minNameLength, maxNameLength);
this.maxNameLength = maxNameLength;
}
/**
* The constructor is planned to be used only to instantiate Requirement inside Requirements enum
*/
LengthRequirement(ErrorCodes associatedErrorIfNotMeetRequirement){
setErrorCode(associatedErrorIfNotMeetRequirement);
}
LengthRequirement(int minNameLength, int maxNameLength, ErrorCodes associatedErrorIfNotMeetRequirement){
validateArguments(minNameLength, maxNameLength);
this.minNameLength = minNameLength;
this.maxNameLength = maxNameLength;
setErrorCode(associatedErrorIfNotMeetRequirement);
}
private void validateArguments(Integer minNameLength, Integer maxNameLength){
if (minNameLength != null && maxNameLength != null && minNameLength>maxNameLength)
throw new RuntimeException("Minimum value of the length have to be less or equal that the maximum one");
}
@Override
public boolean meets(Object objectToCheck) {
boolean result = true;
if (objectToCheck == null){
if (minNameLength != null || maxNameLength != null) result = false;
} else {
result = (minNameLength == null || objectToCheck.toString().length() >= minNameLength)
&& (maxNameLength == null || objectToCheck.toString().length() <= maxNameLength);
}
return result;
}
}