private TemplateVariables getOptionalVariables() { return variables.asList().stream() // .filter(variable -> !variable.isRequired()) // .collect(Collectors.collectingAndThen(Collectors.toList(), TemplateVariables::new)); }
if (!variable.isRequired() && start < baseUriEndIndex) { baseUriEndIndex = start;
if (variable.isRequired()) { throw new IllegalArgumentException( String.format("Template variable %s is required but no value was given!", variable.getName()));
private TemplateVariables getOptionalVariables() { List<TemplateVariable> result = new ArrayList<TemplateVariable>(); for (TemplateVariable variable : this) { if (!variable.isRequired()) { result.add(variable); } } return new TemplateVariables(result); }
/** * 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); }
if (variable.isRequired()) { throw new IllegalArgumentException(String.format("Template variable %s is required but no value was given!", variable.getName()));