@@ -164,10 +164,15 @@ public static IntChecker argument(String argName, Integer number) {
164
164
return new IntChecker (argName , number );
165
165
}
166
166
167
+ @ Deprecated (forRemoval = true )
167
168
public static FileChecker argument (String argName , File file ) {
168
169
return new FileChecker (argName , file );
169
170
}
170
171
172
+ public static PathChecker argument (String argName , Path path ) {
173
+ return new PathChecker (argName , path );
174
+ }
175
+
171
176
public static void stateCondition (boolean state , String message , Object ... args ) {
172
177
if (!state ) {
173
178
throw new IllegalStateException (String .format (message , args ));
@@ -178,6 +183,7 @@ public static <T> StateChecker<T> state(String name, T state) {
178
183
return new StateChecker <>(name , state );
179
184
}
180
185
186
+ @ Deprecated (forRemoval = true )
181
187
public static FileStateChecker state (String name , File file ) {
182
188
return new FileStateChecker (name , file );
183
189
}
@@ -252,6 +258,7 @@ public int greaterThan(int max, String message) {
252
258
}
253
259
}
254
260
261
+ @ Deprecated (forRemoval = true )
255
262
public static class FileChecker {
256
263
257
264
private final String argName ;
@@ -293,6 +300,47 @@ public File isDirectory() {
293
300
}
294
301
}
295
302
303
+ public static class PathChecker {
304
+
305
+ private final String argName ;
306
+ private final Path path ;
307
+
308
+ PathChecker (String argName , Path path ) {
309
+ this .argName = argName ;
310
+ this .path = path ;
311
+ }
312
+
313
+ public Path isFile () {
314
+ if (path == null ) {
315
+ throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
316
+ }
317
+ if (!Files .exists (path )) {
318
+ throw new IllegalArgumentException (
319
+ String .format (MUST_EXIST , argName , path .toAbsolutePath ()));
320
+ }
321
+ if (!Files .isRegularFile (path )) {
322
+ throw new IllegalArgumentException (
323
+ String .format (MUST_BE_FILE , argName , path .toAbsolutePath ()));
324
+ }
325
+ return path ;
326
+ }
327
+
328
+ public Path isDirectory () {
329
+ if (path == null ) {
330
+ throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
331
+ }
332
+ if (!Files .exists (path )) {
333
+ throw new IllegalArgumentException (
334
+ String .format (MUST_EXIST , argName , path .toAbsolutePath ()));
335
+ }
336
+ if (!Files .isDirectory (path )) {
337
+ throw new IllegalArgumentException (
338
+ String .format (MUST_BE_DIR , argName , path .toAbsolutePath ()));
339
+ }
340
+ return path ;
341
+ }
342
+ }
343
+
296
344
public static class StateChecker <T > {
297
345
298
346
private final String name ;
@@ -328,6 +376,7 @@ public T instanceOf(Class<?> cls) {
328
376
}
329
377
}
330
378
379
+ @ Deprecated (forRemoval = true )
331
380
public static class FileStateChecker {
332
381
333
382
private final String name ;
@@ -365,7 +414,12 @@ public File isDirectory() {
365
414
}
366
415
367
416
public File isExecutable () {
368
- isFile ();
417
+ if (file == null ) {
418
+ throw new IllegalStateException (String .format (MUST_BE_SET , name ));
419
+ }
420
+ if (!file .exists ()) {
421
+ throw new IllegalStateException (String .format (MUST_EXIST , name , file .getAbsolutePath ()));
422
+ }
369
423
if (!file .canExecute ()) {
370
424
throw new IllegalStateException (
371
425
String .format (MUST_BE_EXECUTABLE , name , file .getAbsolutePath ()));
@@ -409,5 +463,20 @@ public Path isDirectory() {
409
463
}
410
464
return path ;
411
465
}
466
+
467
+ public Path isExecutable () {
468
+ if (path == null ) {
469
+ throw new IllegalStateException (String .format (MUST_BE_SET , name ));
470
+ }
471
+ if (!Files .exists (path )) {
472
+ throw new IllegalStateException (String .format (MUST_EXIST , name , path ));
473
+ }
474
+ // do not check for isRegularFile here, there are executable none regular files e.g. Windows
475
+ // app execution aliases
476
+ if (!Files .isExecutable (path )) {
477
+ throw new IllegalStateException (String .format (MUST_BE_EXECUTABLE , name , path ));
478
+ }
479
+ return path ;
480
+ }
412
481
}
413
482
}
0 commit comments