17
17
package io .opencensus .internal ;
18
18
19
19
import java .util .List ;
20
- import javax .annotation .Nullable ;
21
20
22
21
/*>>>
23
22
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -33,11 +32,38 @@ private Utils() {}
33
32
* {@code Preconditions.checkArgument(boolean, Object)} from Guava.
34
33
*
35
34
* @param isValid whether the argument check passed.
36
- * @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)}.
37
37
*/
38
- public static void checkArgument (boolean isValid , String message ) {
38
+ public static void checkArgument (
39
+ boolean isValid , @ javax .annotation .Nullable Object errorMessage ) {
39
40
if (!isValid ) {
40
- 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 ));
41
67
}
42
68
}
43
69
@@ -46,11 +72,12 @@ public static void checkArgument(boolean isValid, String message) {
46
72
* {@code Preconditions.checkState(boolean, Object)} from Guava.
47
73
*
48
74
* @param isValid whether the state check passed.
49
- * @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)}.
50
77
*/
51
- public static void checkState (boolean isValid , String message ) {
78
+ public static void checkState (boolean isValid , @ javax . annotation . Nullable Object errorMessage ) {
52
79
if (!isValid ) {
53
- throw new IllegalStateException (message );
80
+ throw new IllegalStateException (String . valueOf ( errorMessage ) );
54
81
}
55
82
}
56
83
@@ -77,12 +104,14 @@ public static void checkIndex(int index, int size) {
77
104
* Preconditions.checkNotNull(Object, Object)} from Guava.
78
105
*
79
106
* @param arg the argument to check for null.
80
- * @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)}.
81
109
* @return the argument, if it passes the null check.
82
110
*/
83
- 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 ) {
84
113
if (arg == null ) {
85
- throw new NullPointerException (message );
114
+ throw new NullPointerException (String . valueOf ( errorMessage ) );
86
115
}
87
116
return arg ;
88
117
}
@@ -91,13 +120,14 @@ public static void checkIndex(int index, int size) {
91
120
* Throws a {@link NullPointerException} if any of the list elements is null.
92
121
*
93
122
* @param list the argument list to check for null.
94
- * @param message the message to use for the exception.
123
+ * @param errorMessage the message to use for the exception. Will be converted to a string using
124
+ * {@link String#valueOf(Object)}.
95
125
*/
96
126
public static <T /*>>> extends @NonNull Object*/ > void checkListElementNotNull (
97
- List <T > list , String message ) {
127
+ List <T > list , @ javax . annotation . Nullable Object errorMessage ) {
98
128
for (T element : list ) {
99
129
if (element == null ) {
100
- throw new NullPointerException (message );
130
+ throw new NullPointerException (String . valueOf ( errorMessage ) );
101
131
}
102
132
}
103
133
}
@@ -106,7 +136,56 @@ public static void checkIndex(int index, int size) {
106
136
* Compares two Objects for equality. This functionality is provided by {@code
107
137
* Objects.equal(Object, Object)} in Java 7.
108
138
*/
109
- 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 ) {
110
141
return x == null ? y == null : x .equals (y );
111
142
}
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
+ }
112
191
}
0 commit comments