Skip to content

Performance improvements in TemplateVariable.essence() #1764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/main/java/org/springframework/hateoas/TemplateVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,14 @@ public String asString() {
}

String essence() {

return String.format("%s%s%s", name,
limit != -1 ? ":".concat(String.valueOf(limit)) : "",
isComposite() ? "*" : "");
StringBuilder essenceBuilder = new StringBuilder(name);
if (limit != -1) {
essenceBuilder = essenceBuilder.append(":").append(limit);
}
if (isComposite()) {
essenceBuilder = essenceBuilder.append("*");
}
return essenceBuilder.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TemplateVariablesUnitTest {
* @see #137
*/
@Test
void rendersNoTempalteVariablesAsEmptyString() {
void rendersNoTemplateVariablesAsEmptyString() {
assertThat(TemplateVariables.NONE.toString()).isEqualTo("");
}

Expand All @@ -49,6 +49,18 @@ void rendersSingleVariableCorrectly() {
assertThat(variables.toString()).isEqualTo("{/foo}");
}

@Test
void rendersCompositeVariableCorrectly() {
TemplateVariables variables = new TemplateVariables(new TemplateVariable("bars", SIMPLE).composite());
assertThat(variables.toString()).isEqualTo("{bars*}");
}

@Test
void rendersLimitedVariableCorrectly() {
TemplateVariables variables = new TemplateVariables(new TemplateVariable("apples", SIMPLE).limit(5));
assertThat(variables.toString()).isEqualTo("{apples:5}");
}

/**
* @see #137
*/
Expand Down Expand Up @@ -151,7 +163,7 @@ void considersRequestParameterVariablesEquivalent() {
* @see #217
*/
@Test
void considersFragementVariable() {
void considersFragmentVariable() {

assertThat(new TemplateVariable("foo", VariableType.FRAGMENT).isFragment()).isTrue();
assertThat(new TemplateVariable("foo", VariableType.REQUEST_PARAM).isFragment()).isFalse();
Expand Down