forked from estraier/tkrzw-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.java
More file actions
195 lines (176 loc) · 5.75 KB
/
Status.java
File metadata and controls
195 lines (176 loc) · 5.75 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*************************************************************************************************
* Status interface
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
package tkrzw;
/**
* Status of operations
*/
public class Status {
/**
* Enumeration of status codes.
*/
public enum Code {
/** Success. */
SUCCESS(0),
/** Generic error whose cause is unknown. */
UNKNOWN_ERROR(1),
/** Generic error from underlying systems. */
SYSTEM_ERROR(2),
/** Error that the feature is not implemented. */
NOT_IMPLEMENTED_ERROR(3),
/** Error that a precondition is not met. */
PRECONDITION_ERROR(4),
/** Error that a given argument is invalid. */
INVALID_ARGUMENT_ERROR(5),
/** Error that the operation is canceled. */
CANCELED_ERROR(6),
/** Error that a specific resource is not found. */
NOT_FOUND_ERROR(7),
/** Error that the operation is not permitted. */
PERMISSION_ERROR(8),
/** Error that the operation is infeasible. */
INFEASIBLE_ERROR(9),
/** Error that a specific resource is duplicated. */
DUPLICATION_ERROR(10),
/** Error that internal data are broken. */
BROKEN_DATA_ERROR(11),
/** Generic error caused by the application logic. */
APPLICATION_ERROR(12);
/**
* Constructor to set the ID value.
*/
private Code(int id) {
id_ = id;
}
/** The ID value. */
private int id_;
}
/** Status code: Success. */
public static final Code SUCCESS = Code.SUCCESS;
/** Status code: Generic error whose cause is unknown. */
public static final Code UNKNOWN_ERROR = Code.UNKNOWN_ERROR;
/** Status code: Generic error from underlying systems. */
public static final Code SYSTEM_ERROR = Code.SYSTEM_ERROR;
/** Status code: Error that the feature is not implemented. */
public static final Code NOT_IMPLEMENTED_ERROR = Code.NOT_IMPLEMENTED_ERROR;
/** Status code: Error that a precondition is not met. */
public static final Code PRECONDITION_ERROR = Code.PRECONDITION_ERROR;
/** Status code: Error that a given argument is invalid. */
public static final Code INVALID_ARGUMENT_ERROR = Code.INVALID_ARGUMENT_ERROR;
/** Status code: Error that the operation is canceled. */
public static final Code CANCELED_ERROR = Code.CANCELED_ERROR;
/** Status code: Error that a specific resource is not found. */
public static final Code NOT_FOUND_ERROR = Code.NOT_FOUND_ERROR;
/** Status code: Error that the operation is not permitted. */
public static final Code PERMISSION_ERROR = Code.PERMISSION_ERROR;
/** Status code: Error that the operation is infeasible. */
public static final Code INFEASIBLE_ERROR = Code.INFEASIBLE_ERROR;
/** Status code: Error that a specific resource is duplicated. */
public static final Code DUPLICATION_ERROR = Code.DUPLICATION_ERROR;
/** Status code: Error that internal data are broken. */
public static final Code BROKEN_DATA_ERROR = Code.BROKEN_DATA_ERROR;
/** Status code: Generic error caused by the application logic. */
public static final Code APPLICATION_ERROR = Code.APPLICATION_ERROR;
/**
* Constructor representing the success code.
*/
public Status() {
code_ = SUCCESS;
message_ = "";
}
/**
* Constructor representing a specific status.
* @param code The status code.
* @param message An arbitrary status message.
*/
public Status(Code code, String message) {
code_ = code;
message_ = message;
}
/**
* Gets the status code.
* @return The status code.
*/
public Code getCode() {
return code_;
}
/**
* Gets the status message.
* @return The status message.
*/
public String getMessage() {
return message_;
}
/**
* Sets the code and the message.
* @param code The status code.
* @param message An arbitrary status message.
*/
public void set(Code code, String message) {
code_ = code;
message_ = message;
}
/**
* Gets the string expression.
* @return The string expression.
*/
public String toString() {
if (message_.isEmpty()) {
return code_.name();
}
return code_.name() + ": " + message_;
}
/**
* Checks equality.
* @param rht a status object.
* @return true for the both operands are equal, or false if not.
*/
public boolean equals(Status rht) {
return rht.code_ == code_;
}
/**
* Checks equality.
* @param rht a status code.
* @return true for the both operands are equal, or false if not.
*/
public boolean equals(Code rht) {
return rht == code_;
}
/**
* Clones the object.
* @return The clone object.
*/
public Status clone() {
return new Status(code_, message_);
}
/**
* Returns true if the status is success.
* @return True if the status is success, or false if not.
*/
public boolean isOK() {
return code_ == SUCCESS;
}
/**
* Throws an exception if the status is not success.
* @throws StatusException The exception object containing the status.
*/
public void orDie() {
if (code_ != SUCCESS) {
throw new StatusException(this);
}
}
/** The status code. */
private Code code_;
/** The status message. */
private String message_;
}
// END OF FILE