-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Make the graphql-java Asserts truly lazy and hence more efficient #1913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8d06b50
c69e656
f5b087b
0d8cf32
d5359f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,26 @@ | ||
| package graphql; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
||
| @SuppressWarnings("TypeParameterUnusedInFormals") | ||
| @Internal | ||
| public class Assert { | ||
|
|
||
| public static <T> T assertNotNull(T object, String format, Object... args) { | ||
| public static <T> T assertNotNull(T object, Supplier<String> msg) { | ||
| if (object != null) { | ||
| return object; | ||
| } | ||
| throw new AssertException(format(format, args)); | ||
| throw new AssertException(msg.get()); | ||
| } | ||
|
|
||
| public static <T> T assertNotNullWithNPE(T object, String format, Object... args) { | ||
| public static <T> T assertNotNullWithNPE(T object, Supplier<String> msg) { | ||
| if (object != null) { | ||
| return object; | ||
| } | ||
| throw new NullPointerException(format(format, args)); | ||
| throw new NullPointerException(msg.get()); | ||
| } | ||
|
|
||
| public static <T> T assertNotNull(T object) { | ||
|
|
@@ -29,11 +30,11 @@ public static <T> T assertNotNull(T object) { | |
| throw new AssertException("Object required to be not null"); | ||
| } | ||
|
|
||
| public static <T> void assertNull(T object, String format, Object... args) { | ||
| public static <T> void assertNull(T object, Supplier<String> msg) { | ||
| if (object == null) { | ||
| return; | ||
| } | ||
| throw new AssertException(format(format, args)); | ||
| throw new AssertException(msg.get()); | ||
| } | ||
|
|
||
| public static <T> void assertNull(T object) { | ||
|
|
@@ -62,18 +63,18 @@ public static <T> Collection<T> assertNotEmpty(Collection<T> collection) { | |
| return collection; | ||
| } | ||
|
|
||
| public static <T> Collection<T> assertNotEmpty(Collection<T> collection, String format, Object... args) { | ||
| public static <T> Collection<T> assertNotEmpty(Collection<T> collection, Supplier<String> msg) { | ||
| if (collection == null || collection.isEmpty()) { | ||
| throw new AssertException(format(format, args)); | ||
| throw new AssertException(msg.get()); | ||
| } | ||
| return collection; | ||
| } | ||
|
|
||
| public static void assertTrue(boolean condition, String format, Object... args) { | ||
| public static void assertTrue(boolean condition, Supplier<String> msg) { | ||
| if (condition) { | ||
| return; | ||
| } | ||
| throw new AssertException(format(format, args)); | ||
| throw new AssertException(msg.get()); | ||
| } | ||
|
|
||
| public static void assertTrue(boolean condition) { | ||
|
|
@@ -83,11 +84,18 @@ public static void assertTrue(boolean condition) { | |
| throw new AssertException("condition expected to be true"); | ||
| } | ||
|
|
||
| public static void assertFalse(boolean condition, String format, Object... args) { | ||
| public static void assertFalse(boolean condition, Supplier<String> msg) { | ||
| if (!condition) { | ||
| return; | ||
| } | ||
| throw new AssertException(format(format, args)); | ||
| throw new AssertException(msg.get()); | ||
| } | ||
|
|
||
| public static void assertFalse(boolean condition) { | ||
| if (!condition) { | ||
| return; | ||
| } | ||
| throw new AssertException("condition expected to be false"); | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above was missing but present for assertTrue so I made it
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertShouldNeverHappen can be as slow as it likes - its ALWAYS going to assert. I mean we could make it a supplier based on consistency but....I thought its ok to leave it |
||
|
|
||
| private static final String invalidNameErrorMessage = "Name must be non-null, non-empty and match [_A-Za-z][_0-9A-Za-z]* - was '%s'"; | ||
|
|
@@ -97,7 +105,6 @@ public static void assertFalse(boolean condition, String format, Object... args) | |
| * currently non null, non empty, | ||
| * | ||
| * @param name - the name to be validated. | ||
| * | ||
| * @return the name if valid, or AssertException if invalid. | ||
| */ | ||
| public static String assertValidName(String name) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the KEY file changed and its test.
The others are all knock on effects