Skip to content

Commit 6480bc4

Browse files
joshgordbrharrington
authored andcommitted
add a checkArgument that avoids varargs (Netflix#465)
Avoids varargs and the allocation overhead of creating a string array. During application profiling found that some allocation can be avoided if we explicitly invoke the checkArgument method without varargs Allocation profiling via async-profiler: ``` --- 1451114625215846968 bytes (21.40%), 10371 samples [ 0] java.lang.String[] [ 1] com.netflix.servo.tag.BasicTag.checkNotEmpty [ 2] com.netflix.servo.tag.BasicTag.<init> [ 3] com.netflix.servo.tag.Tags.newTag [ 4] com.netflix.servo.monitor.MonitorConfig$Builder.withTag ```
1 parent 3252cd4 commit 6480bc4

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

servo-core/src/main/java/com/netflix/servo/util/Preconditions.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,28 @@ public static <T> T checkNotNull(T obj, String name) {
4343
* @throws IllegalArgumentException if {@code expression} is false
4444
*/
4545
public static void checkArgument(boolean expression, String errorMessage) {
46-
checkArgument(expression, errorMessage, null);
46+
checkArgument(expression, errorMessage, (String) null);
47+
}
48+
49+
/**
50+
* Ensures the truth of an expression involving one or more parameters to the
51+
* calling method.
52+
*
53+
*
54+
* @param expression a boolean expression
55+
* @param errorMessage the error message that can be a formattable string
56+
* @param arg argument if using a formatted string
57+
* @throws IllegalArgumentException if {@code expression} is false
58+
*/
59+
public static void checkArgument(boolean expression, String errorMessage, String arg) {
60+
if (!expression) {
61+
if (arg != null) {
62+
String message = String.format(errorMessage, arg);
63+
throw new IllegalArgumentException(message);
64+
} else {
65+
throw new IllegalArgumentException(errorMessage);
66+
}
67+
}
4768
}
4869

4970
/**

0 commit comments

Comments
 (0)