1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jute.base;
17
18 import static com.google.common.base.Preconditions.checkArgument;
19 import static org.apache.commons.lang3.StringUtils.isNotBlank;
20 import static org.apache.commons.lang3.StringUtils.isNotEmpty;
21
22 import java.io.File;
23
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Iterables;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class Precondition {
41
42 private static final String NOT_NULL_MSG = "'%s' cannot be null";
43 private static final String EXISTS_MSG = "[%s] does not exist";
44 private static final String IS_DIR_MSG = "[%s] is not an existing directory";
45 private static final String IS_FILE_MSG = "[%s] is not an existing file";
46 private static final String NOT_BLANK_MSG = "'%s' cannot be blank";
47 private static final String NOT_EMPTY_MSG = "'%s' cannot be the empty string";
48 private static final String MIN_MSG = "%s not allowed. '%s' must be greater than or equal to %s";
49 private static final String MAX_MSG = "%s not allowed. '%s' must be less than or equal to %s";
50 private static final String EQUALS_MSG = "[%s] not allowed. '%s' must be equal to [%s]";
51 private static final String NOT_EQUALS_MSG = "[%s] not allowed. '%s' must not be equal to [%s]";
52 private static final String ARG_NAME = "argName";
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public static File checkExists(File arg, String argName) {
70 checkNotBlank(argName, ARG_NAME);
71 checkNotNull(arg, argName);
72 checkArgument(arg.exists(), EXISTS_MSG, arg);
73 return arg;
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public static File checkIsDir(File arg, String argName) {
92 checkArgument(checkExists(arg, argName).isDirectory(), IS_DIR_MSG, arg);
93 return arg;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public static File checkIsFile(File arg, String argName) {
112 checkArgument(checkExists(arg, argName).isFile(), IS_FILE_MSG, arg);
113 return arg;
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public static <T> T checkNotNull(T arg, String argName) {
132 checkNotBlank(argName, ARG_NAME);
133 return Preconditions.checkNotNull(arg, NOT_NULL_MSG, argName);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public static String checkNotBlank(String arg, String argName) {
152 checkArgument(isNotBlank(argName), NOT_BLANK_MSG, ARG_NAME);
153 checkArgument(isNotBlank(arg), NOT_BLANK_MSG, argName);
154 return arg;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public static String checkNotEmpty(String arg, String argName) {
173 checkArgument(isNotBlank(argName), NOT_BLANK_MSG, ARG_NAME);
174 checkArgument(isNotEmpty(arg), NOT_EMPTY_MSG, argName);
175 return arg;
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static Optional<String> checkNotBlank(Optional<String> arg, String argName) {
194 if (arg.isPresent()) {
195 checkNotBlank(arg.get(), argName);
196 }
197 return arg;
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public static Optional<String> checkNotEmpty(Optional<String> arg, String argName) {
216 if (arg.isPresent()) {
217 checkArgument(isNotEmpty(arg.get()), NOT_EMPTY_MSG, argName);
218 }
219 return arg;
220 }
221
222
223
224
225 public static Optional<Integer> checkMin(Optional<Integer> arg, int min, String argName) {
226 if (arg.isPresent()) {
227 checkMin(arg.get(), min, argName);
228 }
229 return arg;
230 }
231
232
233
234
235 public static Optional<Long> checkMin(Optional<Long> arg, long min, String argName) {
236 if (arg.isPresent()) {
237 checkMin(arg.get(), min, argName);
238 }
239 return arg;
240 }
241
242
243
244
245 public static int checkMax(int arg, int max, String argName) {
246 checkNotBlank(argName, ARG_NAME);
247 checkArgument(arg <= max, MAX_MSG, arg, argName, max);
248 return arg;
249 }
250
251
252
253
254 public static long checkMax(long arg, long max, String argName) {
255 checkNotBlank(argName, ARG_NAME);
256 checkArgument(arg <= max, MAX_MSG, arg, argName, max);
257 return arg;
258 }
259
260 public static int checkRange(int arg, int min, int max, String argName) {
261 return checkMin(checkMax(arg, max, argName), min, argName);
262 }
263
264
265
266
267 public static int checkMin(int arg, int min, String argName) {
268 checkNotBlank(argName, ARG_NAME);
269 checkArgument(arg >= min, MIN_MSG, arg, argName, min);
270 return arg;
271 }
272
273
274
275
276 public static long checkMin(long arg, long min, String argName) {
277 checkNotBlank(argName, ARG_NAME);
278 checkArgument(arg >= min, MIN_MSG, arg, argName, min);
279 return arg;
280 }
281
282 public static <T> T checkNotEquals(T arg, T illegal, String argName) {
283 checkNotNull(arg, checkNotBlank(argName, ARG_NAME));
284 checkNotNull(illegal, "illegal");
285 checkArgument(!arg.equals(illegal), NOT_EQUALS_MSG, argName, arg, illegal);
286 return arg;
287 }
288
289 public static <T> T checkEquals(T arg, T expected, String argName) {
290 checkNotNull(arg, checkNotBlank(argName, ARG_NAME));
291 checkNotNull(expected, "expected");
292 checkArgument(arg.equals(expected), EQUALS_MSG, argName, arg, expected);
293 return arg;
294 }
295
296 public static long checkEquals(long arg, long expected, String argName) {
297 checkNotBlank(argName, ARG_NAME);
298 checkArgument(arg == expected, EQUALS_MSG, argName, arg, expected);
299 return arg;
300 }
301
302 public static int checkEquals(int arg, int expected, String argName) {
303 checkNotBlank(argName, ARG_NAME);
304 checkArgument(arg == expected, EQUALS_MSG, argName, arg, expected);
305 return arg;
306 }
307
308 public static void checkTruths(int expected, boolean... expressions) {
309 checkMin(expected, 0, "expected");
310 checkArgument(expressions.length > 0, "must supply at least 1 expression");
311 int actual = 0;
312 for (boolean expression : expressions) {
313 actual = expression ? (actual + 1) : actual;
314 }
315 checkArgument(expected == actual, "expected %s of %s expression(s) to be true, but %s were true instead", expected, expressions.length, actual);
316 }
317
318 public static <I extends Iterable<T>, T> I checkSize(I iterable, int expected) {
319 int actual = Iterables.size(iterable);
320 checkArgument(expected == actual, "expected exactly %s element(s) but there were %s instead", expected, actual);
321 return iterable;
322 }
323
324 }