001/* 002 * Copyright (C) 2013 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.base; 016 017import static com.google.common.base.Preconditions.format; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import javax.annotation.Nullable; 022 023/** 024 * Static convenience methods that serve the same purpose as Java language 025 * <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html"> 026 * assertions</a>, except that they are always enabled. These methods should be used instead of Java 027 * assertions whenever there is a chance the check may fail "in real life". Example: <pre> {@code 028 * 029 * Bill bill = remoteService.getLastUnpaidBill(); 030 * 031 * // In case bug 12345 happens again we'd rather just die 032 * Verify.verify(bill.status() == Status.UNPAID, 033 * "Unexpected bill status: %s", bill.status());}</pre> 034 * 035 * <h3>Comparison to alternatives</h3> 036 * 037 * <p><b>Note:</b> In some cases the differences explained below can be subtle. When it's unclear 038 * which approach to use, <b>don't worry</b> too much about it; just pick something that seems 039 * reasonable and it will be fine. 040 * 041 * <ul> 042 * <li>If checking whether the <i>caller</i> has violated your method or constructor's contract 043 * (such as by passing an invalid argument), use the utilities of the {@link Preconditions} 044 * class instead. 045 * 046 * <li>If checking an <i>impossible</i> condition (which <i>cannot</i> happen unless your own class 047 * or its <i>trusted</i> dependencies is badly broken), this is what ordinary Java assertions 048 * are for. Note that assertions are not enabled by default; they are essentially considered 049 * "compiled comments." 050 * 051 * <li>An explicit {@code if/throw} (as illustrated below) is always acceptable; we still recommend 052 * using our {@link VerifyException} exception type. Throwing a plain {@link RuntimeException} 053 * is frowned upon. 054 * 055 * <li>Use of {@link java.util.Objects#requireNonNull(Object)} is generally discouraged, since 056 * {@link #verifyNotNull(Object)} and {@link Preconditions#checkNotNull(Object)} perform the 057 * same function with more clarity. 058 * </ul> 059 * 060 * <h3>Warning about performance</h3> 061 * 062 * <p>Remember that parameter values for message construction must all be computed eagerly, and 063 * autoboxing and varargs array creation may happen as well, even when the verification succeeds and 064 * the message ends up unneeded. Performance-sensitive verification checks should continue to use 065 * usual form: <pre> {@code 066 * 067 * Bill bill = remoteService.getLastUnpaidBill(); 068 * if (bill.status() != Status.UNPAID) { 069 * throw new VerifyException("Unexpected bill status: " + bill.status()); 070 * }}</pre> 071 * 072 * <h3>Only {@code %s} is supported</h3> 073 * 074 * <p>As with {@link Preconditions} error message template strings, only the {@code "%s"} specifier 075 * is supported, not the full range of {@link java.util.Formatter} specifiers. However, note that if 076 * the number of arguments does not match the number of occurrences of {@code "%s"} in the format 077 * string, {@code Verify} will still behave as expected, and will still include all argument values 078 * in the error message; the message will simply not be formatted exactly as intended. 079 * 080 * <h3>More information</h3> 081 * 082 * See <a href="https://github.com/google/guava/wiki/ConditionalFailuresExplained">Conditional 083 * failures explained</a> in the Guava User Guide for advice on when this class should be used. 084 * 085 * @since 17.0 086 */ 087@GwtCompatible 088public final class Verify { 089 /** 090 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with no 091 * message otherwise. 092 * 093 * @throws VerifyException if {@code expression} is {@code false} 094 * @see Preconditions#checkState Preconditions.checkState() 095 */ 096 public static void verify(boolean expression) { 097 if (!expression) { 098 throw new VerifyException(); 099 } 100 } 101 102 /** 103 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 104 * custom message otherwise. 105 * 106 * @param expression a boolean expression 107 * @param errorMessageTemplate a template for the exception message should the check fail. The 108 * message is formed by replacing each {@code %s} placeholder in the template with an 109 * argument. These are matched by position - the first {@code %s} gets 110 * {@code errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted 111 * message in square braces. Unmatched placeholders will be left as-is. 112 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 113 * are converted to strings using {@link String#valueOf(Object)}. 114 * @throws VerifyException if {@code expression} is {@code false} 115 * @see Preconditions#checkState Preconditions.checkState() 116 */ 117 public static void verify( 118 boolean expression, 119 @Nullable String errorMessageTemplate, 120 @Nullable Object... errorMessageArgs) { 121 if (!expression) { 122 throw new VerifyException(format(errorMessageTemplate, errorMessageArgs)); 123 } 124 } 125 126 /** 127 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 128 * custom message otherwise. 129 * 130 * <p>See {@link #verify(boolean, String, Object...)} for details. 131 * 132 * @since 23.1 (varargs overload since 17.0) 133 */ 134 public static void verify(boolean expression, @Nullable String errorMessageTemplate, char p1) { 135 if (!expression) { 136 throw new VerifyException(format(errorMessageTemplate, p1)); 137 } 138 } 139 140 /** 141 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 142 * custom message otherwise. 143 * 144 * <p>See {@link #verify(boolean, String, Object...)} for details. 145 * 146 * @since 23.1 (varargs overload since 17.0) 147 */ 148 public static void verify(boolean expression, @Nullable String errorMessageTemplate, int p1) { 149 if (!expression) { 150 throw new VerifyException(format(errorMessageTemplate, p1)); 151 } 152 } 153 154 /** 155 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 156 * custom message otherwise. 157 * 158 * <p>See {@link #verify(boolean, String, Object...)} for details. 159 * 160 * @since 23.1 (varargs overload since 17.0) 161 */ 162 public static void verify(boolean expression, @Nullable String errorMessageTemplate, long p1) { 163 if (!expression) { 164 throw new VerifyException(format(errorMessageTemplate, p1)); 165 } 166 } 167 168 /** 169 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 170 * custom message otherwise. 171 * 172 * <p>See {@link #verify(boolean, String, Object...)} for details. 173 * 174 * @since 23.1 (varargs overload since 17.0) 175 */ 176 public static void verify( 177 boolean expression, @Nullable String errorMessageTemplate, @Nullable Object p1) { 178 if (!expression) { 179 throw new VerifyException(format(errorMessageTemplate, p1)); 180 } 181 } 182 183 /** 184 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 185 * custom message otherwise. 186 * 187 * <p>See {@link #verify(boolean, String, Object...)} for details. 188 * 189 * @since 23.1 (varargs overload since 17.0) 190 */ 191 public static void verify( 192 boolean expression, @Nullable String errorMessageTemplate, char p1, char p2) { 193 if (!expression) { 194 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 195 } 196 } 197 198 /** 199 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 200 * custom message otherwise. 201 * 202 * <p>See {@link #verify(boolean, String, Object...)} for details. 203 * 204 * @since 23.1 (varargs overload since 17.0) 205 */ 206 public static void verify( 207 boolean expression, @Nullable String errorMessageTemplate, int p1, char p2) { 208 if (!expression) { 209 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 210 } 211 } 212 213 /** 214 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 215 * custom message otherwise. 216 * 217 * <p>See {@link #verify(boolean, String, Object...)} for details. 218 * 219 * @since 23.1 (varargs overload since 17.0) 220 */ 221 public static void verify( 222 boolean expression, @Nullable String errorMessageTemplate, long p1, char p2) { 223 if (!expression) { 224 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 225 } 226 } 227 228 /** 229 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 230 * custom message otherwise. 231 * 232 * <p>See {@link #verify(boolean, String, Object...)} for details. 233 * 234 * @since 23.1 (varargs overload since 17.0) 235 */ 236 public static void verify( 237 boolean expression, @Nullable String errorMessageTemplate, @Nullable Object p1, char p2) { 238 if (!expression) { 239 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 240 } 241 } 242 243 /** 244 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 245 * custom message otherwise. 246 * 247 * <p>See {@link #verify(boolean, String, Object...)} for details. 248 * 249 * @since 23.1 (varargs overload since 17.0) 250 */ 251 public static void verify( 252 boolean expression, @Nullable String errorMessageTemplate, char p1, int p2) { 253 if (!expression) { 254 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 255 } 256 } 257 258 /** 259 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 260 * custom message otherwise. 261 * 262 * <p>See {@link #verify(boolean, String, Object...)} for details. 263 * 264 * @since 23.1 (varargs overload since 17.0) 265 */ 266 public static void verify( 267 boolean expression, @Nullable String errorMessageTemplate, int p1, int p2) { 268 if (!expression) { 269 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 270 } 271 } 272 273 /** 274 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 275 * custom message otherwise. 276 * 277 * <p>See {@link #verify(boolean, String, Object...)} for details. 278 * 279 * @since 23.1 (varargs overload since 17.0) 280 */ 281 public static void verify( 282 boolean expression, @Nullable String errorMessageTemplate, long p1, int p2) { 283 if (!expression) { 284 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 285 } 286 } 287 288 /** 289 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 290 * custom message otherwise. 291 * 292 * <p>See {@link #verify(boolean, String, Object...)} for details. 293 * 294 * @since 23.1 (varargs overload since 17.0) 295 */ 296 public static void verify( 297 boolean expression, @Nullable String errorMessageTemplate, @Nullable Object p1, int p2) { 298 if (!expression) { 299 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 300 } 301 } 302 303 /** 304 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 305 * custom message otherwise. 306 * 307 * <p>See {@link #verify(boolean, String, Object...)} for details. 308 * 309 * @since 23.1 (varargs overload since 17.0) 310 */ 311 public static void verify( 312 boolean expression, @Nullable String errorMessageTemplate, char p1, long p2) { 313 if (!expression) { 314 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 315 } 316 } 317 318 /** 319 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 320 * custom message otherwise. 321 * 322 * <p>See {@link #verify(boolean, String, Object...)} for details. 323 * 324 * @since 23.1 (varargs overload since 17.0) 325 */ 326 public static void verify( 327 boolean expression, @Nullable String errorMessageTemplate, int p1, long p2) { 328 if (!expression) { 329 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 330 } 331 } 332 333 /** 334 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 335 * custom message otherwise. 336 * 337 * <p>See {@link #verify(boolean, String, Object...)} for details. 338 * 339 * @since 23.1 (varargs overload since 17.0) 340 */ 341 public static void verify( 342 boolean expression, @Nullable String errorMessageTemplate, long p1, long p2) { 343 if (!expression) { 344 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 345 } 346 } 347 348 /** 349 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 350 * custom message otherwise. 351 * 352 * <p>See {@link #verify(boolean, String, Object...)} for details. 353 * 354 * @since 23.1 (varargs overload since 17.0) 355 */ 356 public static void verify( 357 boolean expression, @Nullable String errorMessageTemplate, @Nullable Object p1, long p2) { 358 if (!expression) { 359 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 360 } 361 } 362 363 /** 364 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 365 * custom message otherwise. 366 * 367 * <p>See {@link #verify(boolean, String, Object...)} for details. 368 * 369 * @since 23.1 (varargs overload since 17.0) 370 */ 371 public static void verify( 372 boolean expression, @Nullable String errorMessageTemplate, char p1, @Nullable Object p2) { 373 if (!expression) { 374 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 375 } 376 } 377 378 /** 379 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 380 * custom message otherwise. 381 * 382 * <p>See {@link #verify(boolean, String, Object...)} for details. 383 * 384 * @since 23.1 (varargs overload since 17.0) 385 */ 386 public static void verify( 387 boolean expression, @Nullable String errorMessageTemplate, int p1, @Nullable Object p2) { 388 if (!expression) { 389 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 390 } 391 } 392 393 /** 394 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 395 * custom message otherwise. 396 * 397 * <p>See {@link #verify(boolean, String, Object...)} for details. 398 * 399 * @since 23.1 (varargs overload since 17.0) 400 */ 401 public static void verify( 402 boolean expression, @Nullable String errorMessageTemplate, long p1, @Nullable Object p2) { 403 if (!expression) { 404 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 405 } 406 } 407 408 /** 409 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 410 * custom message otherwise. 411 * 412 * <p>See {@link #verify(boolean, String, Object...)} for details. 413 * 414 * @since 23.1 (varargs overload since 17.0) 415 */ 416 public static void verify( 417 boolean expression, 418 @Nullable String errorMessageTemplate, 419 @Nullable Object p1, 420 @Nullable Object p2) { 421 if (!expression) { 422 throw new VerifyException(format(errorMessageTemplate, p1, p2)); 423 } 424 } 425 426 /** 427 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 428 * custom message otherwise. 429 * 430 * <p>See {@link #verify(boolean, String, Object...)} for details. 431 * 432 * @since 23.1 (varargs overload since 17.0) 433 */ 434 public static void verify( 435 boolean expression, 436 @Nullable String errorMessageTemplate, 437 @Nullable Object p1, 438 @Nullable Object p2, 439 @Nullable Object p3) { 440 if (!expression) { 441 throw new VerifyException(format(errorMessageTemplate, p1, p2, p3)); 442 } 443 } 444 445 /** 446 * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a 447 * custom message otherwise. 448 * 449 * <p>See {@link #verify(boolean, String, Object...)} for details. 450 * 451 * @since 23.1 (varargs overload since 17.0) 452 */ 453 public static void verify( 454 boolean expression, 455 @Nullable String errorMessageTemplate, 456 @Nullable Object p1, 457 @Nullable Object p2, 458 @Nullable Object p3, 459 @Nullable Object p4) { 460 if (!expression) { 461 throw new VerifyException(format(errorMessageTemplate, p1, p2, p3, p4)); 462 } 463 } 464 465 /** 466 * Ensures that {@code reference} is non-null, throwing a {@code VerifyException} with a default 467 * message otherwise. 468 * 469 * @return {@code reference}, guaranteed to be non-null, for convenience 470 * @throws VerifyException if {@code reference} is {@code null} 471 * @see Preconditions#checkNotNull Preconditions.checkNotNull() 472 */ 473 @CanIgnoreReturnValue 474 public static <T> T verifyNotNull(@Nullable T reference) { 475 return verifyNotNull(reference, "expected a non-null reference"); 476 } 477 478 /** 479 * Ensures that {@code reference} is non-null, throwing a {@code VerifyException} with a custom 480 * message otherwise. 481 * 482 * @param errorMessageTemplate a template for the exception message should the check fail. The 483 * message is formed by replacing each {@code %s} placeholder in the template with an 484 * argument. These are matched by position - the first {@code %s} gets 485 * {@code errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted 486 * message in square braces. Unmatched placeholders will be left as-is. 487 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 488 * are converted to strings using {@link String#valueOf(Object)}. 489 * @return {@code reference}, guaranteed to be non-null, for convenience 490 * @throws VerifyException if {@code reference} is {@code null} 491 * @see Preconditions#checkNotNull Preconditions.checkNotNull() 492 */ 493 @CanIgnoreReturnValue 494 public static <T> T verifyNotNull( 495 @Nullable T reference, 496 @Nullable String errorMessageTemplate, 497 @Nullable Object... errorMessageArgs) { 498 verify(reference != null, errorMessageTemplate, errorMessageArgs); 499 return reference; 500 } 501 502 // TODO(kevinb): consider <T> T verifySingleton(Iterable<T>) to take over for 503 // Iterables.getOnlyElement() 504 505 private Verify() {} 506}