2222import java .nio .file .Path ;
2323import java .time .Duration ;
2424import java .util .Objects ;
25+ import org .jspecify .annotations .NullMarked ;
26+ import org .jspecify .annotations .Nullable ;
2527
2628/**
2729 * A utility class to check arguments (preconditions) and state.
3537 * }
3638 * </pre>
3739 */
40+ @ NullMarked
3841public final class Require {
3942
4043 private static final String MUST_BE_SET = "%s must be set" ;
@@ -56,25 +59,25 @@ public static void precondition(boolean condition, String message, Object... arg
5659 }
5760 }
5861
59- public static <T > T nonNull (String argName , T arg ) {
62+ public static <T > T nonNull (String argName , @ Nullable T arg ) {
6063 if (arg == null ) {
6164 throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
6265 }
6366 return arg ;
6467 }
6568
66- public static <T > T nonNull (String argName , T arg , String message , Object ... args ) {
69+ public static <T > T nonNull (String argName , @ Nullable T arg , String message , Object ... args ) {
6770 if (arg == null ) {
6871 throw new IllegalArgumentException (String .join (" " , argName , String .format (message , args )));
6972 }
7073 return arg ;
7174 }
7275
73- public static <T > ArgumentChecker <T > argument (String argName , T arg ) {
76+ public static <T > ArgumentChecker <T > argument (String argName , @ Nullable T arg ) {
7477 return new ArgumentChecker <>(argName , arg );
7578 }
7679
77- public static Duration nonNegative (String argName , Duration arg ) {
80+ public static Duration nonNegative (String argName , @ Nullable Duration arg ) {
7881 if (arg == null ) {
7982 throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
8083 }
@@ -84,7 +87,7 @@ public static Duration nonNegative(String argName, Duration arg) {
8487 return arg ;
8588 }
8689
87- public static Duration nonNegative (Duration arg ) {
90+ public static Duration nonNegative (@ Nullable Duration arg ) {
8891 if (arg == null ) {
8992 throw new IllegalArgumentException (String .format (MUST_BE_SET , "Duration" ));
9093 }
@@ -94,7 +97,7 @@ public static Duration nonNegative(Duration arg) {
9497 return arg ;
9598 }
9699
97- public static Duration positive (String argName , Duration arg ) {
100+ public static Duration positive (String argName , @ Nullable Duration arg ) {
98101 if (arg == null ) {
99102 throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
100103 }
@@ -104,7 +107,7 @@ public static Duration positive(String argName, Duration arg) {
104107 return arg ;
105108 }
106109
107- public static Duration positive (Duration arg ) {
110+ public static Duration positive (@ Nullable Duration arg ) {
108111 if (arg == null ) {
109112 throw new IllegalArgumentException (String .format (MUST_BE_SET , "Duration" ));
110113 }
@@ -114,7 +117,7 @@ public static Duration positive(Duration arg) {
114117 return arg ;
115118 }
116119
117- public static int nonNegative (String argName , Integer number ) {
120+ public static int nonNegative (String argName , @ Nullable Integer number ) {
118121 if (number == null ) {
119122 throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
120123 }
@@ -124,7 +127,7 @@ public static int nonNegative(String argName, Integer number) {
124127 return number ;
125128 }
126129
127- public static int positive (String argName , Integer number , String message ) {
130+ public static int positive (String argName , @ Nullable Integer number , @ Nullable String message ) {
128131 if (number == null ) {
129132 throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
130133 }
@@ -135,7 +138,7 @@ public static int positive(String argName, Integer number, String message) {
135138 return number ;
136139 }
137140
138- public static double positive (String argName , Double number , String message ) {
141+ public static double positive (String argName , @ Nullable Double number , @ Nullable String message ) {
139142 if (number == null ) {
140143 throw new IllegalArgumentException (String .format (MUST_BE_SET , argName ));
141144 }
@@ -146,24 +149,24 @@ public static double positive(String argName, Double number, String message) {
146149 return number ;
147150 }
148151
149- public static double positive (String argName , Double number ) {
152+ public static double positive (String argName , @ Nullable Double number ) {
150153 return positive (argName , number , null );
151154 }
152155
153- public static int positive (String argName , Integer number ) {
156+ public static int positive (String argName , @ Nullable Integer number ) {
154157 return positive (argName , number , null );
155158 }
156159
157- public static IntChecker argument (String argName , Integer number ) {
160+ public static IntChecker argument (String argName , @ Nullable Integer number ) {
158161 return new IntChecker (argName , number );
159162 }
160163
161164 @ Deprecated (forRemoval = true )
162- public static FileChecker argument (String argName , File file ) {
165+ public static FileChecker argument (String argName , @ Nullable File file ) {
163166 return new FileChecker (argName , file );
164167 }
165168
166- public static PathChecker argument (String argName , Path path ) {
169+ public static PathChecker argument (String argName , @ Nullable Path path ) {
167170 return new PathChecker (argName , path );
168171 }
169172
@@ -173,25 +176,25 @@ public static void stateCondition(boolean state, String message, Object... args)
173176 }
174177 }
175178
176- public static <T > StateChecker <T > state (String name , T state ) {
179+ public static <T > StateChecker <T > state (String name , @ Nullable T state ) {
177180 return new StateChecker <>(name , state );
178181 }
179182
180183 @ Deprecated (forRemoval = true )
181- public static FileStateChecker state (String name , File file ) {
184+ public static FileStateChecker state (String name , @ Nullable File file ) {
182185 return new FileStateChecker (name , file );
183186 }
184187
185- public static PathStateChecker state (String name , Path path ) {
188+ public static PathStateChecker state (String name , @ Nullable Path path ) {
186189 return new PathStateChecker (name , path );
187190 }
188191
189192 public static class ArgumentChecker <T > {
190193
191194 private final String argName ;
192- private final T arg ;
195+ private final @ Nullable T arg ;
193196
194- ArgumentChecker (String argName , T arg ) {
197+ ArgumentChecker (String argName , @ Nullable T arg ) {
195198 this .argName = argName ;
196199 this .arg = arg ;
197200 }
@@ -234,9 +237,9 @@ public T instanceOf(Class<?> cls) {
234237 public static class IntChecker {
235238
236239 private final String argName ;
237- private final Integer number ;
240+ private final @ Nullable Integer number ;
238241
239- IntChecker (String argName , Integer number ) {
242+ IntChecker (String argName , @ Nullable Integer number ) {
240243 this .argName = argName ;
241244 this .number = number ;
242245 }
@@ -256,9 +259,9 @@ public int greaterThan(int max, String message) {
256259 public static class FileChecker {
257260
258261 private final String argName ;
259- private final File file ;
262+ private final @ Nullable File file ;
260263
261- FileChecker (String argName , File file ) {
264+ FileChecker (String argName , @ Nullable File file ) {
262265 this .argName = argName ;
263266 this .file = file ;
264267 }
@@ -297,9 +300,9 @@ public File isDirectory() {
297300 public static class PathChecker {
298301
299302 private final String argName ;
300- private final Path path ;
303+ private final @ Nullable Path path ;
301304
302- PathChecker (String argName , Path path ) {
305+ PathChecker (String argName , @ Nullable Path path ) {
303306 this .argName = argName ;
304307 this .path = path ;
305308 }
@@ -338,9 +341,9 @@ public Path isDirectory() {
338341 public static class StateChecker <T > {
339342
340343 private final String name ;
341- private final T state ;
344+ private final @ Nullable T state ;
342345
343- StateChecker (String name , T state ) {
346+ StateChecker (String name , @ Nullable T state ) {
344347 this .name = name ;
345348 this .state = state ;
346349 }
@@ -374,9 +377,9 @@ public T instanceOf(Class<?> cls) {
374377 public static class FileStateChecker {
375378
376379 private final String name ;
377- private final File file ;
380+ private final @ Nullable File file ;
378381
379- FileStateChecker (String name , File file ) {
382+ FileStateChecker (String name , @ Nullable File file ) {
380383 this .name = name ;
381384 this .file = file ;
382385 }
@@ -425,9 +428,9 @@ public File isExecutable() {
425428 public static class PathStateChecker {
426429
427430 private final String name ;
428- private final Path path ;
431+ private final @ Nullable Path path ;
429432
430- PathStateChecker (String name , Path path ) {
433+ PathStateChecker (String name , @ Nullable Path path ) {
431434 this .name = name ;
432435 this .path = path ;
433436 }
0 commit comments