/** * Returns the {@link VariableType} for the given variable key. * * @param key must not be {@literal null}. * @return */ public static TemplateVariable.VariableType from(String key) { return Arrays.stream(values()) // .filter(type -> type.key.equals(key)) // .findFirst() // .orElseThrow(() -> new IllegalArgumentException("Unsupported variable type " + key + "!")); }
TemplateVariable.VariableType type = TemplateVariable.VariableType.from(matcher.group(1));
TemplateVariable.VariableType type = TemplateVariable.VariableType.from(matcher.group(1));
/** * Creates a new {@link UriTemplate} using the given template string. * * @param template must not be {@literal null} or empty. */ public UriTemplate(String template) { Assert.hasText(template, "Template must not be null or empty!"); Matcher matcher = VARIABLE_REGEX.matcher(template); int baseUriEndIndex = template.length(); List<TemplateVariable> variables = new ArrayList<TemplateVariable>(); while (matcher.find()) { int start = matcher.start(0); VariableType type = VariableType.from(matcher.group(1)); String[] names = matcher.group(2).split(","); for (String name : names) { TemplateVariable variable = new TemplateVariable(name, type); if (!variable.isRequired() && start < baseUriEndIndex) { baseUriEndIndex = start; } variables.add(variable); } } this.variables = variables.isEmpty() ? TemplateVariables.NONE : new TemplateVariables(variables); this.baseUri = template.substring(0, baseUriEndIndex); }
public boolean canBeCombinedWith(VariableType type) { return this.equals(type) || COMBINABLE_TYPES.contains(this) && COMBINABLE_TYPES.contains(type); }
/** * Returns the {@link VariableType} for the given variable key. * * @param key must not be {@literal null}. * @return */ public static TemplateVariable.VariableType from(String key) { for (TemplateVariable.VariableType type : values()) { if (type.key.equals(key)) { return type; } } throw new IllegalArgumentException("Unsupported variable type " + key + "!"); }
/** * Returns whether the variable is a fragment one. * * @return */ boolean isFragment() { return type.equals(FRAGMENT); }
@Override public String toString() { String base = String.format("{%s%s}", type.toString(), name); return StringUtils.hasText(description) ? String.format("%s - %s", base, description) : base; }
/** * Returns whether the template variable is optional, which means the template can be expanded to a URI without a * value given for that variable. * * @return */ boolean isRequired() { return !type.isOptional(); }
/** * Returns whether the given {@link TemplateVariable} is of the same type as the current one. * * @param variable must not be {@literal null}. * @return */ boolean isCombinable(TemplateVariable variable) { return this.type.canBeCombinedWith(variable.type); }
public boolean canBeCombinedWith(VariableType type) { return this.equals(type) || COMBINABLE_TYPES.contains(this) && COMBINABLE_TYPES.contains(type); }
/** * Returns whether the variable is a fragment one. * * @return */ boolean isFragment() { return type.equals(FRAGMENT); }
/** * Returns whether the given {@link TemplateVariable} is of the same type as the current one. * * @param variable must not be {@literal null}. * @return */ boolean isCombinable(TemplateVariable variable) { return this.type.canBeCombinedWith(variable.type); }
/** * Returns whether the template variable is optional, which means the template can be expanded to a URI without a * value given for that variable. * * @return */ boolean isRequired() { return !type.isOptional(); }
@Override public String toString() { String base = String.format("{%s%s}", type.toString(), name); return StringUtils.hasText(description) ? String.format("%s - %s", base, description) : base; }