|
1 | 1 | package graphql.scalars.regex; |
2 | 2 |
|
3 | 3 | import graphql.Assert; |
| 4 | +import graphql.PublicApi; |
4 | 5 | import graphql.language.StringValue; |
5 | 6 | import graphql.schema.Coercing; |
6 | 7 | import graphql.schema.CoercingParseLiteralException; |
7 | 8 | import graphql.schema.CoercingParseValueException; |
8 | 9 | import graphql.schema.CoercingSerializeException; |
9 | 10 | import graphql.schema.GraphQLScalarType; |
10 | 11 |
|
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
11 | 15 | import java.util.function.Function; |
12 | 16 | import java.util.regex.Matcher; |
13 | 17 | import java.util.regex.Pattern; |
|
18 | 22 | * This is really a scalar factory for creating new scalar String types that are based on a value matching |
19 | 23 | * a regular expression. |
20 | 24 | */ |
| 25 | +@PublicApi |
21 | 26 | public class RegexScalar extends GraphQLScalarType { |
22 | 27 |
|
23 | | - private RegexScalar(String name, String description, Coercing coercing) { |
24 | | - super(name, description, coercing); |
25 | | - } |
26 | | - |
27 | 28 | /** |
28 | | - * Creates a new scalar that uses the specified {@link java.util.regex.Pattern} to control |
29 | | - * the values that are allowed. |
30 | | - * |
31 | | - * @param name the name of the scalar |
32 | | - * @param pattern the allowable values pattern |
33 | | - * |
34 | | - * @return a new regex scalar |
| 29 | + * A builder for {@link graphql.scalars.regex.RegexScalar} |
35 | 30 | */ |
36 | | - public static RegexScalar regexScalar(String name, Pattern pattern) { |
37 | | - return regexScalarImpl(name, null, pattern); |
38 | | - } |
| 31 | + public static class Builder { |
| 32 | + private String name; |
| 33 | + private String description; |
| 34 | + private List<Pattern> patterns = new ArrayList<>(); |
39 | 35 |
|
40 | | - /** |
41 | | - * Creates a new scalar that uses the specified {@link java.util.regex.Pattern} to control |
42 | | - * the values that are allowed. |
43 | | - * |
44 | | - * @param name the name of the scalar |
45 | | - * @param description the description of the scalar |
46 | | - * @param pattern the allowable values pattern |
47 | | - * |
48 | | - * @return a new regex scalar |
49 | | - */ |
50 | | - public static RegexScalar regexScalar(String name, String description, Pattern pattern) { |
51 | | - return regexScalarImpl(name, description, pattern); |
52 | | - } |
| 36 | + /** |
| 37 | + * Sets the name of the regex scalar |
| 38 | + * |
| 39 | + * @param name the name of the regex scalar |
| 40 | + * |
| 41 | + * @return this builder |
| 42 | + */ |
| 43 | + public Builder name(String name) { |
| 44 | + this.name = name; |
| 45 | + return this; |
| 46 | + } |
53 | 47 |
|
54 | | - /** |
55 | | - * Creates a new scalar that uses the specified {@link java.util.regex.Pattern}s to control |
56 | | - * the values that are allowed. |
57 | | - * |
58 | | - * @param name the name of the scalar |
59 | | - * @param patterns the allowable values patterns |
60 | | - * |
61 | | - * @return a new regex scalar |
62 | | - */ |
63 | | - public static RegexScalar regexScalar(String name, Pattern... patterns) { |
64 | | - return regexScalarImpl(name, null, patterns); |
| 48 | + /** |
| 49 | + * Sets the name of the regex scalar |
| 50 | + * |
| 51 | + * @param description the description of the regex scalar |
| 52 | + * |
| 53 | + * @return this builder |
| 54 | + */ |
| 55 | + public Builder description(String description) { |
| 56 | + this.description = description; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Adds a {@link java.util.regex.Pattern} that controls the acceptable value for this scalar |
| 62 | + * |
| 63 | + * @param pattern the regex pattern |
| 64 | + * |
| 65 | + * @return this builder |
| 66 | + */ |
| 67 | + public Builder addPattern(Pattern pattern) { |
| 68 | + this.patterns.add(pattern); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds a {@link java.util.regex.Pattern} that controls the acceptable value for this scalar |
| 74 | + * |
| 75 | + * @param patterns one of more regex patterns |
| 76 | + * |
| 77 | + * @return this builder |
| 78 | + */ |
| 79 | + public Builder addPatterns(Pattern... patterns) { |
| 80 | + Collections.addAll(this.patterns, patterns); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return the built {@link graphql.scalars.regex.RegexScalar} |
| 86 | + */ |
| 87 | + public RegexScalar build() { |
| 88 | + Assert.assertNotNull(name); |
| 89 | + return regexScalarImpl(name, description, patterns); |
| 90 | + } |
65 | 91 | } |
66 | 92 |
|
67 | | - /** |
68 | | - * Creates a new scalar that uses the specified {@link java.util.regex.Pattern}s to control |
69 | | - * the values that are allowed. |
70 | | - * |
71 | | - * @param name the name of the scalar |
72 | | - * @param description the description of the scalar |
73 | | - * @param patterns the allowable values patterns |
74 | | - * |
75 | | - * @return a new regex scalar |
76 | | - */ |
77 | | - public static RegexScalar regexScalar(String name, String description, Pattern... patterns) { |
78 | | - return regexScalarImpl(name, description, patterns); |
| 93 | + private RegexScalar(String name, String description, Coercing coercing) { |
| 94 | + super(name, description, coercing); |
79 | 95 | } |
80 | 96 |
|
81 | | - private static RegexScalar regexScalarImpl(String name, String description, Pattern... patterns) { |
| 97 | + private static RegexScalar regexScalarImpl(String name, String description, List<Pattern> patterns) { |
82 | 98 | Assert.assertNotNull(patterns); |
83 | 99 | return new RegexScalar(name, description, new Coercing<String, String>() { |
84 | 100 | @Override |
|
0 commit comments