-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathValidationResult.java
More file actions
108 lines (84 loc) · 2.65 KB
/
ValidationResult.java
File metadata and controls
108 lines (84 loc) · 2.65 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.uprotocol.validation;
import org.eclipse.uprotocol.v1.UStatus;
import org.eclipse.uprotocol.v1.UCode;
import java.util.Objects;
/**
* Class wrapping a ValidationResult of success or failure wrapping the value of
* a google.rpc.Status.
*/
public abstract class ValidationResult {
public static final UStatus STATUS_SUCCESS = UStatus.newBuilder().setCode(UCode.OK).setMessage("OK").build();
private static final ValidationResult SUCCESS = new Success();
private ValidationResult() {
}
public abstract UStatus toStatus();
public abstract boolean isSuccess();
public boolean isFailure() {
return !isSuccess();
}
public abstract String getMessage();
/**
* Implementation for failure, wrapping the message.
*/
private static class Failure extends ValidationResult {
private final String message;
private Failure(String message) {
this.message = Objects.requireNonNullElse(message, "Validation Failed.");
}
@Override
public UStatus toStatus() {
return UStatus.newBuilder().setCode(UCode.INVALID_ARGUMENT).setMessage(message).build();
}
@Override
public boolean isSuccess() {
return false;
}
@Override
public String getMessage() {
return message;
}
@Override
public String toString() {
return "ValidationResult.Failure(" + "message='" + message + '\'' + ')';
}
}
/**
* Implementation for success, wrapping a UStatus with Code 0 for success.
*/
private static class Success extends ValidationResult {
@Override
public UStatus toStatus() {
return STATUS_SUCCESS;
}
@Override
public boolean isSuccess() {
return true;
}
@Override
public String getMessage() {
return "";
}
@Override
public String toString() {
return "ValidationResult.Success()";
}
}
public static ValidationResult success() {
return SUCCESS;
}
public static ValidationResult failure(String message) {
return new Failure(message);
}
}