/
NonEmptyString.java
43 lines (34 loc) · 1.16 KB
/
NonEmptyString.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
package nu.tengstrand.stateguard.validator;
import nu.tengstrand.stateguard.Validatable;
import nu.tengstrand.stateguard.ValidationMessages;
public class NonEmptyString implements Validatable {
private String string;
private final String attributeName;
private String messageKey;
protected NonEmptyString(String attributeName) {
this.attributeName = attributeName;
}
public static NonEmptyString attributeName(String attributeName) {
return new NonEmptyString(attributeName);
}
public NonEmptyString setValue(String value) {
this.string = value;
return this;
}
public NonEmptyString messageKey(String messageKey) {
this.messageKey = messageKey;
return this;
}
public String value() {
return string;
}
public boolean isValid() {
return string != null && string.length() > 0;
}
public ValidationMessages validationMessages() {
if (isValid()) {
return ValidationMessages.withoutMessage();
}
return ValidationMessages.message("Attribute ''{0}'' can not be empty").arguments(attributeName).messageKey(messageKey);
}
}