blob: 7c852467171653782e8f9c71c3e50a829e587c44 [file] [log] [blame]
Jeff Sharkeyd2a45872011-05-28 20:56:34 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.util;
18
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080019import android.annotation.IntRange;
20import android.annotation.NonNull;
21import android.text.TextUtils;
22
Ruben Brunk91838de2014-07-16 17:24:17 -070023import java.util.Collection;
24
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070025/**
26 * Simple static methods to be called at the start of your own methods to verify
27 * correct arguments and state.
28 */
29public class Preconditions {
30
Jeff Sharkey620b32b2015-04-23 19:36:02 -070031 public static void checkArgument(boolean expression) {
32 if (!expression) {
33 throw new IllegalArgumentException();
34 }
35 }
36
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070037 /**
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -070038 * Ensures that an expression checking an argument is true.
39 *
40 * @param expression the expression to check
41 * @param errorMessage the exception message to use if the check fails; will
42 * be converted to a string using {@link String#valueOf(Object)}
43 * @throws IllegalArgumentException if {@code expression} is false
44 */
45 public static void checkArgument(boolean expression, final Object errorMessage) {
46 if (!expression) {
47 throw new IllegalArgumentException(String.valueOf(errorMessage));
48 }
49 }
50
51 /**
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080052 * Ensures that an string reference passed as a parameter to the calling
53 * method is not empty.
54 *
55 * @param string an string reference
56 * @return the string reference that was validated
57 * @throws IllegalArgumentException if {@code string} is empty
58 */
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070059 public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string) {
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -080060 if (TextUtils.isEmpty(string)) {
61 throw new IllegalArgumentException();
62 }
63 return string;
64 }
65
66 /**
67 * Ensures that an string reference passed as a parameter to the calling
68 * method is not empty.
69 *
70 * @param string an string reference
71 * @param errorMessage the exception message to use if the check fails; will
72 * be converted to a string using {@link String#valueOf(Object)}
73 * @return the string reference that was validated
74 * @throws IllegalArgumentException if {@code string} is empty
75 */
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070076 public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string,
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080077 final Object errorMessage) {
78 if (TextUtils.isEmpty(string)) {
79 throw new IllegalArgumentException(String.valueOf(errorMessage));
80 }
81 return string;
82 }
83
84 /**
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070085 * Ensures that an object reference passed as a parameter to the calling
86 * method is not null.
87 *
88 * @param reference an object reference
89 * @return the non-null reference that was validated
90 * @throws NullPointerException if {@code reference} is null
91 */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080092 public static @NonNull <T> T checkNotNull(final T reference) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070093 if (reference == null) {
94 throw new NullPointerException();
95 }
96 return reference;
97 }
98
99 /**
100 * Ensures that an object reference passed as a parameter to the calling
101 * method is not null.
102 *
103 * @param reference an object reference
104 * @param errorMessage the exception message to use if the check fails; will
105 * be converted to a string using {@link String#valueOf(Object)}
106 * @return the non-null reference that was validated
107 * @throws NullPointerException if {@code reference} is null
108 */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800109 public static @NonNull <T> T checkNotNull(final T reference, final Object errorMessage) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700110 if (reference == null) {
111 throw new NullPointerException(String.valueOf(errorMessage));
112 }
113 return reference;
114 }
115
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700116 /**
117 * Ensures the truth of an expression involving the state of the calling
118 * instance, but not involving any parameters to the calling method.
119 *
120 * @param expression a boolean expression
Makoto Onuki1a5ee772016-02-12 15:34:57 -0800121 * @param message exception message
122 * @throws IllegalStateException if {@code expression} is false
123 */
124 public static void checkState(final boolean expression, String message) {
125 if (!expression) {
126 throw new IllegalStateException(message);
127 }
128 }
129
130 /**
131 * Ensures the truth of an expression involving the state of the calling
132 * instance, but not involving any parameters to the calling method.
133 *
134 * @param expression a boolean expression
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700135 * @throws IllegalStateException if {@code expression} is false
136 */
Igor Murashkinb3a78b22014-04-17 14:01:13 -0700137 public static void checkState(final boolean expression) {
Makoto Onuki1a5ee772016-02-12 15:34:57 -0800138 checkState(expression, null);
Jeff Sharkeyc268f0b2012-08-24 10:25:31 -0700139 }
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700140
141 /**
142 * Check the requested flags, throwing if any requested flags are outside
143 * the allowed set.
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -0700144 *
145 * @return the validated requested flags.
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700146 */
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -0700147 public static int checkFlagsArgument(final int requestedFlags, final int allowedFlags) {
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700148 if ((requestedFlags & allowedFlags) != requestedFlags) {
149 throw new IllegalArgumentException("Requested flags 0x"
150 + Integer.toHexString(requestedFlags) + ", but only 0x"
151 + Integer.toHexString(allowedFlags) + " are allowed");
152 }
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -0700153
154 return requestedFlags;
Jeff Sharkeyee2f7df2013-09-26 11:32:30 -0700155 }
Igor Murashkinb3a78b22014-04-17 14:01:13 -0700156
157 /**
158 * Ensures that that the argument numeric value is non-negative.
159 *
160 * @param value a numeric int value
161 * @param errorMessage the exception message to use if the check fails
162 * @return the validated numeric value
163 * @throws IllegalArgumentException if {@code value} was negative
164 */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800165 public static @IntRange(from = 0) int checkArgumentNonnegative(final int value,
166 final String errorMessage) {
Igor Murashkinb3a78b22014-04-17 14:01:13 -0700167 if (value < 0) {
168 throw new IllegalArgumentException(errorMessage);
169 }
170
171 return value;
172 }
173
174 /**
175 * Ensures that that the argument numeric value is non-negative.
176 *
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -0700177 * @param value a numeric int value
178 *
179 * @return the validated numeric value
180 * @throws IllegalArgumentException if {@code value} was negative
181 */
182 public static @IntRange(from = 0) int checkArgumentNonnegative(final int value) {
183 if (value < 0) {
184 throw new IllegalArgumentException();
185 }
186
187 return value;
188 }
189
190 /**
191 * Ensures that that the argument numeric value is non-negative.
192 *
Igor Murashkinb3a78b22014-04-17 14:01:13 -0700193 * @param value a numeric long value
194 * @param errorMessage the exception message to use if the check fails
195 * @return the validated numeric value
196 * @throws IllegalArgumentException if {@code value} was negative
197 */
198 public static long checkArgumentNonnegative(final long value, final String errorMessage) {
199 if (value < 0) {
200 throw new IllegalArgumentException(errorMessage);
201 }
202
203 return value;
204 }
205
206 /**
207 * Ensures that that the argument numeric value is positive.
208 *
209 * @param value a numeric int value
210 * @param errorMessage the exception message to use if the check fails
211 * @return the validated numeric value
212 * @throws IllegalArgumentException if {@code value} was not positive
213 */
214 public static int checkArgumentPositive(final int value, final String errorMessage) {
215 if (value <= 0) {
216 throw new IllegalArgumentException(errorMessage);
217 }
218
219 return value;
220 }
221
222 /**
223 * Ensures that the argument floating point value is a finite number.
224 *
225 * <p>A finite number is defined to be both representable (that is, not NaN) and
226 * not infinite (that is neither positive or negative infinity).</p>
227 *
228 * @param value a floating point value
229 * @param valueName the name of the argument to use if the check fails
230 *
231 * @return the validated floating point value
232 *
233 * @throws IllegalArgumentException if {@code value} was not finite
234 */
235 public static float checkArgumentFinite(final float value, final String valueName) {
236 if (Float.isNaN(value)) {
237 throw new IllegalArgumentException(valueName + " must not be NaN");
238 } else if (Float.isInfinite(value)) {
239 throw new IllegalArgumentException(valueName + " must not be infinite");
240 }
241
242 return value;
243 }
244
245 /**
246 * Ensures that the argument floating point value is within the inclusive range.
247 *
248 * <p>While this can be used to range check against +/- infinity, note that all NaN numbers
249 * will always be out of range.</p>
250 *
251 * @param value a floating point value
252 * @param lower the lower endpoint of the inclusive range
253 * @param upper the upper endpoint of the inclusive range
254 * @param valueName the name of the argument to use if the check fails
255 *
256 * @return the validated floating point value
257 *
258 * @throws IllegalArgumentException if {@code value} was not within the range
259 */
260 public static float checkArgumentInRange(float value, float lower, float upper,
261 String valueName) {
262 if (Float.isNaN(value)) {
263 throw new IllegalArgumentException(valueName + " must not be NaN");
264 } else if (value < lower) {
265 throw new IllegalArgumentException(
266 String.format(
267 "%s is out of range of [%f, %f] (too low)", valueName, lower, upper));
268 } else if (value > upper) {
269 throw new IllegalArgumentException(
270 String.format(
271 "%s is out of range of [%f, %f] (too high)", valueName, lower, upper));
272 }
273
274 return value;
275 }
276
277 /**
Yin-Chia Yeh97f1c852014-05-28 16:36:05 -0700278 * Ensures that the argument int value is within the inclusive range.
279 *
280 * @param value a int value
281 * @param lower the lower endpoint of the inclusive range
282 * @param upper the upper endpoint of the inclusive range
283 * @param valueName the name of the argument to use if the check fails
284 *
285 * @return the validated int value
286 *
287 * @throws IllegalArgumentException if {@code value} was not within the range
288 */
289 public static int checkArgumentInRange(int value, int lower, int upper,
290 String valueName) {
291 if (value < lower) {
292 throw new IllegalArgumentException(
293 String.format(
294 "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
295 } else if (value > upper) {
296 throw new IllegalArgumentException(
297 String.format(
298 "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
299 }
300
301 return value;
302 }
303
304 /**
Daichi Hirono2dd48252016-01-12 15:46:41 +0900305 * Ensures that the argument long value is within the inclusive range.
306 *
307 * @param value a long value
308 * @param lower the lower endpoint of the inclusive range
309 * @param upper the upper endpoint of the inclusive range
310 * @param valueName the name of the argument to use if the check fails
311 *
312 * @return the validated long value
313 *
314 * @throws IllegalArgumentException if {@code value} was not within the range
315 */
316 public static long checkArgumentInRange(long value, long lower, long upper,
317 String valueName) {
318 if (value < lower) {
319 throw new IllegalArgumentException(
320 String.format(
321 "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
322 } else if (value > upper) {
323 throw new IllegalArgumentException(
324 String.format(
325 "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
326 }
327
328 return value;
329 }
330
331 /**
Ruben Brunk91838de2014-07-16 17:24:17 -0700332 * Ensures that the array is not {@code null}, and none of its elements are {@code null}.
Igor Murashkinb3a78b22014-04-17 14:01:13 -0700333 *
334 * @param value an array of boxed objects
335 * @param valueName the name of the argument to use if the check fails
336 *
337 * @return the validated array
338 *
339 * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
340 */
341 public static <T> T[] checkArrayElementsNotNull(final T[] value, final String valueName) {
342 if (value == null) {
343 throw new NullPointerException(valueName + " must not be null");
344 }
345
346 for (int i = 0; i < value.length; ++i) {
347 if (value[i] == null) {
348 throw new NullPointerException(
349 String.format("%s[%d] must not be null", valueName, i));
350 }
351 }
352
353 return value;
354 }
355
356 /**
Ruben Brunk91838de2014-07-16 17:24:17 -0700357 * Ensures that the {@link Collection} is not {@code null}, and none of its elements are
358 * {@code null}.
359 *
360 * @param value a {@link Collection} of boxed objects
361 * @param valueName the name of the argument to use if the check fails
362 *
363 * @return the validated {@link Collection}
364 *
365 * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
366 */
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800367 public static @NonNull <C extends Collection<T>, T> C checkCollectionElementsNotNull(
368 final C value, final String valueName) {
Ruben Brunk91838de2014-07-16 17:24:17 -0700369 if (value == null) {
370 throw new NullPointerException(valueName + " must not be null");
371 }
372
373 long ctr = 0;
374 for (T elem : value) {
375 if (elem == null) {
376 throw new NullPointerException(
377 String.format("%s[%d] must not be null", valueName, ctr));
378 }
379 ++ctr;
380 }
381
382 return value;
383 }
384
385 /**
386 * Ensures that the {@link Collection} is not {@code null}, and contains at least one element.
387 *
388 * @param value a {@link Collection} of boxed elements.
389 * @param valueName the name of the argument to use if the check fails.
390
391 * @return the validated {@link Collection}
392 *
393 * @throws NullPointerException if the {@code value} was {@code null}
394 * @throws IllegalArgumentException if the {@code value} was empty
395 */
396 public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value,
397 final String valueName) {
398 if (value == null) {
399 throw new NullPointerException(valueName + " must not be null");
400 }
401 if (value.isEmpty()) {
402 throw new IllegalArgumentException(valueName + " is empty");
403 }
404 return value;
405 }
406
407 /**
Igor Murashkinb3a78b22014-04-17 14:01:13 -0700408 * Ensures that all elements in the argument floating point array are within the inclusive range
409 *
410 * <p>While this can be used to range check against +/- infinity, note that all NaN numbers
411 * will always be out of range.</p>
412 *
413 * @param value a floating point array of values
414 * @param lower the lower endpoint of the inclusive range
415 * @param upper the upper endpoint of the inclusive range
416 * @param valueName the name of the argument to use if the check fails
417 *
418 * @return the validated floating point value
419 *
420 * @throws IllegalArgumentException if any of the elements in {@code value} were out of range
421 * @throws NullPointerException if the {@code value} was {@code null}
422 */
423 public static float[] checkArrayElementsInRange(float[] value, float lower, float upper,
424 String valueName) {
425 checkNotNull(value, valueName + " must not be null");
426
427 for (int i = 0; i < value.length; ++i) {
428 float v = value[i];
429
430 if (Float.isNaN(v)) {
431 throw new IllegalArgumentException(valueName + "[" + i + "] must not be NaN");
432 } else if (v < lower) {
433 throw new IllegalArgumentException(
434 String.format("%s[%d] is out of range of [%f, %f] (too low)",
435 valueName, i, lower, upper));
436 } else if (v > upper) {
437 throw new IllegalArgumentException(
438 String.format("%s[%d] is out of range of [%f, %f] (too high)",
439 valueName, i, lower, upper));
440 }
441 }
442
443 return value;
444 }
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700445}