• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.base;
16 
17 import static com.google.common.base.Strings.lenientFormat;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.errorprone.annotations.CanIgnoreReturnValue;
21 import org.checkerframework.checker.nullness.compatqual.NullableDecl;
22 
23 /**
24  * Static convenience methods that serve the same purpose as Java language <a
25  * href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html">assertions</a>,
26  * except that they are always enabled. These methods should be used instead of Java assertions
27  * whenever there is a chance the check may fail "in real life". Example:
28  *
29  * <pre>{@code
30  * Bill bill = remoteService.getLastUnpaidBill();
31  *
32  * // In case bug 12345 happens again we'd rather just die
33  * Verify.verify(bill.status() == Status.UNPAID,
34  *     "Unexpected bill status: %s", bill.status());
35  * }</pre>
36  *
37  * <h3>Comparison to alternatives</h3>
38  *
39  * <p><b>Note:</b> In some cases the differences explained below can be subtle. When it's unclear
40  * which approach to use, <b>don't worry</b> too much about it; just pick something that seems
41  * reasonable and it will be fine.
42  *
43  * <ul>
44  *   <li>If checking whether the <i>caller</i> has violated your method or constructor's contract
45  *       (such as by passing an invalid argument), use the utilities of the {@link Preconditions}
46  *       class instead.
47  *   <li>If checking an <i>impossible</i> condition (which <i>cannot</i> happen unless your own
48  *       class or its <i>trusted</i> dependencies is badly broken), this is what ordinary Java
49  *       assertions are for. Note that assertions are not enabled by default; they are essentially
50  *       considered "compiled comments."
51  *   <li>An explicit {@code if/throw} (as illustrated below) is always acceptable; we still
52  *       recommend using our {@link VerifyException} exception type. Throwing a plain {@link
53  *       RuntimeException} is frowned upon.
54  *   <li>Use of {@link java.util.Objects#requireNonNull(Object)} is generally discouraged, since
55  *       {@link #verifyNotNull(Object)} and {@link Preconditions#checkNotNull(Object)} perform the
56  *       same function with more clarity.
57  * </ul>
58  *
59  * <h3>Warning about performance</h3>
60  *
61  * <p>Remember that parameter values for message construction must all be computed eagerly, and
62  * autoboxing and varargs array creation may happen as well, even when the verification succeeds and
63  * the message ends up unneeded. Performance-sensitive verification checks should continue to use
64  * usual form:
65  *
66  * <pre>{@code
67  * Bill bill = remoteService.getLastUnpaidBill();
68  * if (bill.status() != Status.UNPAID) {
69  *   throw new VerifyException("Unexpected bill status: " + bill.status());
70  * }
71  * }</pre>
72  *
73  * <h3>Only {@code %s} is supported</h3>
74  *
75  * <p>As with {@link Preconditions}, {@code Verify} uses {@link Strings#lenientFormat} to format
76  * error message template strings. This only supports the {@code "%s"} specifier, not the full range
77  * of {@link java.util.Formatter} specifiers. However, note that if the number of arguments does not
78  * match the number of occurrences of {@code "%s"} in the format string, {@code Verify} will still
79  * behave as expected, and will still include all argument values in the error message; the message
80  * will simply not be formatted exactly as intended.
81  *
82  * <h3>More information</h3>
83  *
84  * See <a href="https://github.com/google/guava/wiki/ConditionalFailuresExplained">Conditional
85  * failures explained</a> in the Guava User Guide for advice on when this class should be used.
86  *
87  * @since 17.0
88  */
89 @GwtCompatible
90 public final class Verify {
91   /**
92    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with no
93    * message otherwise.
94    *
95    * @throws VerifyException if {@code expression} is {@code false}
96    * @see Preconditions#checkState Preconditions.checkState()
97    */
verify(boolean expression)98   public static void verify(boolean expression) {
99     if (!expression) {
100       throw new VerifyException();
101     }
102   }
103 
104   /**
105    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
106    * custom message otherwise.
107    *
108    * @param expression a boolean expression
109    * @param errorMessageTemplate a template for the exception message should the check fail. The
110    *     message is formed by replacing each {@code %s} placeholder in the template with an
111    *     argument. These are matched by position - the first {@code %s} gets {@code
112    *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
113    *     square braces. Unmatched placeholders will be left as-is.
114    * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
115    *     are converted to strings using {@link String#valueOf(Object)}.
116    * @throws VerifyException if {@code expression} is {@code false}
117    * @see Preconditions#checkState Preconditions.checkState()
118    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object... errorMessageArgs)119   public static void verify(
120       boolean expression,
121       @NullableDecl String errorMessageTemplate,
122       @NullableDecl Object... errorMessageArgs) {
123     if (!expression) {
124       throw new VerifyException(lenientFormat(errorMessageTemplate, errorMessageArgs));
125     }
126   }
127 
128   /**
129    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
130    * custom message otherwise.
131    *
132    * <p>See {@link #verify(boolean, String, Object...)} for details.
133    *
134    * @since 23.1 (varargs overload since 17.0)
135    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, char p1)136   public static void verify(
137       boolean expression, @NullableDecl String errorMessageTemplate, char p1) {
138     if (!expression) {
139       throw new VerifyException(lenientFormat(errorMessageTemplate, p1));
140     }
141   }
142 
143   /**
144    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
145    * custom message otherwise.
146    *
147    * <p>See {@link #verify(boolean, String, Object...)} for details.
148    *
149    * @since 23.1 (varargs overload since 17.0)
150    */
verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1)151   public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1) {
152     if (!expression) {
153       throw new VerifyException(lenientFormat(errorMessageTemplate, p1));
154     }
155   }
156 
157   /**
158    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
159    * custom message otherwise.
160    *
161    * <p>See {@link #verify(boolean, String, Object...)} for details.
162    *
163    * @since 23.1 (varargs overload since 17.0)
164    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, long p1)165   public static void verify(
166       boolean expression, @NullableDecl String errorMessageTemplate, long p1) {
167     if (!expression) {
168       throw new VerifyException(lenientFormat(errorMessageTemplate, p1));
169     }
170   }
171 
172   /**
173    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
174    * custom message otherwise.
175    *
176    * <p>See {@link #verify(boolean, String, Object...)} for details.
177    *
178    * @since 23.1 (varargs overload since 17.0)
179    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1)180   public static void verify(
181       boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1) {
182     if (!expression) {
183       throw new VerifyException(lenientFormat(errorMessageTemplate, p1));
184     }
185   }
186 
187   /**
188    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
189    * custom message otherwise.
190    *
191    * <p>See {@link #verify(boolean, String, Object...)} for details.
192    *
193    * @since 23.1 (varargs overload since 17.0)
194    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, char p1, char p2)195   public static void verify(
196       boolean expression, @NullableDecl String errorMessageTemplate, char p1, char p2) {
197     if (!expression) {
198       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
199     }
200   }
201 
202   /**
203    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
204    * custom message otherwise.
205    *
206    * <p>See {@link #verify(boolean, String, Object...)} for details.
207    *
208    * @since 23.1 (varargs overload since 17.0)
209    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, int p1, char p2)210   public static void verify(
211       boolean expression, @NullableDecl String errorMessageTemplate, int p1, char p2) {
212     if (!expression) {
213       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
214     }
215   }
216 
217   /**
218    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
219    * custom message otherwise.
220    *
221    * <p>See {@link #verify(boolean, String, Object...)} for details.
222    *
223    * @since 23.1 (varargs overload since 17.0)
224    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, long p1, char p2)225   public static void verify(
226       boolean expression, @NullableDecl String errorMessageTemplate, long p1, char p2) {
227     if (!expression) {
228       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
229     }
230   }
231 
232   /**
233    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
234    * custom message otherwise.
235    *
236    * <p>See {@link #verify(boolean, String, Object...)} for details.
237    *
238    * @since 23.1 (varargs overload since 17.0)
239    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, char p2)240   public static void verify(
241       boolean expression,
242       @NullableDecl String errorMessageTemplate,
243       @NullableDecl Object p1,
244       char p2) {
245     if (!expression) {
246       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
247     }
248   }
249 
250   /**
251    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
252    * custom message otherwise.
253    *
254    * <p>See {@link #verify(boolean, String, Object...)} for details.
255    *
256    * @since 23.1 (varargs overload since 17.0)
257    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, char p1, int p2)258   public static void verify(
259       boolean expression, @NullableDecl String errorMessageTemplate, char p1, int p2) {
260     if (!expression) {
261       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
262     }
263   }
264 
265   /**
266    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
267    * custom message otherwise.
268    *
269    * <p>See {@link #verify(boolean, String, Object...)} for details.
270    *
271    * @since 23.1 (varargs overload since 17.0)
272    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, int p1, int p2)273   public static void verify(
274       boolean expression, @NullableDecl String errorMessageTemplate, int p1, int p2) {
275     if (!expression) {
276       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
277     }
278   }
279 
280   /**
281    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
282    * custom message otherwise.
283    *
284    * <p>See {@link #verify(boolean, String, Object...)} for details.
285    *
286    * @since 23.1 (varargs overload since 17.0)
287    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, long p1, int p2)288   public static void verify(
289       boolean expression, @NullableDecl String errorMessageTemplate, long p1, int p2) {
290     if (!expression) {
291       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
292     }
293   }
294 
295   /**
296    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
297    * custom message otherwise.
298    *
299    * <p>See {@link #verify(boolean, String, Object...)} for details.
300    *
301    * @since 23.1 (varargs overload since 17.0)
302    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, int p2)303   public static void verify(
304       boolean expression,
305       @NullableDecl String errorMessageTemplate,
306       @NullableDecl Object p1,
307       int p2) {
308     if (!expression) {
309       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
310     }
311   }
312 
313   /**
314    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
315    * custom message otherwise.
316    *
317    * <p>See {@link #verify(boolean, String, Object...)} for details.
318    *
319    * @since 23.1 (varargs overload since 17.0)
320    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, char p1, long p2)321   public static void verify(
322       boolean expression, @NullableDecl String errorMessageTemplate, char p1, long p2) {
323     if (!expression) {
324       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
325     }
326   }
327 
328   /**
329    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
330    * custom message otherwise.
331    *
332    * <p>See {@link #verify(boolean, String, Object...)} for details.
333    *
334    * @since 23.1 (varargs overload since 17.0)
335    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, int p1, long p2)336   public static void verify(
337       boolean expression, @NullableDecl String errorMessageTemplate, int p1, long p2) {
338     if (!expression) {
339       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
340     }
341   }
342 
343   /**
344    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
345    * custom message otherwise.
346    *
347    * <p>See {@link #verify(boolean, String, Object...)} for details.
348    *
349    * @since 23.1 (varargs overload since 17.0)
350    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, long p1, long p2)351   public static void verify(
352       boolean expression, @NullableDecl String errorMessageTemplate, long p1, long p2) {
353     if (!expression) {
354       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
355     }
356   }
357 
358   /**
359    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
360    * custom message otherwise.
361    *
362    * <p>See {@link #verify(boolean, String, Object...)} for details.
363    *
364    * @since 23.1 (varargs overload since 17.0)
365    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, long p2)366   public static void verify(
367       boolean expression,
368       @NullableDecl String errorMessageTemplate,
369       @NullableDecl Object p1,
370       long p2) {
371     if (!expression) {
372       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
373     }
374   }
375 
376   /**
377    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
378    * custom message otherwise.
379    *
380    * <p>See {@link #verify(boolean, String, Object...)} for details.
381    *
382    * @since 23.1 (varargs overload since 17.0)
383    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, char p1, @NullableDecl Object p2)384   public static void verify(
385       boolean expression,
386       @NullableDecl String errorMessageTemplate,
387       char p1,
388       @NullableDecl Object p2) {
389     if (!expression) {
390       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
391     }
392   }
393 
394   /**
395    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
396    * custom message otherwise.
397    *
398    * <p>See {@link #verify(boolean, String, Object...)} for details.
399    *
400    * @since 23.1 (varargs overload since 17.0)
401    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, int p1, @NullableDecl Object p2)402   public static void verify(
403       boolean expression,
404       @NullableDecl String errorMessageTemplate,
405       int p1,
406       @NullableDecl Object p2) {
407     if (!expression) {
408       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
409     }
410   }
411 
412   /**
413    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
414    * custom message otherwise.
415    *
416    * <p>See {@link #verify(boolean, String, Object...)} for details.
417    *
418    * @since 23.1 (varargs overload since 17.0)
419    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, long p1, @NullableDecl Object p2)420   public static void verify(
421       boolean expression,
422       @NullableDecl String errorMessageTemplate,
423       long p1,
424       @NullableDecl Object p2) {
425     if (!expression) {
426       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
427     }
428   }
429 
430   /**
431    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
432    * custom message otherwise.
433    *
434    * <p>See {@link #verify(boolean, String, Object...)} for details.
435    *
436    * @since 23.1 (varargs overload since 17.0)
437    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, @NullableDecl Object p2)438   public static void verify(
439       boolean expression,
440       @NullableDecl String errorMessageTemplate,
441       @NullableDecl Object p1,
442       @NullableDecl Object p2) {
443     if (!expression) {
444       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2));
445     }
446   }
447 
448   /**
449    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
450    * custom message otherwise.
451    *
452    * <p>See {@link #verify(boolean, String, Object...)} for details.
453    *
454    * @since 23.1 (varargs overload since 17.0)
455    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, @NullableDecl Object p2, @NullableDecl Object p3)456   public static void verify(
457       boolean expression,
458       @NullableDecl String errorMessageTemplate,
459       @NullableDecl Object p1,
460       @NullableDecl Object p2,
461       @NullableDecl Object p3) {
462     if (!expression) {
463       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2, p3));
464     }
465   }
466 
467   /**
468    * Ensures that {@code expression} is {@code true}, throwing a {@code VerifyException} with a
469    * custom message otherwise.
470    *
471    * <p>See {@link #verify(boolean, String, Object...)} for details.
472    *
473    * @since 23.1 (varargs overload since 17.0)
474    */
verify( boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, @NullableDecl Object p2, @NullableDecl Object p3, @NullableDecl Object p4)475   public static void verify(
476       boolean expression,
477       @NullableDecl String errorMessageTemplate,
478       @NullableDecl Object p1,
479       @NullableDecl Object p2,
480       @NullableDecl Object p3,
481       @NullableDecl Object p4) {
482     if (!expression) {
483       throw new VerifyException(lenientFormat(errorMessageTemplate, p1, p2, p3, p4));
484     }
485   }
486 
487   /**
488    * Ensures that {@code reference} is non-null, throwing a {@code VerifyException} with a default
489    * message otherwise.
490    *
491    * @return {@code reference}, guaranteed to be non-null, for convenience
492    * @throws VerifyException if {@code reference} is {@code null}
493    * @see Preconditions#checkNotNull Preconditions.checkNotNull()
494    */
495   @CanIgnoreReturnValue
verifyNotNull(@ullableDecl T reference)496   public static <T> T verifyNotNull(@NullableDecl T reference) {
497     return verifyNotNull(reference, "expected a non-null reference");
498   }
499 
500   /**
501    * Ensures that {@code reference} is non-null, throwing a {@code VerifyException} with a custom
502    * message otherwise.
503    *
504    * @param errorMessageTemplate a template for the exception message should the check fail. The
505    *     message is formed by replacing each {@code %s} placeholder in the template with an
506    *     argument. These are matched by position - the first {@code %s} gets {@code
507    *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
508    *     square braces. Unmatched placeholders will be left as-is.
509    * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
510    *     are converted to strings using {@link String#valueOf(Object)}.
511    * @return {@code reference}, guaranteed to be non-null, for convenience
512    * @throws VerifyException if {@code reference} is {@code null}
513    * @see Preconditions#checkNotNull Preconditions.checkNotNull()
514    */
515   @CanIgnoreReturnValue
verifyNotNull( @ullableDecl T reference, @NullableDecl String errorMessageTemplate, @NullableDecl Object... errorMessageArgs)516   public static <T> T verifyNotNull(
517       @NullableDecl T reference,
518       @NullableDecl String errorMessageTemplate,
519       @NullableDecl Object... errorMessageArgs) {
520     verify(reference != null, errorMessageTemplate, errorMessageArgs);
521     return reference;
522   }
523 
524   // TODO(kevinb): consider <T> T verifySingleton(Iterable<T>) to take over for
525   // Iterables.getOnlyElement()
526 
Verify()527   private Verify() {}
528 }
529