1616
1717package io .opencensus .internal ;
1818
19- import javax . annotation . Nullable ;
19+ import java . util . List ;
2020
2121/*>>>
2222import org.checkerframework.checker.nullness.qual.NonNull;
@@ -32,11 +32,38 @@ private Utils() {}
3232 * {@code Preconditions.checkArgument(boolean, Object)} from Guava.
3333 *
3434 * @param isValid whether the argument check passed.
35- * @param message the message to use for the exception.
35+ * @param errorMessage the message to use for the exception. Will be converted to a string using
36+ * {@link String#valueOf(Object)}.
3637 */
37- public static void checkArgument (boolean isValid , String message ) {
38+ public static void checkArgument (
39+ boolean isValid , @ javax .annotation .Nullable Object errorMessage ) {
3840 if (!isValid ) {
39- throw new IllegalArgumentException (message );
41+ throw new IllegalArgumentException (String .valueOf (errorMessage ));
42+ }
43+ }
44+
45+ /**
46+ * Throws an {@link IllegalArgumentException} if the argument is false. This method is similar to
47+ * {@code Preconditions.checkArgument(boolean, Object)} from Guava.
48+ *
49+ * @param expression a boolean expression
50+ * @param errorMessageTemplate a template for the exception message should the check fail. The
51+ * message is formed by replacing each {@code %s} placeholder in the template with an
52+ * argument. These are matched by position - the first {@code %s} gets {@code
53+ * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
54+ * square braces. Unmatched placeholders will be left as-is.
55+ * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
56+ * are converted to strings using {@link String#valueOf(Object)}.
57+ * @throws IllegalArgumentException if {@code expression} is false
58+ * @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or
59+ * {@code errorMessageArgs} is null (don't let this happen)
60+ */
61+ public static void checkArgument (
62+ boolean expression ,
63+ String errorMessageTemplate ,
64+ @ javax .annotation .Nullable Object ... errorMessageArgs ) {
65+ if (!expression ) {
66+ throw new IllegalArgumentException (format (errorMessageTemplate , errorMessageArgs ));
4067 }
4168 }
4269
@@ -45,11 +72,12 @@ public static void checkArgument(boolean isValid, String message) {
4572 * {@code Preconditions.checkState(boolean, Object)} from Guava.
4673 *
4774 * @param isValid whether the state check passed.
48- * @param message the message to use for the exception.
75+ * @param errorMessage the message to use for the exception. Will be converted to a string using
76+ * {@link String#valueOf(Object)}.
4977 */
50- public static void checkState (boolean isValid , String message ) {
78+ public static void checkState (boolean isValid , @ javax . annotation . Nullable Object errorMessage ) {
5179 if (!isValid ) {
52- throw new IllegalStateException (message );
80+ throw new IllegalStateException (String . valueOf ( errorMessage ) );
5381 }
5482 }
5583
@@ -76,21 +104,88 @@ public static void checkIndex(int index, int size) {
76104 * Preconditions.checkNotNull(Object, Object)} from Guava.
77105 *
78106 * @param arg the argument to check for null.
79- * @param message the message to use for the exception.
107+ * @param errorMessage the message to use for the exception. Will be converted to a string using
108+ * {@link String#valueOf(Object)}.
80109 * @return the argument, if it passes the null check.
81110 */
82- public static <T /*>>> extends @NonNull Object*/ > T checkNotNull (T arg , String message ) {
111+ public static <T /*>>> extends @NonNull Object*/ > T checkNotNull (
112+ T arg , @ javax .annotation .Nullable Object errorMessage ) {
83113 if (arg == null ) {
84- throw new NullPointerException (message );
114+ throw new NullPointerException (String . valueOf ( errorMessage ) );
85115 }
86116 return arg ;
87117 }
88118
119+ /**
120+ * Throws a {@link NullPointerException} if any of the list elements is null.
121+ *
122+ * @param list the argument list to check for null.
123+ * @param errorMessage the message to use for the exception. Will be converted to a string using
124+ * {@link String#valueOf(Object)}.
125+ */
126+ public static <T /*>>> extends @NonNull Object*/ > void checkListElementNotNull (
127+ List <T > list , @ javax .annotation .Nullable Object errorMessage ) {
128+ for (T element : list ) {
129+ if (element == null ) {
130+ throw new NullPointerException (String .valueOf (errorMessage ));
131+ }
132+ }
133+ }
134+
89135 /**
90136 * Compares two Objects for equality. This functionality is provided by {@code
91137 * Objects.equal(Object, Object)} in Java 7.
92138 */
93- public static boolean equalsObjects (@ Nullable Object x , @ Nullable Object y ) {
139+ public static boolean equalsObjects (
140+ @ javax .annotation .Nullable Object x , @ javax .annotation .Nullable Object y ) {
94141 return x == null ? y == null : x .equals (y );
95142 }
143+
144+ /**
145+ * Substitutes each {@code %s} in {@code template} with an argument. These are matched by
146+ * position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than
147+ * placeholders, the unmatched arguments will be appended to the end of the formatted message in
148+ * square braces.
149+ *
150+ * <p>Copied from {@code Preconditions.format(String, Object...)} from Guava
151+ *
152+ * @param template a non-null string containing 0 or more {@code %s} placeholders.
153+ * @param args the arguments to be substituted into the message template. Arguments are converted
154+ * to strings using {@link String#valueOf(Object)}. Arguments can be null.
155+ */
156+ // Note that this is somewhat-improperly used from Verify.java as well.
157+ private static String format (String template , @ javax .annotation .Nullable Object ... args ) {
158+ // If no arguments return the template.
159+ if (args == null ) {
160+ return template ;
161+ }
162+
163+ // start substituting the arguments into the '%s' placeholders
164+ StringBuilder builder = new StringBuilder (template .length () + 16 * args .length );
165+ int templateStart = 0 ;
166+ int i = 0 ;
167+ while (i < args .length ) {
168+ int placeholderStart = template .indexOf ("%s" , templateStart );
169+ if (placeholderStart == -1 ) {
170+ break ;
171+ }
172+ builder .append (template , templateStart , placeholderStart );
173+ builder .append (args [i ++]);
174+ templateStart = placeholderStart + 2 ;
175+ }
176+ builder .append (template , templateStart , template .length ());
177+
178+ // if we run out of placeholders, append the extra args in square braces
179+ if (i < args .length ) {
180+ builder .append (" [" );
181+ builder .append (args [i ++]);
182+ while (i < args .length ) {
183+ builder .append (", " );
184+ builder .append (args [i ++]);
185+ }
186+ builder .append (']' );
187+ }
188+
189+ return builder .toString ();
190+ }
96191}
0 commit comments