001/*
002 * Copyright (C) 2007 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.Strings.lenientFormat;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import org.checkerframework.checker.nullness.compatqual.NonNullDecl;
022import org.checkerframework.checker.nullness.compatqual.NullableDecl;
023
024/**
025 * Static convenience methods that help a method or constructor check whether it was invoked
026 * correctly (that is, whether its <i>preconditions</i> were met).
027 *
028 * <p>If the precondition is not met, the {@code Preconditions} method throws an unchecked exception
029 * of a specified type, which helps the method in which the exception was thrown communicate that
030 * its caller has made a mistake. This allows constructs such as
031 *
032 * <pre>{@code
033 * public static double sqrt(double value) {
034 *   if (value < 0) {
035 *     throw new IllegalArgumentException("input is negative: " + value);
036 *   }
037 *   // calculate square root
038 * }
039 * }</pre>
040 *
041 * <p>to be replaced with the more compact
042 *
043 * <pre>{@code
044 * public static double sqrt(double value) {
045 *   checkArgument(value >= 0, "input is negative: %s", value);
046 *   // calculate square root
047 * }
048 * }</pre>
049 *
050 * <p>so that a hypothetical bad caller of this method, such as:
051 *
052 * <pre>{@code
053 * void exampleBadCaller() {
054 *   double d = sqrt(-1.0);
055 * }
056 * }</pre>
057 *
058 * <p>would be flagged as having called {@code sqrt()} with an illegal argument.
059 *
060 * <h3>Performance</h3>
061 *
062 * <p>Avoid passing message arguments that are expensive to compute; your code will always compute
063 * them, even though they usually won't be needed. If you have such arguments, use the conventional
064 * if/throw idiom instead.
065 *
066 * <p>Depending on your message arguments, memory may be allocated for boxing and varargs array
067 * creation. However, the methods of this class have a large number of overloads that prevent such
068 * allocations in many common cases.
069 *
070 * <p>The message string is not formatted unless the exception will be thrown, so the cost of the
071 * string formatting itself should not be a concern.
072 *
073 * <p>As with any performance concerns, you should consider profiling your code (in a production
074 * environment if possible) before spending a lot of effort on tweaking a particular element.
075 *
076 * <h3>Other types of preconditions</h3>
077 *
078 * <p>Not every type of precondition failure is supported by these methods. Continue to throw
079 * standard JDK exceptions such as {@link java.util.NoSuchElementException} or {@link
080 * UnsupportedOperationException} in the situations they are intended for.
081 *
082 * <h3>Non-preconditions</h3>
083 *
084 * <p>It is of course possible to use the methods of this class to check for invalid conditions
085 * which are <i>not the caller's fault</i>. Doing so is <b>not recommended</b> because it is
086 * misleading to future readers of the code and of stack traces. See <a
087 * href="https://github.com/google/guava/wiki/ConditionalFailuresExplained">Conditional failures
088 * explained</a> in the Guava User Guide for more advice. Notably, {@link Verify} offers assertions
089 * similar to those in this class for non-precondition checks.
090 *
091 * <h3>{@code java.util.Objects.requireNonNull()}</h3>
092 *
093 * <p>Projects which use {@code com.google.common} should generally avoid the use of {@link
094 * java.util.Objects#requireNonNull(Object)}. Instead, use whichever of {@link
095 * #checkNotNull(Object)} or {@link Verify#verifyNotNull(Object)} is appropriate to the situation.
096 * (The same goes for the message-accepting overloads.)
097 *
098 * <h3>Only {@code %s} is supported</h3>
099 *
100 * <p>{@code Preconditions} uses {@link Strings#lenientFormat} to format error message template
101 * strings. This only supports the {@code "%s"} specifier, not the full range of {@link
102 * java.util.Formatter} specifiers. However, note that if the number of arguments does not match the
103 * number of occurrences of {@code "%s"} in the format string, {@code Preconditions} will still
104 * behave as expected, and will still include all argument values in the error message; the message
105 * will simply not be formatted exactly as intended.
106 *
107 * <h3>More information</h3>
108 *
109 * <p>See the Guava User Guide on <a
110 * href="https://github.com/google/guava/wiki/PreconditionsExplained">using {@code
111 * Preconditions}</a>.
112 *
113 * @author Kevin Bourrillion
114 * @since 2.0
115 */
116@GwtCompatible
117public final class Preconditions {
118  private Preconditions() {}
119
120  /**
121   * Ensures the truth of an expression involving one or more parameters to the calling method.
122   *
123   * @param expression a boolean expression
124   * @throws IllegalArgumentException if {@code expression} is false
125   */
126  public static void checkArgument(boolean expression) {
127    if (!expression) {
128      throw new IllegalArgumentException();
129    }
130  }
131
132  /**
133   * Ensures the truth of an expression involving one or more parameters to the calling method.
134   *
135   * @param expression a boolean expression
136   * @param errorMessage the exception message to use if the check fails; will be converted to a
137   *     string using {@link String#valueOf(Object)}
138   * @throws IllegalArgumentException if {@code expression} is false
139   */
140  public static void checkArgument(boolean expression, @NullableDecl Object errorMessage) {
141    if (!expression) {
142      throw new IllegalArgumentException(String.valueOf(errorMessage));
143    }
144  }
145
146  /**
147   * Ensures the truth of an expression involving one or more parameters to the calling method.
148   *
149   * @param expression a boolean expression
150   * @param errorMessageTemplate a template for the exception message should the check fail. The
151   *     message is formed by replacing each {@code %s} placeholder in the template with an
152   *     argument. These are matched by position - the first {@code %s} gets {@code
153   *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
154   *     square braces. Unmatched placeholders will be left as-is.
155   * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
156   *     are converted to strings using {@link String#valueOf(Object)}.
157   * @throws IllegalArgumentException if {@code expression} is false
158   */
159  public static void checkArgument(
160      boolean expression,
161      @NullableDecl String errorMessageTemplate,
162      @NullableDecl Object... errorMessageArgs) {
163    if (!expression) {
164      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, errorMessageArgs));
165    }
166  }
167
168  /**
169   * Ensures the truth of an expression involving one or more parameters to the calling method.
170   *
171   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
172   *
173   * @since 20.0 (varargs overload since 2.0)
174   */
175  public static void checkArgument(boolean b, @NullableDecl String errorMessageTemplate, char p1) {
176    if (!b) {
177      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1));
178    }
179  }
180
181  /**
182   * Ensures the truth of an expression involving one or more parameters to the calling method.
183   *
184   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
185   *
186   * @since 20.0 (varargs overload since 2.0)
187   */
188  public static void checkArgument(boolean b, @NullableDecl String errorMessageTemplate, int p1) {
189    if (!b) {
190      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1));
191    }
192  }
193
194  /**
195   * Ensures the truth of an expression involving one or more parameters to the calling method.
196   *
197   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
198   *
199   * @since 20.0 (varargs overload since 2.0)
200   */
201  public static void checkArgument(boolean b, @NullableDecl String errorMessageTemplate, long p1) {
202    if (!b) {
203      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1));
204    }
205  }
206
207  /**
208   * Ensures the truth of an expression involving one or more parameters to the calling method.
209   *
210   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
211   *
212   * @since 20.0 (varargs overload since 2.0)
213   */
214  public static void checkArgument(
215      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1) {
216    if (!b) {
217      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1));
218    }
219  }
220
221  /**
222   * Ensures the truth of an expression involving one or more parameters to the calling method.
223   *
224   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
225   *
226   * @since 20.0 (varargs overload since 2.0)
227   */
228  public static void checkArgument(
229      boolean b, @NullableDecl String errorMessageTemplate, char p1, char p2) {
230    if (!b) {
231      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
232    }
233  }
234
235  /**
236   * Ensures the truth of an expression involving one or more parameters to the calling method.
237   *
238   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
239   *
240   * @since 20.0 (varargs overload since 2.0)
241   */
242  public static void checkArgument(
243      boolean b, @NullableDecl String errorMessageTemplate, char p1, int p2) {
244    if (!b) {
245      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
246    }
247  }
248
249  /**
250   * Ensures the truth of an expression involving one or more parameters to the calling method.
251   *
252   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
253   *
254   * @since 20.0 (varargs overload since 2.0)
255   */
256  public static void checkArgument(
257      boolean b, @NullableDecl String errorMessageTemplate, char p1, long p2) {
258    if (!b) {
259      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
260    }
261  }
262
263  /**
264   * Ensures the truth of an expression involving one or more parameters to the calling method.
265   *
266   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
267   *
268   * @since 20.0 (varargs overload since 2.0)
269   */
270  public static void checkArgument(
271      boolean b, @NullableDecl String errorMessageTemplate, char p1, @NullableDecl Object p2) {
272    if (!b) {
273      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
274    }
275  }
276
277  /**
278   * Ensures the truth of an expression involving one or more parameters to the calling method.
279   *
280   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
281   *
282   * @since 20.0 (varargs overload since 2.0)
283   */
284  public static void checkArgument(
285      boolean b, @NullableDecl String errorMessageTemplate, int p1, char p2) {
286    if (!b) {
287      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
288    }
289  }
290
291  /**
292   * Ensures the truth of an expression involving one or more parameters to the calling method.
293   *
294   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
295   *
296   * @since 20.0 (varargs overload since 2.0)
297   */
298  public static void checkArgument(
299      boolean b, @NullableDecl String errorMessageTemplate, int p1, int p2) {
300    if (!b) {
301      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
302    }
303  }
304
305  /**
306   * Ensures the truth of an expression involving one or more parameters to the calling method.
307   *
308   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
309   *
310   * @since 20.0 (varargs overload since 2.0)
311   */
312  public static void checkArgument(
313      boolean b, @NullableDecl String errorMessageTemplate, int p1, long p2) {
314    if (!b) {
315      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
316    }
317  }
318
319  /**
320   * Ensures the truth of an expression involving one or more parameters to the calling method.
321   *
322   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
323   *
324   * @since 20.0 (varargs overload since 2.0)
325   */
326  public static void checkArgument(
327      boolean b, @NullableDecl String errorMessageTemplate, int p1, @NullableDecl Object p2) {
328    if (!b) {
329      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
330    }
331  }
332
333  /**
334   * Ensures the truth of an expression involving one or more parameters to the calling method.
335   *
336   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
337   *
338   * @since 20.0 (varargs overload since 2.0)
339   */
340  public static void checkArgument(
341      boolean b, @NullableDecl String errorMessageTemplate, long p1, char p2) {
342    if (!b) {
343      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
344    }
345  }
346
347  /**
348   * Ensures the truth of an expression involving one or more parameters to the calling method.
349   *
350   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
351   *
352   * @since 20.0 (varargs overload since 2.0)
353   */
354  public static void checkArgument(
355      boolean b, @NullableDecl String errorMessageTemplate, long p1, int p2) {
356    if (!b) {
357      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
358    }
359  }
360
361  /**
362   * Ensures the truth of an expression involving one or more parameters to the calling method.
363   *
364   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
365   *
366   * @since 20.0 (varargs overload since 2.0)
367   */
368  public static void checkArgument(
369      boolean b, @NullableDecl String errorMessageTemplate, long p1, long p2) {
370    if (!b) {
371      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
372    }
373  }
374
375  /**
376   * Ensures the truth of an expression involving one or more parameters to the calling method.
377   *
378   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
379   *
380   * @since 20.0 (varargs overload since 2.0)
381   */
382  public static void checkArgument(
383      boolean b, @NullableDecl String errorMessageTemplate, long p1, @NullableDecl Object p2) {
384    if (!b) {
385      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
386    }
387  }
388
389  /**
390   * Ensures the truth of an expression involving one or more parameters to the calling method.
391   *
392   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
393   *
394   * @since 20.0 (varargs overload since 2.0)
395   */
396  public static void checkArgument(
397      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, char p2) {
398    if (!b) {
399      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
400    }
401  }
402
403  /**
404   * Ensures the truth of an expression involving one or more parameters to the calling method.
405   *
406   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
407   *
408   * @since 20.0 (varargs overload since 2.0)
409   */
410  public static void checkArgument(
411      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, int p2) {
412    if (!b) {
413      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
414    }
415  }
416
417  /**
418   * Ensures the truth of an expression involving one or more parameters to the calling method.
419   *
420   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
421   *
422   * @since 20.0 (varargs overload since 2.0)
423   */
424  public static void checkArgument(
425      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, long p2) {
426    if (!b) {
427      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
428    }
429  }
430
431  /**
432   * Ensures the truth of an expression involving one or more parameters to the calling method.
433   *
434   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
435   *
436   * @since 20.0 (varargs overload since 2.0)
437   */
438  public static void checkArgument(
439      boolean b,
440      @NullableDecl String errorMessageTemplate,
441      @NullableDecl Object p1,
442      @NullableDecl Object p2) {
443    if (!b) {
444      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2));
445    }
446  }
447
448  /**
449   * Ensures the truth of an expression involving one or more parameters to the calling method.
450   *
451   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
452   *
453   * @since 20.0 (varargs overload since 2.0)
454   */
455  public static void checkArgument(
456      boolean b,
457      @NullableDecl String errorMessageTemplate,
458      @NullableDecl Object p1,
459      @NullableDecl Object p2,
460      @NullableDecl Object p3) {
461    if (!b) {
462      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2, p3));
463    }
464  }
465
466  /**
467   * Ensures the truth of an expression involving one or more parameters to the calling method.
468   *
469   * <p>See {@link #checkArgument(boolean, String, Object...)} for details.
470   *
471   * @since 20.0 (varargs overload since 2.0)
472   */
473  public static void checkArgument(
474      boolean b,
475      @NullableDecl String errorMessageTemplate,
476      @NullableDecl Object p1,
477      @NullableDecl Object p2,
478      @NullableDecl Object p3,
479      @NullableDecl Object p4) {
480    if (!b) {
481      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, p1, p2, p3, p4));
482    }
483  }
484
485  /**
486   * Ensures the truth of an expression involving the state of the calling instance, but not
487   * involving any parameters to the calling method.
488   *
489   * @param expression a boolean expression
490   * @throws IllegalStateException if {@code expression} is false
491   * @see Verify#verify Verify.verify()
492   */
493  public static void checkState(boolean expression) {
494    if (!expression) {
495      throw new IllegalStateException();
496    }
497  }
498
499  /**
500   * Ensures the truth of an expression involving the state of the calling instance, but not
501   * involving any parameters to the calling method.
502   *
503   * @param expression a boolean expression
504   * @param errorMessage the exception message to use if the check fails; will be converted to a
505   *     string using {@link String#valueOf(Object)}
506   * @throws IllegalStateException if {@code expression} is false
507   * @see Verify#verify Verify.verify()
508   */
509  public static void checkState(boolean expression, @NullableDecl Object errorMessage) {
510    if (!expression) {
511      throw new IllegalStateException(String.valueOf(errorMessage));
512    }
513  }
514
515  /**
516   * Ensures the truth of an expression involving the state of the calling instance, but not
517   * involving any parameters to the calling method.
518   *
519   * @param expression a boolean expression
520   * @param errorMessageTemplate a template for the exception message should the check fail. The
521   *     message is formed by replacing each {@code %s} placeholder in the template with an
522   *     argument. These are matched by position - the first {@code %s} gets {@code
523   *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
524   *     square braces. Unmatched placeholders will be left as-is.
525   * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
526   *     are converted to strings using {@link String#valueOf(Object)}.
527   * @throws IllegalStateException if {@code expression} is false
528   * @see Verify#verify Verify.verify()
529   */
530  public static void checkState(
531      boolean expression,
532      @NullableDecl String errorMessageTemplate,
533      @NullableDecl Object... errorMessageArgs) {
534    if (!expression) {
535      throw new IllegalStateException(lenientFormat(errorMessageTemplate, errorMessageArgs));
536    }
537  }
538
539  /**
540   * Ensures the truth of an expression involving the state of the calling instance, but not
541   * involving any parameters to the calling method.
542   *
543   * <p>See {@link #checkState(boolean, String, Object...)} for details.
544   *
545   * @since 20.0 (varargs overload since 2.0)
546   */
547  public static void checkState(boolean b, @NullableDecl String errorMessageTemplate, char p1) {
548    if (!b) {
549      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1));
550    }
551  }
552
553  /**
554   * Ensures the truth of an expression involving the state of the calling instance, but not
555   * involving any parameters to the calling method.
556   *
557   * <p>See {@link #checkState(boolean, String, Object...)} for details.
558   *
559   * @since 20.0 (varargs overload since 2.0)
560   */
561  public static void checkState(boolean b, @NullableDecl String errorMessageTemplate, int p1) {
562    if (!b) {
563      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1));
564    }
565  }
566
567  /**
568   * Ensures the truth of an expression involving the state of the calling instance, but not
569   * involving any parameters to the calling method.
570   *
571   * <p>See {@link #checkState(boolean, String, Object...)} for details.
572   *
573   * @since 20.0 (varargs overload since 2.0)
574   */
575  public static void checkState(boolean b, @NullableDecl String errorMessageTemplate, long p1) {
576    if (!b) {
577      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1));
578    }
579  }
580
581  /**
582   * Ensures the truth of an expression involving the state of the calling instance, but not
583   * involving any parameters to the calling method.
584   *
585   * <p>See {@link #checkState(boolean, String, Object...)} for details.
586   *
587   * @since 20.0 (varargs overload since 2.0)
588   */
589  public static void checkState(
590      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1) {
591    if (!b) {
592      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1));
593    }
594  }
595
596  /**
597   * Ensures the truth of an expression involving the state of the calling instance, but not
598   * involving any parameters to the calling method.
599   *
600   * <p>See {@link #checkState(boolean, String, Object...)} for details.
601   *
602   * @since 20.0 (varargs overload since 2.0)
603   */
604  public static void checkState(
605      boolean b, @NullableDecl String errorMessageTemplate, char p1, char p2) {
606    if (!b) {
607      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
608    }
609  }
610
611  /**
612   * Ensures the truth of an expression involving the state of the calling instance, but not
613   * involving any parameters to the calling method.
614   *
615   * <p>See {@link #checkState(boolean, String, Object...)} for details.
616   *
617   * @since 20.0 (varargs overload since 2.0)
618   */
619  public static void checkState(
620      boolean b, @NullableDecl String errorMessageTemplate, char p1, int p2) {
621    if (!b) {
622      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
623    }
624  }
625
626  /**
627   * Ensures the truth of an expression involving the state of the calling instance, but not
628   * involving any parameters to the calling method.
629   *
630   * <p>See {@link #checkState(boolean, String, Object...)} for details.
631   *
632   * @since 20.0 (varargs overload since 2.0)
633   */
634  public static void checkState(
635      boolean b, @NullableDecl String errorMessageTemplate, char p1, long p2) {
636    if (!b) {
637      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
638    }
639  }
640
641  /**
642   * Ensures the truth of an expression involving the state of the calling instance, but not
643   * involving any parameters to the calling method.
644   *
645   * <p>See {@link #checkState(boolean, String, Object...)} for details.
646   *
647   * @since 20.0 (varargs overload since 2.0)
648   */
649  public static void checkState(
650      boolean b, @NullableDecl String errorMessageTemplate, char p1, @NullableDecl Object p2) {
651    if (!b) {
652      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
653    }
654  }
655
656  /**
657   * Ensures the truth of an expression involving the state of the calling instance, but not
658   * involving any parameters to the calling method.
659   *
660   * <p>See {@link #checkState(boolean, String, Object...)} for details.
661   *
662   * @since 20.0 (varargs overload since 2.0)
663   */
664  public static void checkState(
665      boolean b, @NullableDecl String errorMessageTemplate, int p1, char p2) {
666    if (!b) {
667      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
668    }
669  }
670
671  /**
672   * Ensures the truth of an expression involving the state of the calling instance, but not
673   * involving any parameters to the calling method.
674   *
675   * <p>See {@link #checkState(boolean, String, Object...)} for details.
676   *
677   * @since 20.0 (varargs overload since 2.0)
678   */
679  public static void checkState(
680      boolean b, @NullableDecl String errorMessageTemplate, int p1, int p2) {
681    if (!b) {
682      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
683    }
684  }
685
686  /**
687   * Ensures the truth of an expression involving the state of the calling instance, but not
688   * involving any parameters to the calling method.
689   *
690   * <p>See {@link #checkState(boolean, String, Object...)} for details.
691   *
692   * @since 20.0 (varargs overload since 2.0)
693   */
694  public static void checkState(
695      boolean b, @NullableDecl String errorMessageTemplate, int p1, long p2) {
696    if (!b) {
697      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
698    }
699  }
700
701  /**
702   * Ensures the truth of an expression involving the state of the calling instance, but not
703   * involving any parameters to the calling method.
704   *
705   * <p>See {@link #checkState(boolean, String, Object...)} for details.
706   *
707   * @since 20.0 (varargs overload since 2.0)
708   */
709  public static void checkState(
710      boolean b, @NullableDecl String errorMessageTemplate, int p1, @NullableDecl Object p2) {
711    if (!b) {
712      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
713    }
714  }
715
716  /**
717   * Ensures the truth of an expression involving the state of the calling instance, but not
718   * involving any parameters to the calling method.
719   *
720   * <p>See {@link #checkState(boolean, String, Object...)} for details.
721   *
722   * @since 20.0 (varargs overload since 2.0)
723   */
724  public static void checkState(
725      boolean b, @NullableDecl String errorMessageTemplate, long p1, char p2) {
726    if (!b) {
727      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
728    }
729  }
730
731  /**
732   * Ensures the truth of an expression involving the state of the calling instance, but not
733   * involving any parameters to the calling method.
734   *
735   * <p>See {@link #checkState(boolean, String, Object...)} for details.
736   *
737   * @since 20.0 (varargs overload since 2.0)
738   */
739  public static void checkState(
740      boolean b, @NullableDecl String errorMessageTemplate, long p1, int p2) {
741    if (!b) {
742      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
743    }
744  }
745
746  /**
747   * Ensures the truth of an expression involving the state of the calling instance, but not
748   * involving any parameters to the calling method.
749   *
750   * <p>See {@link #checkState(boolean, String, Object...)} for details.
751   *
752   * @since 20.0 (varargs overload since 2.0)
753   */
754  public static void checkState(
755      boolean b, @NullableDecl String errorMessageTemplate, long p1, long p2) {
756    if (!b) {
757      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
758    }
759  }
760
761  /**
762   * Ensures the truth of an expression involving the state of the calling instance, but not
763   * involving any parameters to the calling method.
764   *
765   * <p>See {@link #checkState(boolean, String, Object...)} for details.
766   *
767   * @since 20.0 (varargs overload since 2.0)
768   */
769  public static void checkState(
770      boolean b, @NullableDecl String errorMessageTemplate, long p1, @NullableDecl Object p2) {
771    if (!b) {
772      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
773    }
774  }
775
776  /**
777   * Ensures the truth of an expression involving the state of the calling instance, but not
778   * involving any parameters to the calling method.
779   *
780   * <p>See {@link #checkState(boolean, String, Object...)} for details.
781   *
782   * @since 20.0 (varargs overload since 2.0)
783   */
784  public static void checkState(
785      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, char p2) {
786    if (!b) {
787      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
788    }
789  }
790
791  /**
792   * Ensures the truth of an expression involving the state of the calling instance, but not
793   * involving any parameters to the calling method.
794   *
795   * <p>See {@link #checkState(boolean, String, Object...)} for details.
796   *
797   * @since 20.0 (varargs overload since 2.0)
798   */
799  public static void checkState(
800      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, int p2) {
801    if (!b) {
802      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
803    }
804  }
805
806  /**
807   * Ensures the truth of an expression involving the state of the calling instance, but not
808   * involving any parameters to the calling method.
809   *
810   * <p>See {@link #checkState(boolean, String, Object...)} for details.
811   *
812   * @since 20.0 (varargs overload since 2.0)
813   */
814  public static void checkState(
815      boolean b, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, long p2) {
816    if (!b) {
817      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
818    }
819  }
820
821  /**
822   * Ensures the truth of an expression involving the state of the calling instance, but not
823   * involving any parameters to the calling method.
824   *
825   * <p>See {@link #checkState(boolean, String, Object...)} for details.
826   *
827   * @since 20.0 (varargs overload since 2.0)
828   */
829  public static void checkState(
830      boolean b,
831      @NullableDecl String errorMessageTemplate,
832      @NullableDecl Object p1,
833      @NullableDecl Object p2) {
834    if (!b) {
835      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2));
836    }
837  }
838
839  /**
840   * Ensures the truth of an expression involving the state of the calling instance, but not
841   * involving any parameters to the calling method.
842   *
843   * <p>See {@link #checkState(boolean, String, Object...)} for details.
844   *
845   * @since 20.0 (varargs overload since 2.0)
846   */
847  public static void checkState(
848      boolean b,
849      @NullableDecl String errorMessageTemplate,
850      @NullableDecl Object p1,
851      @NullableDecl Object p2,
852      @NullableDecl Object p3) {
853    if (!b) {
854      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2, p3));
855    }
856  }
857
858  /**
859   * Ensures the truth of an expression involving the state of the calling instance, but not
860   * involving any parameters to the calling method.
861   *
862   * <p>See {@link #checkState(boolean, String, Object...)} for details.
863   *
864   * @since 20.0 (varargs overload since 2.0)
865   */
866  public static void checkState(
867      boolean b,
868      @NullableDecl String errorMessageTemplate,
869      @NullableDecl Object p1,
870      @NullableDecl Object p2,
871      @NullableDecl Object p3,
872      @NullableDecl Object p4) {
873    if (!b) {
874      throw new IllegalStateException(lenientFormat(errorMessageTemplate, p1, p2, p3, p4));
875    }
876  }
877
878  /**
879   * Ensures that an object reference passed as a parameter to the calling method is not null.
880   *
881   * @param reference an object reference
882   * @return the non-null reference that was validated
883   * @throws NullPointerException if {@code reference} is null
884   * @see Verify#verifyNotNull Verify.verifyNotNull()
885   */
886  @CanIgnoreReturnValue
887  @NonNullDecl
888  public static <T extends Object> T checkNotNull(@NonNullDecl T reference) {
889    if (reference == null) {
890      throw new NullPointerException();
891    }
892    return reference;
893  }
894
895  /**
896   * Ensures that an object reference passed as a parameter to the calling method is not null.
897   *
898   * @param reference an object reference
899   * @param errorMessage the exception message to use if the check fails; will be converted to a
900   *     string using {@link String#valueOf(Object)}
901   * @return the non-null reference that was validated
902   * @throws NullPointerException if {@code reference} is null
903   * @see Verify#verifyNotNull Verify.verifyNotNull()
904   */
905  @CanIgnoreReturnValue
906  @NonNullDecl
907  public static <T extends Object> T checkNotNull(
908      @NonNullDecl T reference, @NullableDecl Object errorMessage) {
909    if (reference == null) {
910      throw new NullPointerException(String.valueOf(errorMessage));
911    }
912    return reference;
913  }
914
915  /**
916   * Ensures that an object reference passed as a parameter to the calling method is not null.
917   *
918   * @param reference an object reference
919   * @param errorMessageTemplate a template for the exception message should the check fail. The
920   *     message is formed by replacing each {@code %s} placeholder in the template with an
921   *     argument. These are matched by position - the first {@code %s} gets {@code
922   *     errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
923   *     square braces. Unmatched placeholders will be left as-is.
924   * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
925   *     are converted to strings using {@link String#valueOf(Object)}.
926   * @return the non-null reference that was validated
927   * @throws NullPointerException if {@code reference} is null
928   * @see Verify#verifyNotNull Verify.verifyNotNull()
929   */
930  @CanIgnoreReturnValue
931  @NonNullDecl
932  public static <T extends Object> T checkNotNull(
933      @NonNullDecl T reference,
934      @NullableDecl String errorMessageTemplate,
935      @NullableDecl Object... errorMessageArgs) {
936    if (reference == null) {
937      throw new NullPointerException(lenientFormat(errorMessageTemplate, errorMessageArgs));
938    }
939    return reference;
940  }
941
942  /**
943   * Ensures that an object reference passed as a parameter to the calling method is not null.
944   *
945   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
946   *
947   * @since 20.0 (varargs overload since 2.0)
948   */
949  @CanIgnoreReturnValue
950  @NonNullDecl
951  public static <T extends Object> T checkNotNull(
952      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, char p1) {
953    if (obj == null) {
954      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1));
955    }
956    return obj;
957  }
958
959  /**
960   * Ensures that an object reference passed as a parameter to the calling method is not null.
961   *
962   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
963   *
964   * @since 20.0 (varargs overload since 2.0)
965   */
966  @CanIgnoreReturnValue
967  @NonNullDecl
968  public static <T extends Object> T checkNotNull(
969      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, int p1) {
970    if (obj == null) {
971      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1));
972    }
973    return obj;
974  }
975
976  /**
977   * Ensures that an object reference passed as a parameter to the calling method is not null.
978   *
979   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
980   *
981   * @since 20.0 (varargs overload since 2.0)
982   */
983  @CanIgnoreReturnValue
984  @NonNullDecl
985  public static <T extends Object> T checkNotNull(
986      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, long p1) {
987    if (obj == null) {
988      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1));
989    }
990    return obj;
991  }
992
993  /**
994   * Ensures that an object reference passed as a parameter to the calling method is not null.
995   *
996   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
997   *
998   * @since 20.0 (varargs overload since 2.0)
999   */
1000  @CanIgnoreReturnValue
1001  @NonNullDecl
1002  public static <T extends Object> T checkNotNull(
1003      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1) {
1004    if (obj == null) {
1005      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1));
1006    }
1007    return obj;
1008  }
1009
1010  /**
1011   * Ensures that an object reference passed as a parameter to the calling method is not null.
1012   *
1013   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1014   *
1015   * @since 20.0 (varargs overload since 2.0)
1016   */
1017  @CanIgnoreReturnValue
1018  @NonNullDecl
1019  public static <T extends Object> T checkNotNull(
1020      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, char p1, char p2) {
1021    if (obj == null) {
1022      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1023    }
1024    return obj;
1025  }
1026
1027  /**
1028   * Ensures that an object reference passed as a parameter to the calling method is not null.
1029   *
1030   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1031   *
1032   * @since 20.0 (varargs overload since 2.0)
1033   */
1034  @CanIgnoreReturnValue
1035  @NonNullDecl
1036  public static <T extends Object> T checkNotNull(
1037      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, char p1, int p2) {
1038    if (obj == null) {
1039      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1040    }
1041    return obj;
1042  }
1043
1044  /**
1045   * Ensures that an object reference passed as a parameter to the calling method is not null.
1046   *
1047   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1048   *
1049   * @since 20.0 (varargs overload since 2.0)
1050   */
1051  @CanIgnoreReturnValue
1052  @NonNullDecl
1053  public static <T extends Object> T checkNotNull(
1054      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, char p1, long p2) {
1055    if (obj == null) {
1056      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1057    }
1058    return obj;
1059  }
1060
1061  /**
1062   * Ensures that an object reference passed as a parameter to the calling method is not null.
1063   *
1064   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1065   *
1066   * @since 20.0 (varargs overload since 2.0)
1067   */
1068  @CanIgnoreReturnValue
1069  @NonNullDecl
1070  public static <T extends Object> T checkNotNull(
1071      @NonNullDecl T obj,
1072      @NullableDecl String errorMessageTemplate,
1073      char p1,
1074      @NullableDecl Object p2) {
1075    if (obj == null) {
1076      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1077    }
1078    return obj;
1079  }
1080
1081  /**
1082   * Ensures that an object reference passed as a parameter to the calling method is not null.
1083   *
1084   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1085   *
1086   * @since 20.0 (varargs overload since 2.0)
1087   */
1088  @CanIgnoreReturnValue
1089  @NonNullDecl
1090  public static <T extends Object> T checkNotNull(
1091      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, int p1, char p2) {
1092    if (obj == null) {
1093      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1094    }
1095    return obj;
1096  }
1097
1098  /**
1099   * Ensures that an object reference passed as a parameter to the calling method is not null.
1100   *
1101   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1102   *
1103   * @since 20.0 (varargs overload since 2.0)
1104   */
1105  @CanIgnoreReturnValue
1106  @NonNullDecl
1107  public static <T extends Object> T checkNotNull(
1108      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, int p1, int p2) {
1109    if (obj == null) {
1110      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1111    }
1112    return obj;
1113  }
1114
1115  /**
1116   * Ensures that an object reference passed as a parameter to the calling method is not null.
1117   *
1118   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1119   *
1120   * @since 20.0 (varargs overload since 2.0)
1121   */
1122  @CanIgnoreReturnValue
1123  @NonNullDecl
1124  public static <T extends Object> T checkNotNull(
1125      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, int p1, long p2) {
1126    if (obj == null) {
1127      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1128    }
1129    return obj;
1130  }
1131
1132  /**
1133   * Ensures that an object reference passed as a parameter to the calling method is not null.
1134   *
1135   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1136   *
1137   * @since 20.0 (varargs overload since 2.0)
1138   */
1139  @CanIgnoreReturnValue
1140  @NonNullDecl
1141  public static <T extends Object> T checkNotNull(
1142      @NonNullDecl T obj,
1143      @NullableDecl String errorMessageTemplate,
1144      int p1,
1145      @NullableDecl Object p2) {
1146    if (obj == null) {
1147      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1148    }
1149    return obj;
1150  }
1151
1152  /**
1153   * Ensures that an object reference passed as a parameter to the calling method is not null.
1154   *
1155   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1156   *
1157   * @since 20.0 (varargs overload since 2.0)
1158   */
1159  @CanIgnoreReturnValue
1160  @NonNullDecl
1161  public static <T extends Object> T checkNotNull(
1162      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, long p1, char p2) {
1163    if (obj == null) {
1164      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1165    }
1166    return obj;
1167  }
1168
1169  /**
1170   * Ensures that an object reference passed as a parameter to the calling method is not null.
1171   *
1172   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1173   *
1174   * @since 20.0 (varargs overload since 2.0)
1175   */
1176  @CanIgnoreReturnValue
1177  @NonNullDecl
1178  public static <T extends Object> T checkNotNull(
1179      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, long p1, int p2) {
1180    if (obj == null) {
1181      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1182    }
1183    return obj;
1184  }
1185
1186  /**
1187   * Ensures that an object reference passed as a parameter to the calling method is not null.
1188   *
1189   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1190   *
1191   * @since 20.0 (varargs overload since 2.0)
1192   */
1193  @CanIgnoreReturnValue
1194  @NonNullDecl
1195  public static <T extends Object> T checkNotNull(
1196      @NonNullDecl T obj, @NullableDecl String errorMessageTemplate, long p1, long p2) {
1197    if (obj == null) {
1198      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1199    }
1200    return obj;
1201  }
1202
1203  /**
1204   * Ensures that an object reference passed as a parameter to the calling method is not null.
1205   *
1206   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1207   *
1208   * @since 20.0 (varargs overload since 2.0)
1209   */
1210  @CanIgnoreReturnValue
1211  @NonNullDecl
1212  public static <T extends Object> T checkNotNull(
1213      @NonNullDecl T obj,
1214      @NullableDecl String errorMessageTemplate,
1215      long p1,
1216      @NullableDecl Object p2) {
1217    if (obj == null) {
1218      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1219    }
1220    return obj;
1221  }
1222
1223  /**
1224   * Ensures that an object reference passed as a parameter to the calling method is not null.
1225   *
1226   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1227   *
1228   * @since 20.0 (varargs overload since 2.0)
1229   */
1230  @CanIgnoreReturnValue
1231  @NonNullDecl
1232  public static <T extends Object> T checkNotNull(
1233      @NonNullDecl T obj,
1234      @NullableDecl String errorMessageTemplate,
1235      @NullableDecl Object p1,
1236      char p2) {
1237    if (obj == null) {
1238      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1239    }
1240    return obj;
1241  }
1242
1243  /**
1244   * Ensures that an object reference passed as a parameter to the calling method is not null.
1245   *
1246   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1247   *
1248   * @since 20.0 (varargs overload since 2.0)
1249   */
1250  @CanIgnoreReturnValue
1251  @NonNullDecl
1252  public static <T extends Object> T checkNotNull(
1253      @NonNullDecl T obj,
1254      @NullableDecl String errorMessageTemplate,
1255      @NullableDecl Object p1,
1256      int p2) {
1257    if (obj == null) {
1258      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1259    }
1260    return obj;
1261  }
1262
1263  /**
1264   * Ensures that an object reference passed as a parameter to the calling method is not null.
1265   *
1266   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1267   *
1268   * @since 20.0 (varargs overload since 2.0)
1269   */
1270  @CanIgnoreReturnValue
1271  @NonNullDecl
1272  public static <T extends Object> T checkNotNull(
1273      @NonNullDecl T obj,
1274      @NullableDecl String errorMessageTemplate,
1275      @NullableDecl Object p1,
1276      long p2) {
1277    if (obj == null) {
1278      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1279    }
1280    return obj;
1281  }
1282
1283  /**
1284   * Ensures that an object reference passed as a parameter to the calling method is not null.
1285   *
1286   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1287   *
1288   * @since 20.0 (varargs overload since 2.0)
1289   */
1290  @CanIgnoreReturnValue
1291  @NonNullDecl
1292  public static <T extends Object> T checkNotNull(
1293      @NonNullDecl T obj,
1294      @NullableDecl String errorMessageTemplate,
1295      @NullableDecl Object p1,
1296      @NullableDecl Object p2) {
1297    if (obj == null) {
1298      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2));
1299    }
1300    return obj;
1301  }
1302
1303  /**
1304   * Ensures that an object reference passed as a parameter to the calling method is not null.
1305   *
1306   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1307   *
1308   * @since 20.0 (varargs overload since 2.0)
1309   */
1310  @CanIgnoreReturnValue
1311  @NonNullDecl
1312  public static <T extends Object> T checkNotNull(
1313      @NonNullDecl T obj,
1314      @NullableDecl String errorMessageTemplate,
1315      @NullableDecl Object p1,
1316      @NullableDecl Object p2,
1317      @NullableDecl Object p3) {
1318    if (obj == null) {
1319      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2, p3));
1320    }
1321    return obj;
1322  }
1323
1324  /**
1325   * Ensures that an object reference passed as a parameter to the calling method is not null.
1326   *
1327   * <p>See {@link #checkNotNull(Object, String, Object...)} for details.
1328   *
1329   * @since 20.0 (varargs overload since 2.0)
1330   */
1331  @CanIgnoreReturnValue
1332  @NonNullDecl
1333  public static <T extends Object> T checkNotNull(
1334      @NonNullDecl T obj,
1335      @NullableDecl String errorMessageTemplate,
1336      @NullableDecl Object p1,
1337      @NullableDecl Object p2,
1338      @NullableDecl Object p3,
1339      @NullableDecl Object p4) {
1340    if (obj == null) {
1341      throw new NullPointerException(lenientFormat(errorMessageTemplate, p1, p2, p3, p4));
1342    }
1343    return obj;
1344  }
1345
1346  /*
1347   * All recent hotspots (as of 2009) *really* like to have the natural code
1348   *
1349   * if (guardExpression) {
1350   *    throw new BadException(messageExpression);
1351   * }
1352   *
1353   * refactored so that messageExpression is moved to a separate String-returning method.
1354   *
1355   * if (guardExpression) {
1356   *    throw new BadException(badMsg(...));
1357   * }
1358   *
1359   * The alternative natural refactorings into void or Exception-returning methods are much slower.
1360   * This is a big deal - we're talking factors of 2-8 in microbenchmarks, not just 10-20%. (This is
1361   * a hotspot optimizer bug, which should be fixed, but that's a separate, big project).
1362   *
1363   * The coding pattern above is heavily used in java.util, e.g. in ArrayList. There is a
1364   * RangeCheckMicroBenchmark in the JDK that was used to test this.
1365   *
1366   * But the methods in this class want to throw different exceptions, depending on the args, so it
1367   * appears that this pattern is not directly applicable. But we can use the ridiculous, devious
1368   * trick of throwing an exception in the middle of the construction of another exception. Hotspot
1369   * is fine with that.
1370   */
1371
1372  /**
1373   * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
1374   * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
1375   *
1376   * @param index a user-supplied index identifying an element of an array, list or string
1377   * @param size the size of that array, list or string
1378   * @return the value of {@code index}
1379   * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
1380   * @throws IllegalArgumentException if {@code size} is negative
1381   */
1382  @CanIgnoreReturnValue
1383  public static int checkElementIndex(int index, int size) {
1384    return checkElementIndex(index, size, "index");
1385  }
1386
1387  /**
1388   * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
1389   * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
1390   *
1391   * @param index a user-supplied index identifying an element of an array, list or string
1392   * @param size the size of that array, list or string
1393   * @param desc the text to use to describe this index in an error message
1394   * @return the value of {@code index}
1395   * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
1396   * @throws IllegalArgumentException if {@code size} is negative
1397   */
1398  @CanIgnoreReturnValue
1399  public static int checkElementIndex(int index, int size, @NullableDecl String desc) {
1400    // Carefully optimized for execution by hotspot (explanatory comment above)
1401    if (index < 0 || index >= size) {
1402      throw new IndexOutOfBoundsException(badElementIndex(index, size, desc));
1403    }
1404    return index;
1405  }
1406
1407  private static String badElementIndex(int index, int size, @NullableDecl String desc) {
1408    if (index < 0) {
1409      return lenientFormat("%s (%s) must not be negative", desc, index);
1410    } else if (size < 0) {
1411      throw new IllegalArgumentException("negative size: " + size);
1412    } else { // index >= size
1413      return lenientFormat("%s (%s) must be less than size (%s)", desc, index, size);
1414    }
1415  }
1416
1417  /**
1418   * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of
1419   * size {@code size}. A position index may range from zero to {@code size}, inclusive.
1420   *
1421   * @param index a user-supplied index identifying a position in an array, list or string
1422   * @param size the size of that array, list or string
1423   * @return the value of {@code index}
1424   * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size}
1425   * @throws IllegalArgumentException if {@code size} is negative
1426   */
1427  @CanIgnoreReturnValue
1428  public static int checkPositionIndex(int index, int size) {
1429    return checkPositionIndex(index, size, "index");
1430  }
1431
1432  /**
1433   * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of
1434   * size {@code size}. A position index may range from zero to {@code size}, inclusive.
1435   *
1436   * @param index a user-supplied index identifying a position in an array, list or string
1437   * @param size the size of that array, list or string
1438   * @param desc the text to use to describe this index in an error message
1439   * @return the value of {@code index}
1440   * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size}
1441   * @throws IllegalArgumentException if {@code size} is negative
1442   */
1443  @CanIgnoreReturnValue
1444  public static int checkPositionIndex(int index, int size, @NullableDecl String desc) {
1445    // Carefully optimized for execution by hotspot (explanatory comment above)
1446    if (index < 0 || index > size) {
1447      throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
1448    }
1449    return index;
1450  }
1451
1452  private static String badPositionIndex(int index, int size, @NullableDecl String desc) {
1453    if (index < 0) {
1454      return lenientFormat("%s (%s) must not be negative", desc, index);
1455    } else if (size < 0) {
1456      throw new IllegalArgumentException("negative size: " + size);
1457    } else { // index > size
1458      return lenientFormat("%s (%s) must not be greater than size (%s)", desc, index, size);
1459    }
1460  }
1461
1462  /**
1463   * Ensures that {@code start} and {@code end} specify valid <i>positions</i> in an array, list or
1464   * string of size {@code size}, and are in order. A position index may range from zero to {@code
1465   * size}, inclusive.
1466   *
1467   * @param start a user-supplied index identifying a starting position in an array, list or string
1468   * @param end a user-supplied index identifying an ending position in an array, list or string
1469   * @param size the size of that array, list or string
1470   * @throws IndexOutOfBoundsException if either index is negative or is greater than {@code size},
1471   *     or if {@code end} is less than {@code start}
1472   * @throws IllegalArgumentException if {@code size} is negative
1473   */
1474  public static void checkPositionIndexes(int start, int end, int size) {
1475    // Carefully optimized for execution by hotspot (explanatory comment above)
1476    if (start < 0 || end < start || end > size) {
1477      throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size));
1478    }
1479  }
1480
1481  private static String badPositionIndexes(int start, int end, int size) {
1482    if (start < 0 || start > size) {
1483      return badPositionIndex(start, size, "start index");
1484    }
1485    if (end < 0 || end > size) {
1486      return badPositionIndex(end, size, "end index");
1487    }
1488    // end < start
1489    return lenientFormat("end index (%s) must not be less than start index (%s)", end, start);
1490  }
1491}