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