Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.util; |
| 18 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 19 | import android.annotation.IntRange; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.text.TextUtils; |
| 22 | |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 23 | import java.util.Collection; |
| 24 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Simple static methods to be called at the start of your own methods to verify |
| 27 | * correct arguments and state. |
| 28 | */ |
| 29 | public class Preconditions { |
| 30 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 31 | public static void checkArgument(boolean expression) { |
| 32 | if (!expression) { |
| 33 | throw new IllegalArgumentException(); |
| 34 | } |
| 35 | } |
| 36 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 37 | /** |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 38 | * 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 52 | * 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. Moltmann | 9dcb86a | 2016-03-14 14:31:12 -0700 | [diff] [blame^] | 59 | public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string) { |
Philip P. Moltmann | 76d7e3e | 2016-01-15 13:22:13 -0800 | [diff] [blame] | 60 | 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. Moltmann | 9dcb86a | 2016-03-14 14:31:12 -0700 | [diff] [blame^] | 76 | public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string, |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 77 | final Object errorMessage) { |
| 78 | if (TextUtils.isEmpty(string)) { |
| 79 | throw new IllegalArgumentException(String.valueOf(errorMessage)); |
| 80 | } |
| 81 | return string; |
| 82 | } |
| 83 | |
| 84 | /** |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 85 | * 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 92 | public static @NonNull <T> T checkNotNull(final T reference) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 93 | 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 109 | public static @NonNull <T> T checkNotNull(final T reference, final Object errorMessage) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 110 | if (reference == null) { |
| 111 | throw new NullPointerException(String.valueOf(errorMessage)); |
| 112 | } |
| 113 | return reference; |
| 114 | } |
| 115 | |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 116 | /** |
| 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 Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 121 | * @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 Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 135 | * @throws IllegalStateException if {@code expression} is false |
| 136 | */ |
Igor Murashkin | b3a78b2 | 2014-04-17 14:01:13 -0700 | [diff] [blame] | 137 | public static void checkState(final boolean expression) { |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 138 | checkState(expression, null); |
Jeff Sharkey | c268f0b | 2012-08-24 10:25:31 -0700 | [diff] [blame] | 139 | } |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Check the requested flags, throwing if any requested flags are outside |
| 143 | * the allowed set. |
Philip P. Moltmann | 9dcb86a | 2016-03-14 14:31:12 -0700 | [diff] [blame^] | 144 | * |
| 145 | * @return the validated requested flags. |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 146 | */ |
Philip P. Moltmann | 9dcb86a | 2016-03-14 14:31:12 -0700 | [diff] [blame^] | 147 | public static int checkFlagsArgument(final int requestedFlags, final int allowedFlags) { |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 148 | 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. Moltmann | 9dcb86a | 2016-03-14 14:31:12 -0700 | [diff] [blame^] | 153 | |
| 154 | return requestedFlags; |
Jeff Sharkey | ee2f7df | 2013-09-26 11:32:30 -0700 | [diff] [blame] | 155 | } |
Igor Murashkin | b3a78b2 | 2014-04-17 14:01:13 -0700 | [diff] [blame] | 156 | |
| 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 165 | public static @IntRange(from = 0) int checkArgumentNonnegative(final int value, |
| 166 | final String errorMessage) { |
Igor Murashkin | b3a78b2 | 2014-04-17 14:01:13 -0700 | [diff] [blame] | 167 | 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. Moltmann | 9dcb86a | 2016-03-14 14:31:12 -0700 | [diff] [blame^] | 177 | * @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 Murashkin | b3a78b2 | 2014-04-17 14:01:13 -0700 | [diff] [blame] | 193 | * @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 Yeh | 97f1c85 | 2014-05-28 16:36:05 -0700 | [diff] [blame] | 278 | * 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 Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 305 | * 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 Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 332 | * Ensures that the array is not {@code null}, and none of its elements are {@code null}. |
Igor Murashkin | b3a78b2 | 2014-04-17 14:01:13 -0700 | [diff] [blame] | 333 | * |
| 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 Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 357 | * 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. Moltmann | 76d7e3e | 2016-01-15 13:22:13 -0800 | [diff] [blame] | 367 | public static @NonNull <C extends Collection<T>, T> C checkCollectionElementsNotNull( |
| 368 | final C value, final String valueName) { |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 369 | 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 Murashkin | b3a78b2 | 2014-04-17 14:01:13 -0700 | [diff] [blame] | 408 | * 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 Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 445 | } |