URI Template Expression.
| 20 | |
| 21 | /** URI Template Expression. */ |
| 22 | abstract class Expression implements TemplateChunk { |
| 23 | |
| 24 | private String name; |
| 25 | private Pattern pattern; |
| 26 | |
| 27 | /** |
| 28 | * Create a new Expression. |
| 29 | * |
| 30 | * @param name of the variable |
| 31 | * @param pattern the resolved variable must adhere to, optional. |
| 32 | */ |
| 33 | Expression(String name, String pattern) { |
| 34 | this.name = name; |
| 35 | Optional.ofNullable(pattern).ifPresent(s -> this.pattern = Pattern.compile(s)); |
| 36 | } |
| 37 | |
| 38 | abstract String expand(Object variable, boolean encode); |
| 39 | |
| 40 | public String getName() { |
| 41 | return this.name; |
| 42 | } |
| 43 | |
| 44 | Pattern getPattern() { |
| 45 | return pattern; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks if the provided value matches the variable pattern, if one is defined. Always true if no |
| 50 | * pattern is defined. |
| 51 | * |
| 52 | * @param value to check. |
| 53 | * @return true if it matches. |
| 54 | */ |
| 55 | boolean matches(String value) { |
| 56 | if (pattern == null) { |
| 57 | return true; |
| 58 | } |
| 59 | return pattern.matcher(value).matches(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String getValue() { |
| 64 | if (this.pattern != null) { |
| 65 | return "{" + this.name + ":" + this.pattern + "}"; |
| 66 | } |
| 67 | return "{" + this.name + "}"; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return this.getValue(); |
| 73 | } |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected