3636 */
3737public final class Require {
3838
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+
3944 private Require () {
4045 // An utility class
4146 }
@@ -48,14 +53,15 @@ public static void precondition(boolean condition, String message, Object... arg
4853
4954 public static <T > T nonNull (String argName , T arg ) {
5055 if (arg == null ) {
51- throw new IllegalArgumentException (argName + " must be set" );
56+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
5257 }
5358 return arg ;
5459 }
5560
5661 public static <T > T nonNull (String argName , T arg , String message , Object ... args ) {
5762 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 )));
5965 }
6066 return arg ;
6167 }
@@ -76,7 +82,7 @@ public static class ArgumentChecker<T> {
7682
7783 public T nonNull () {
7884 if (arg == null ) {
79- throw new IllegalArgumentException (argName + " must be set" );
85+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
8086 }
8187 return arg ;
8288 }
@@ -90,7 +96,7 @@ public T nonNull(String message, Object... args) {
9096
9197 public T equalTo (Object other ) {
9298 if (arg == null ) {
93- throw new IllegalArgumentException (argName + " must be set" );
99+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
94100 }
95101 if (!Objects .equals (arg , other )) {
96102 throw new IllegalArgumentException (argName + " must be equal to `" + other + "`" );
@@ -100,7 +106,7 @@ public T equalTo(Object other) {
100106
101107 public T instanceOf (Class <?> cls ) {
102108 if (arg == null ) {
103- throw new IllegalArgumentException (argName + " must be set" );
109+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
104110 }
105111 if (!cls .isInstance (arg )) {
106112 throw new IllegalArgumentException (argName + " must be an instance of " + cls );
@@ -111,7 +117,7 @@ public T instanceOf(Class<?> cls) {
111117
112118 public static Duration nonNegative (String argName , Duration arg ) {
113119 if (arg == null ) {
114- throw new IllegalArgumentException (argName + " must be set" );
120+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
115121 }
116122 if (arg .isNegative ()) {
117123 throw new IllegalArgumentException (argName + " must be set to 0 or more" );
@@ -131,16 +137,17 @@ public static Duration nonNegative(Duration arg) {
131137
132138 public static int nonNegative (String argName , Integer number ) {
133139 if (number == null ) {
134- throw new IllegalArgumentException (argName + " must be set" );
140+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
135141 }
136142 if (number < 0 ) {
137143 throw new IllegalArgumentException (argName + " cannot be less than 0" );
138144 }
139145 return number ;
140146 }
147+
141148 public static int positive (String argName , Integer number , String message ) {
142149 if (number == null ) {
143- throw new IllegalArgumentException (argName + " must be set" );
150+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
144151 }
145152 if (number <= 0 ) {
146153 if (message == null ) {
@@ -172,7 +179,7 @@ public static class IntChecker {
172179
173180 public int greaterThan (int max , String message ) {
174181 if (number == null ) {
175- throw new IllegalArgumentException (argName + " must be set" );
182+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
176183 }
177184 if (number <= max ) {
178185 throw new IllegalArgumentException (message );
@@ -197,26 +204,30 @@ public static class FileChecker {
197204
198205 public File isFile () {
199206 if (file == null ) {
200- throw new IllegalArgumentException (argName + " must be set" );
207+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
201208 }
202209 if (!file .exists ()) {
203- throw new IllegalArgumentException (argName + " must exist: " + file .getAbsolutePath ());
210+ throw new IllegalArgumentException (
211+ String .format (MUST_EXIST , argName , file .getAbsolutePath ()));
204212 }
205213 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 ()));
207216 }
208217 return file ;
209218 }
210219
211220 public File isDirectory () {
212221 if (file == null ) {
213- throw new IllegalArgumentException (argName + " must be set" );
222+ throw new IllegalArgumentException (String . format ( ARG_MUST_BE_SET , argName ) );
214223 }
215224 if (!file .exists ()) {
216- throw new IllegalArgumentException (argName + " must exist: " + file .getAbsolutePath ());
225+ throw new IllegalArgumentException (
226+ String .format (MUST_EXIST , argName , file .getAbsolutePath ()));
217227 }
218228 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 ()));
220231 }
221232 return file ;
222233 }
@@ -258,7 +269,7 @@ public T nonNull(String message, Object... args) {
258269
259270 public T instanceOf (Class <?> cls ) {
260271 if (state == null ) {
261- throw new IllegalStateException (name + " must be set" );
272+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
262273 }
263274 if (!cls .isInstance (state )) {
264275 throw new IllegalStateException (name + " must be an instance of " + cls );
@@ -283,26 +294,26 @@ public static class FileStateChecker {
283294
284295 public File isFile () {
285296 if (file == null ) {
286- throw new IllegalStateException (name + " must be set" );
297+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
287298 }
288299 if (!file .exists ()) {
289- throw new IllegalStateException (name + " must exist: " + file .getAbsolutePath ());
300+ throw new IllegalStateException (String . format ( MUST_EXIST , name , file .getAbsolutePath () ));
290301 }
291302 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 () ));
293304 }
294305 return file ;
295306 }
296307
297308 public File isDirectory () {
298309 if (file == null ) {
299- throw new IllegalStateException (name + " must be set" );
310+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
300311 }
301312 if (!file .exists ()) {
302- throw new IllegalStateException (name + " must exist: " + file .getAbsolutePath ());
313+ throw new IllegalStateException (String . format ( MUST_EXIST , name , file .getAbsolutePath () ));
303314 }
304315 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 () ));
306317 }
307318 return file ;
308319 }
@@ -324,26 +335,26 @@ public static class PathStateChecker {
324335
325336 public Path isFile () {
326337 if (path == null ) {
327- throw new IllegalStateException (name + " must be set" );
338+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
328339 }
329340 if (!Files .exists (path )) {
330- throw new IllegalStateException (name + " must exist: " + path );
341+ throw new IllegalStateException (String . format ( MUST_EXIST , name , path ) );
331342 }
332343 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 ) );
334345 }
335346 return path ;
336347 }
337348
338349 public Path isDirectory () {
339350 if (path == null ) {
340- throw new IllegalStateException (name + " must be set" );
351+ throw new IllegalStateException (String . format ( ARG_MUST_BE_SET , name ) );
341352 }
342353 if (!Files .exists (path )) {
343- throw new IllegalStateException (name + " must exist: " + path );
354+ throw new IllegalStateException (String . format ( MUST_EXIST , name , path ) );
344355 }
345356 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 ) );
347358 }
348359 return path ;
349360 }
0 commit comments