#137 - Improved TemplateVariables to combine variables.

Request parameters variables {?…} and continued ones {&…} are now combined by TemplateVariables. Improved detection of the base URI and adapted the rendering logic accordingly. UriTemplates can now be augmented with additional TemplateVariables.

Added equals(…) and hashCode() methods for TempalteVariables.
This commit is contained in:
Oliver Gierke
2014-01-23 14:09:35 +01:00
parent c790a5d834
commit 9caa352470
5 changed files with 142 additions and 12 deletions

View File

@@ -60,15 +60,17 @@ public class UriTemplate implements Iterable<TemplateVariable> {
int start = matcher.start(0);
if (start < baseUriEndIndex) {
baseUriEndIndex = start;
}
VariableType type = VariableType.from(matcher.group(1));
String[] names = matcher.group(2).split(",");
for (String name : names) {
variables.add(new TemplateVariable(name, type));
TemplateVariable variable = new TemplateVariable(name, type);
if (!variable.isRequired() && start < baseUriEndIndex) {
baseUriEndIndex = start;
}
variables.add(variable);
}
}
@@ -90,6 +92,21 @@ public class UriTemplate implements Iterable<TemplateVariable> {
this.variables = variables == null ? TemplateVariables.NONE : variables;
}
/**
* Creates a new {@link UriTemplate} with the current {@link TemplateVariable}s augmented with the given ones.
*
* @param variables can be {@literal null}.
* @return
*/
public UriTemplate with(TemplateVariables variables) {
if (variables == null) {
return this;
}
return new UriTemplate(baseUri, this.variables.concat(variables));
}
/**
* Returns whether the given candidate is a URI template.
*
@@ -193,7 +210,20 @@ public class UriTemplate implements Iterable<TemplateVariable> {
*/
@Override
public String toString() {
return baseUri + variables.toString();
return baseUri + getOptionalVariables().toString();
}
private TemplateVariables getOptionalVariables() {
List<TemplateVariable> result = new ArrayList<TemplateVariable>();
for (TemplateVariable variable : this) {
if (!variable.isRequired()) {
result.add(variable);
}
}
return new TemplateVariables(result);
}
/**