-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationResult.java
More file actions
47 lines (34 loc) · 929 Bytes
/
ValidationResult.java
File metadata and controls
47 lines (34 loc) · 929 Bytes
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
package com.sample.javaValidators;
import java.util.Optional;
public interface ValidationResult {
static ValidationResult valid() {
return ValidationSupport.valid();
}
static ValidationResult invalid(String reason) {
return new Invalid(reason);
}
boolean isValid();
Optional<String> getReason();
}
final class Invalid implements ValidationResult {
private final String reason;
Invalid(String reason){
this.reason = reason;
}
public boolean isValid(){
return false;
}
public Optional<String> getReason(){
return Optional.of(reason);
}
// equals and hashCode on field reason
}
final class ValidationSupport {
private static final ValidationResult valid = new ValidationResult(){
public boolean isValid(){ return true; }
public Optional<String> getReason(){ return Optional.empty(); }
};
static ValidationResult valid(){
return valid;
}
}