36
36
*/
37
37
public final class Require {
38
38
39
+ private static final String ARG_MUST_BE_SET = "%s must be set" ;
40
+ private static final String MUST_EXIST = "%s must exist: %s" ;
41
+ private static final String MUST_BE_DIR = "%s must be a directory: %s" ;
42
+ private static final String MUST_BE_FILE = "%s must be a regular file: %s" ;
43
+
39
44
private Require () {
40
45
// An utility class
41
46
}
@@ -48,14 +53,15 @@ public static void precondition(boolean condition, String message, Object... arg
48
53
49
54
public static <T > T nonNull (String argName , T arg ) {
50
55
if (arg == null ) {
51
- throw new IllegalArgumentException (argName + " must be set" );
56
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
52
57
}
53
58
return arg ;
54
59
}
55
60
56
61
public static <T > T nonNull (String argName , T arg , String message , Object ... args ) {
57
62
if (arg == null ) {
58
- throw new IllegalArgumentException (String .join (" " , argName , String .format (message , args )));
63
+ throw new IllegalArgumentException (
64
+ String .join (" " , argName , String .format (message , args )));
59
65
}
60
66
return arg ;
61
67
}
@@ -76,7 +82,7 @@ public static class ArgumentChecker<T> {
76
82
77
83
public T nonNull () {
78
84
if (arg == null ) {
79
- throw new IllegalArgumentException (argName + " must be set" );
85
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
80
86
}
81
87
return arg ;
82
88
}
@@ -90,7 +96,7 @@ public T nonNull(String message, Object... args) {
90
96
91
97
public T equalTo (Object other ) {
92
98
if (arg == null ) {
93
- throw new IllegalArgumentException (argName + " must be set" );
99
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
94
100
}
95
101
if (!Objects .equals (arg , other )) {
96
102
throw new IllegalArgumentException (argName + " must be equal to `" + other + "`" );
@@ -100,7 +106,7 @@ public T equalTo(Object other) {
100
106
101
107
public T instanceOf (Class <?> cls ) {
102
108
if (arg == null ) {
103
- throw new IllegalArgumentException (argName + " must be set" );
109
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
104
110
}
105
111
if (!cls .isInstance (arg )) {
106
112
throw new IllegalArgumentException (argName + " must be an instance of " + cls );
@@ -111,7 +117,7 @@ public T instanceOf(Class<?> cls) {
111
117
112
118
public static Duration nonNegative (String argName , Duration arg ) {
113
119
if (arg == null ) {
114
- throw new IllegalArgumentException (argName + " must be set" );
120
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
115
121
}
116
122
if (arg .isNegative ()) {
117
123
throw new IllegalArgumentException (argName + " must be set to 0 or more" );
@@ -131,16 +137,17 @@ public static Duration nonNegative(Duration arg) {
131
137
132
138
public static int nonNegative (String argName , Integer number ) {
133
139
if (number == null ) {
134
- throw new IllegalArgumentException (argName + " must be set" );
140
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
135
141
}
136
142
if (number < 0 ) {
137
143
throw new IllegalArgumentException (argName + " cannot be less than 0" );
138
144
}
139
145
return number ;
140
146
}
147
+
141
148
public static int positive (String argName , Integer number , String message ) {
142
149
if (number == null ) {
143
- throw new IllegalArgumentException (argName + " must be set" );
150
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
144
151
}
145
152
if (number <= 0 ) {
146
153
if (message == null ) {
@@ -172,7 +179,7 @@ public static class IntChecker {
172
179
173
180
public int greaterThan (int max , String message ) {
174
181
if (number == null ) {
175
- throw new IllegalArgumentException (argName + " must be set" );
182
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
176
183
}
177
184
if (number <= max ) {
178
185
throw new IllegalArgumentException (message );
@@ -197,26 +204,30 @@ public static class FileChecker {
197
204
198
205
public File isFile () {
199
206
if (file == null ) {
200
- throw new IllegalArgumentException (argName + " must be set" );
207
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
201
208
}
202
209
if (!file .exists ()) {
203
- throw new IllegalArgumentException (argName + " must exist: " + file .getAbsolutePath ());
210
+ throw new IllegalArgumentException (
211
+ String .format (MUST_EXIST , argName , file .getAbsolutePath ()));
204
212
}
205
213
if (!file .isFile ()) {
206
- throw new IllegalArgumentException (argName + " must be a regular file: " + file .getAbsolutePath ());
214
+ throw new IllegalArgumentException (
215
+ String .format (MUST_BE_FILE , argName , file .getAbsolutePath ()));
207
216
}
208
217
return file ;
209
218
}
210
219
211
220
public File isDirectory () {
212
221
if (file == null ) {
213
- throw new IllegalArgumentException (argName + " must be set" );
222
+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
214
223
}
215
224
if (!file .exists ()) {
216
- throw new IllegalArgumentException (argName + " must exist: " + file .getAbsolutePath ());
225
+ throw new IllegalArgumentException (
226
+ String .format (MUST_EXIST , argName , file .getAbsolutePath ()));
217
227
}
218
228
if (!file .isDirectory ()) {
219
- throw new IllegalArgumentException (argName + " must be a directory: " + file .getAbsolutePath ());
229
+ throw new IllegalArgumentException (
230
+ String .format (MUST_BE_DIR , argName , file .getAbsolutePath ()));
220
231
}
221
232
return file ;
222
233
}
@@ -258,7 +269,7 @@ public T nonNull(String message, Object... args) {
258
269
259
270
public T instanceOf (Class <?> cls ) {
260
271
if (state == null ) {
261
- throw new IllegalStateException (name + " must be set" );
272
+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
262
273
}
263
274
if (!cls .isInstance (state )) {
264
275
throw new IllegalStateException (name + " must be an instance of " + cls );
@@ -283,26 +294,26 @@ public static class FileStateChecker {
283
294
284
295
public File isFile () {
285
296
if (file == null ) {
286
- throw new IllegalStateException (name + " must be set" );
297
+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
287
298
}
288
299
if (!file .exists ()) {
289
- throw new IllegalStateException (name + " must exist: " + file .getAbsolutePath ());
300
+ throw new IllegalStateException (String . format ( MUST_EXIST , name , file .getAbsolutePath () ));
290
301
}
291
302
if (!file .isFile ()) {
292
- throw new IllegalStateException (name + " must be a regular file: " + file .getAbsolutePath ());
303
+ throw new IllegalStateException (String . format ( MUST_BE_FILE , name , file .getAbsolutePath () ));
293
304
}
294
305
return file ;
295
306
}
296
307
297
308
public File isDirectory () {
298
309
if (file == null ) {
299
- throw new IllegalStateException (name + " must be set" );
310
+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
300
311
}
301
312
if (!file .exists ()) {
302
- throw new IllegalStateException (name + " must exist: " + file .getAbsolutePath ());
313
+ throw new IllegalStateException (String . format ( MUST_EXIST , name , file .getAbsolutePath () ));
303
314
}
304
315
if (!file .isDirectory ()) {
305
- throw new IllegalStateException (name + " must be a directory: " + file .getAbsolutePath ());
316
+ throw new IllegalStateException (String . format ( MUST_BE_DIR , name , file .getAbsolutePath () ));
306
317
}
307
318
return file ;
308
319
}
@@ -324,26 +335,26 @@ public static class PathStateChecker {
324
335
325
336
public Path isFile () {
326
337
if (path == null ) {
327
- throw new IllegalStateException (name + " must be set" );
338
+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
328
339
}
329
340
if (!Files .exists (path )) {
330
- throw new IllegalStateException (name + " must exist: " + path );
341
+ throw new IllegalStateException (String . format ( MUST_EXIST , name , path ) );
331
342
}
332
343
if (!Files .isRegularFile (path )) {
333
- throw new IllegalStateException (name + " must be a regular file: " + path );
344
+ throw new IllegalStateException (String . format ( MUST_BE_FILE , name , path ) );
334
345
}
335
346
return path ;
336
347
}
337
348
338
349
public Path isDirectory () {
339
350
if (path == null ) {
340
- throw new IllegalStateException (name + " must be set" );
351
+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
341
352
}
342
353
if (!Files .exists (path )) {
343
- throw new IllegalStateException (name + " must exist: " + path );
354
+ throw new IllegalStateException (String . format ( MUST_EXIST , name , path ) );
344
355
}
345
356
if (!Files .isDirectory (path )) {
346
- throw new IllegalStateException (name + " must be a directory: " + path );
357
+ throw new IllegalStateException (String . format ( MUST_BE_DIR , name , path ) );
347
358
}
348
359
return path ;
349
360
}
0 commit comments