Skip to content

Commit 29e0759

Browse files
committed
[java] Reducing duplicated strings in Require.java
[skip ci]
1 parent 50f808a commit 29e0759

1 file changed

Lines changed: 89 additions & 86 deletions

File tree

java/src/org/openqa/selenium/internal/Require.java

Lines changed: 89 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@
3636
*/
3737
public final class Require {
3838

39-
private static final String ARG_MUST_BE_SET = "%s must be set";
39+
private static final String MUST_BE_SET = "%s must be set";
4040
private static final String MUST_EXIST = "%s must exist: %s";
4141
private static final String MUST_BE_DIR = "%s must be a directory: %s";
4242
private static final String MUST_BE_FILE = "%s must be a regular file: %s";
43+
private static final String MUST_BE_EQUAL = "%s must be equal to: %s";
44+
private static final String MUST_BE_NON_NEGATIVE = "%s must be 0 or greater";
45+
private static final String MUST_BE_POSITIVE = "%s must greater than 0";
4346

4447
private Require() {
4548
// An utility class
@@ -53,7 +56,7 @@ public static void precondition(boolean condition, String message, Object... arg
5356

5457
public static <T> T nonNull(String argName, T arg) {
5558
if (arg == null) {
56-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
59+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
5760
}
5861
return arg;
5962
}
@@ -70,88 +73,43 @@ public static <T> ArgumentChecker<T> argument(String argName, T arg) {
7073
return new ArgumentChecker<>(argName, arg);
7174
}
7275

73-
public static class ArgumentChecker<T> {
74-
75-
private final String argName;
76-
private final T arg;
77-
78-
ArgumentChecker(String argName, T arg) {
79-
this.argName = argName;
80-
this.arg = arg;
81-
}
82-
83-
public T nonNull() {
84-
if (arg == null) {
85-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
86-
}
87-
return arg;
88-
}
89-
90-
public T nonNull(String message, Object... args) {
91-
if (arg == null) {
92-
throw new IllegalArgumentException(String.format(message, args));
93-
}
94-
return arg;
95-
}
96-
97-
public T equalTo(Object other) {
98-
if (arg == null) {
99-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
100-
}
101-
if (!Objects.equals(arg, other)) {
102-
throw new IllegalArgumentException(argName + " must be equal to `" + other + "`");
103-
}
104-
return arg;
105-
}
106-
107-
public T instanceOf(Class<?> cls) {
108-
if (arg == null) {
109-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
110-
}
111-
if (!cls.isInstance(arg)) {
112-
throw new IllegalArgumentException(argName + " must be an instance of " + cls);
113-
}
114-
return arg;
115-
}
116-
}
117-
11876
public static Duration nonNegative(String argName, Duration arg) {
11977
if (arg == null) {
120-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
78+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
12179
}
12280
if (arg.isNegative()) {
123-
throw new IllegalArgumentException(argName + " must be set to 0 or more");
81+
throw new IllegalArgumentException(String.format(MUST_BE_NON_NEGATIVE, argName));
12482
}
12583
return arg;
12684
}
12785

12886
public static Duration nonNegative(Duration arg) {
12987
if (arg == null) {
130-
throw new IllegalArgumentException("Duration must be set");
88+
throw new IllegalArgumentException(String.format(MUST_BE_SET, "Duration"));
13189
}
13290
if (arg.isNegative()) {
133-
throw new IllegalArgumentException("Duration must be set to 0 or more");
91+
throw new IllegalArgumentException(String.format(MUST_BE_NON_NEGATIVE, "Duration"));
13492
}
13593
return arg;
13694
}
13795

13896
public static int nonNegative(String argName, Integer number) {
13997
if (number == null) {
140-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
98+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
14199
}
142100
if (number < 0) {
143-
throw new IllegalArgumentException(argName + " cannot be less than 0");
101+
throw new IllegalArgumentException(String.format(MUST_BE_NON_NEGATIVE, argName));
144102
}
145103
return number;
146104
}
147105

148106
public static int positive(String argName, Integer number, String message) {
149107
if (number == null) {
150-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
108+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
151109
}
152110
if (number <= 0) {
153111
if (message == null) {
154-
throw new IllegalArgumentException(argName + " must be greater than 0");
112+
throw new IllegalArgumentException(String.format(MUST_BE_POSITIVE, argName));
155113
} else {
156114
throw new IllegalArgumentException(message);
157115
}
@@ -162,7 +120,7 @@ public static int positive(String argName, Integer number, String message) {
162120
public static double positive(String argName, Double number, String message) {
163121
if (number <= 0) {
164122
if (message == null) {
165-
throw new IllegalArgumentException(argName + " must be greater than 0");
123+
throw new IllegalArgumentException(String.format(MUST_BE_POSITIVE, argName));
166124
} else {
167125
throw new IllegalArgumentException(message);
168126
}
@@ -182,6 +140,73 @@ public static IntChecker argument(String argName, Integer number) {
182140
return new IntChecker(argName, number);
183141
}
184142

143+
public static FileChecker argument(String argName, File file) {
144+
return new FileChecker(argName, file);
145+
}
146+
147+
public static void stateCondition(boolean state, String message, Object... args) {
148+
if (!state) {
149+
throw new IllegalStateException(String.format(message, args));
150+
}
151+
}
152+
153+
public static <T> StateChecker<T> state(String name, T state) {
154+
return new StateChecker<>(name, state);
155+
}
156+
157+
public static FileStateChecker state(String name, File file) {
158+
return new FileStateChecker(name, file);
159+
}
160+
161+
public static PathStateChecker state(String name, Path path) {
162+
return new PathStateChecker(name, path);
163+
}
164+
165+
public static class ArgumentChecker<T> {
166+
167+
private final String argName;
168+
private final T arg;
169+
170+
ArgumentChecker(String argName, T arg) {
171+
this.argName = argName;
172+
this.arg = arg;
173+
}
174+
175+
public T nonNull() {
176+
if (arg == null) {
177+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
178+
}
179+
return arg;
180+
}
181+
182+
public T nonNull(String message, Object... args) {
183+
if (arg == null) {
184+
throw new IllegalArgumentException(String.format(message, args));
185+
}
186+
return arg;
187+
}
188+
189+
public T equalTo(Object other) {
190+
if (arg == null) {
191+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
192+
}
193+
if (!Objects.equals(arg, other)) {
194+
throw new IllegalArgumentException(String.format(MUST_BE_EQUAL, argName, other));
195+
}
196+
return arg;
197+
}
198+
199+
public T instanceOf(Class<?> cls) {
200+
if (arg == null) {
201+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
202+
}
203+
if (!cls.isInstance(arg)) {
204+
throw new IllegalArgumentException(argName + " must be an instance of " + cls);
205+
}
206+
return arg;
207+
}
208+
}
209+
185210
public static class IntChecker {
186211

187212
private final String argName;
@@ -194,7 +219,7 @@ public static class IntChecker {
194219

195220
public int greaterThan(int max, String message) {
196221
if (number == null) {
197-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
222+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
198223
}
199224
if (number <= max) {
200225
throw new IllegalArgumentException(message);
@@ -203,10 +228,6 @@ public int greaterThan(int max, String message) {
203228
}
204229
}
205230

206-
public static FileChecker argument(String argName, File file) {
207-
return new FileChecker(argName, file);
208-
}
209-
210231
public static class FileChecker {
211232

212233
private final String argName;
@@ -219,7 +240,7 @@ public static class FileChecker {
219240

220241
public File isFile() {
221242
if (file == null) {
222-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
243+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
223244
}
224245
if (!file.exists()) {
225246
throw new IllegalArgumentException(
@@ -234,7 +255,7 @@ public File isFile() {
234255

235256
public File isDirectory() {
236257
if (file == null) {
237-
throw new IllegalArgumentException(String.format(ARG_MUST_BE_SET, argName));
258+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
238259
}
239260
if (!file.exists()) {
240261
throw new IllegalArgumentException(
@@ -248,16 +269,6 @@ public File isDirectory() {
248269
}
249270
}
250271

251-
public static void stateCondition(boolean state, String message, Object... args) {
252-
if (!state) {
253-
throw new IllegalStateException(String.format(message, args));
254-
}
255-
}
256-
257-
public static <T> StateChecker<T> state(String name, T state) {
258-
return new StateChecker<>(name, state);
259-
}
260-
261272
public static class StateChecker<T> {
262273

263274
private final String name;
@@ -284,7 +295,7 @@ public T nonNull(String message, Object... args) {
284295

285296
public T instanceOf(Class<?> cls) {
286297
if (state == null) {
287-
throw new IllegalStateException(String.format(ARG_MUST_BE_SET, name));
298+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
288299
}
289300
if (!cls.isInstance(state)) {
290301
throw new IllegalStateException(name + " must be an instance of " + cls);
@@ -293,10 +304,6 @@ public T instanceOf(Class<?> cls) {
293304
}
294305
}
295306

296-
public static FileStateChecker state(String name, File file) {
297-
return new FileStateChecker(name, file);
298-
}
299-
300307
public static class FileStateChecker {
301308

302309
private final String name;
@@ -309,7 +316,7 @@ public static class FileStateChecker {
309316

310317
public File isFile() {
311318
if (file == null) {
312-
throw new IllegalStateException(String.format(ARG_MUST_BE_SET, name));
319+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
313320
}
314321
if (!file.exists()) {
315322
throw new IllegalStateException(String.format(MUST_EXIST, name, file.getAbsolutePath()));
@@ -322,7 +329,7 @@ public File isFile() {
322329

323330
public File isDirectory() {
324331
if (file == null) {
325-
throw new IllegalStateException(String.format(ARG_MUST_BE_SET, name));
332+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
326333
}
327334
if (!file.exists()) {
328335
throw new IllegalStateException(String.format(MUST_EXIST, name, file.getAbsolutePath()));
@@ -334,10 +341,6 @@ public File isDirectory() {
334341
}
335342
}
336343

337-
public static PathStateChecker state(String name, Path path) {
338-
return new PathStateChecker(name, path);
339-
}
340-
341344
public static class PathStateChecker {
342345

343346
private final String name;
@@ -350,7 +353,7 @@ public static class PathStateChecker {
350353

351354
public Path isFile() {
352355
if (path == null) {
353-
throw new IllegalStateException(String.format(ARG_MUST_BE_SET, name));
356+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
354357
}
355358
if (!Files.exists(path)) {
356359
throw new IllegalStateException(String.format(MUST_EXIST, name, path));
@@ -363,7 +366,7 @@ public Path isFile() {
363366

364367
public Path isDirectory() {
365368
if (path == null) {
366-
throw new IllegalStateException(String.format(ARG_MUST_BE_SET, name));
369+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
367370
}
368371
if (!Files.exists(path)) {
369372
throw new IllegalStateException(String.format(MUST_EXIST, name, path));

0 commit comments

Comments
 (0)