Skip to content

Commit 2ed520e

Browse files
authored
pre-compile valid name Pattern to speed up assertValidName (#1987)
1 parent 31f3d3f commit 2ed520e

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/main/java/graphql/Assert.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collection;
44
import java.util.function.Supplier;
5+
import java.util.regex.Pattern;
56

67
import static java.lang.String.format;
78

@@ -100,6 +101,8 @@ public static void assertFalse(boolean condition) {
100101

101102
private static final String invalidNameErrorMessage = "Name must be non-null, non-empty and match [_A-Za-z][_0-9A-Za-z]* - was '%s'";
102103

104+
private static final Pattern validNamePattern = Pattern.compile("[_A-Za-z][_0-9A-Za-z]*");
105+
103106
/**
104107
* Validates that the Lexical token name matches the current spec.
105108
* currently non null, non empty,
@@ -108,7 +111,7 @@ public static void assertFalse(boolean condition) {
108111
* @return the name if valid, or AssertException if invalid.
109112
*/
110113
public static String assertValidName(String name) {
111-
if (name != null && !name.isEmpty() && name.matches("[_A-Za-z][_0-9A-Za-z]*")) {
114+
if (name != null && !name.isEmpty() && validNamePattern.matcher(name).matches()) {
112115
return name;
113116
}
114117
throw new AssertException(String.format(invalidNameErrorMessage, name));

0 commit comments

Comments
 (0)