@@ -164,10 +164,15 @@ public static IntChecker argument(String argName, Integer number) {
164164 return new IntChecker (argName , number );
165165 }
166166
167+ @ Deprecated (forRemoval = true )
167168 public static FileChecker argument (String argName , File file ) {
168169 return new FileChecker (argName , file );
169170 }
170171
172+ public static PathChecker argument (String argName , Path path ) {
173+ return new PathChecker (argName , path );
174+ }
175+
171176 public static void stateCondition (boolean state , String message , Object ... args ) {
172177 if (!state ) {
173178 throw new IllegalStateException (String .format (message , args ));
@@ -178,6 +183,7 @@ public static <T> StateChecker<T> state(String name, T state) {
178183 return new StateChecker <>(name , state );
179184 }
180185
186+ @ Deprecated (forRemoval = true )
181187 public static FileStateChecker state (String name , File file ) {
182188 return new FileStateChecker (name , file );
183189 }
@@ -252,6 +258,7 @@ public int greaterThan(int max, String message) {
252258 }
253259 }
254260
261+ @ Deprecated (forRemoval = true )
255262 public static class FileChecker {
256263
257264 private final String argName ;
@@ -293,6 +300,47 @@ public File isDirectory() {
293300 }
294301 }
295302
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+
296344 public static class StateChecker <T > {
297345
298346 private final String name ;
@@ -328,6 +376,7 @@ public T instanceOf(Class<?> cls) {
328376 }
329377 }
330378
379+ @ Deprecated (forRemoval = true )
331380 public static class FileStateChecker {
332381
333382 private final String name ;
@@ -365,7 +414,12 @@ public File isDirectory() {
365414 }
366415
367416 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+ }
369423 if (!file .canExecute ()) {
370424 throw new IllegalStateException (
371425 String .format (MUST_BE_EXECUTABLE , name , file .getAbsolutePath ()));
@@ -409,5 +463,20 @@ public Path isDirectory() {
409463 }
410464 return path ;
411465 }
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+ }
412481 }
413482}
0 commit comments