Skip to content

Commit

Permalink
#1172 - Polishing.
Browse files Browse the repository at this point in the history
A bit of Javadoc on the factory methods. Avoiding abbreviations in method names, shorter parameter names. More trailing whitespace removal.

Reformatting of method invocation in test cases.

Original pull request: #1192.
  • Loading branch information
odrotbohm committed Feb 11, 2020
1 parent f1bc790 commit d1c1fbb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
62 changes: 34 additions & 28 deletions src/main/java/org/springframework/hateoas/TemplateVariable.java
Expand Up @@ -29,7 +29,7 @@

/**
* A single template variable.
*
*
* @author Oliver Gierke
* @author JamesE Richardson
*/
Expand All @@ -45,7 +45,7 @@ public final class TemplateVariable implements Serializable {

/**
* Creates a new {@link TemplateVariable} with the given name and type.
*
*
* @param name must not be {@literal null} or empty.
* @param type must not be {@literal null}.
*/
Expand All @@ -55,7 +55,7 @@ public TemplateVariable(String name, TemplateVariable.VariableType type) {

/**
* Creates a new {@link TemplateVariable} with the given name, type and description.
*
*
* @param name must not be {@literal null} or empty.
* @param type must not be {@literal null}.
* @param description must not be {@literal null}.
Expand All @@ -74,38 +74,42 @@ public TemplateVariable(String name, TemplateVariable.VariableType type, String
/**
* Static helper to fashion {@link VariableType#PATH_VARIABLE} variables.
*
* @param pathVariable
* @param variable must not be {@literal null} or empty.
* @return
* @since 1.1
*/
public static TemplateVariable pathVariable(String pathVariable) {
return new TemplateVariable(pathVariable, VariableType.PATH_VARIABLE);
public static TemplateVariable pathVariable(String variable) {
return new TemplateVariable(variable, VariableType.PATH_VARIABLE);
}

/**
* Static helper to fashion {@link VariableType#REQUEST_PARAM} variables.
*
* @param requestParam
* @param parameter must not be {@literal null} or empty.
* @return
* @since 1.1
*/
public static TemplateVariable requestParam(String requestParam) {
return new TemplateVariable(requestParam, VariableType.REQUEST_PARAM);
public static TemplateVariable requestParameter(String parameter) {
return new TemplateVariable(parameter, VariableType.REQUEST_PARAM);
}

/**
* Static helper to fashion {@link VariableType#REQUEST_PARAM_CONTINUED} variables.
*
* @param requestParam
* @param parameter must not be {@literal null} or empty.
* @return
* @since 1.1
*/
public static TemplateVariable requestParamContinued(String requestParam) {
return new TemplateVariable(requestParam, VariableType.REQUEST_PARAM_CONTINUED);
public static TemplateVariable requestParameterContinued(String parameter) {
return new TemplateVariable(parameter, VariableType.REQUEST_PARAM_CONTINUED);
}

/**
* Static helper to fashion {@link VariableType#SEGMENT} variables.
*
* @param segment
* @param segment must not be {@literal null} or empty.
* @return
* @since 1.1
*/
public static TemplateVariable segment(String segment) {
return new TemplateVariable(segment, VariableType.SEGMENT);
Expand All @@ -114,8 +118,9 @@ public static TemplateVariable segment(String segment) {
/**
* Static helper to fashion {@link VariableType#FRAGMENT} variables.
*
* @param fragment
* @param fragment must not be {@literal null} or empty.
* @return
* @since 1.1
*/
public static TemplateVariable fragment(String fragment) {
return new TemplateVariable(fragment, VariableType.FRAGMENT);
Expand All @@ -124,16 +129,17 @@ public static TemplateVariable fragment(String fragment) {
/**
* Static helper to fashion {@link VariableType#COMPOSITE_PARAM} variables.
*
* @param compositeParam
* @param parameter must not be {@literal null} or empty.
* @return
* @since 1.1
*/
public static TemplateVariable compositeParam(String compositeParam) {
return new TemplateVariable(compositeParam, VariableType.COMPOSITE_PARAM);
public static TemplateVariable compositeParameter(String parameter) {
return new TemplateVariable(parameter, VariableType.COMPOSITE_PARAM);
}

/**
* Returns whether the variable has a description.
*
*
* @return
*/
public boolean hasDescription() {
Expand All @@ -143,7 +149,7 @@ public boolean hasDescription() {
/**
* 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() {
Expand All @@ -152,7 +158,7 @@ boolean isRequired() {

/**
* Returns whether the given {@link TemplateVariable} is of the same type as the current one.
*
*
* @param variable must not be {@literal null}.
* @return
*/
Expand All @@ -163,7 +169,7 @@ boolean isCombinable(TemplateVariable variable) {
/**
* Returns whether the given {@link TemplateVariable} is logically equivalent to the given one. This considers request
* parameter variables equivalent independently from whether they're continued or not.
*
*
* @param variable
* @return
*/
Expand All @@ -173,7 +179,7 @@ boolean isEquivalent(TemplateVariable variable) {

/**
* Returns whether the current {@link TemplateVariable} is representing a request parameter.
*
*
* @return
*/
boolean isRequestParameterVariable() {
Expand All @@ -182,14 +188,14 @@ boolean isRequestParameterVariable() {

/**
* Returns whether the variable is a fragment one.
*
*
* @return
*/
boolean isFragment() {
return type.equals(FRAGMENT);
}

/*
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
Expand All @@ -202,7 +208,7 @@ public String toString() {

/**
* An enumeration for all supported variable types.
*
*
* @author Oliver Gierke
*/
public enum VariableType {
Expand All @@ -227,7 +233,7 @@ public enum VariableType {

/**
* Returns whether the variable of this type is optional.
*
*
* @return
*/
public boolean isOptional() {
Expand All @@ -240,7 +246,7 @@ public boolean canBeCombinedWith(VariableType type) {

/**
* Returns the {@link VariableType} for the given variable key.
*
*
* @param key must not be {@literal null}.
* @return
*/
Expand Down
53 changes: 37 additions & 16 deletions src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java
Expand Up @@ -39,7 +39,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.hateoas.TemplateVariable.*;
import org.springframework.hateoas.TemplateVariable.VariableType;

/**
* Unit tests for {@link UriTemplate}.
Expand Down Expand Up @@ -301,23 +301,44 @@ void expandsTemplateWithAddedVariable() {
assertThat(template.expand("value").toString()).isEqualTo("/foo?bar=value");
}

@Test // 1172
@Test // #1172
void useHelperMethodsToBuildUriTemplates() {

assertThat(UriTemplate.of("/foo").with(pathVariable("var")).getVariableNames()).containsExactly("var");

assertThat(UriTemplate.of("/foo").with(requestParam("var")).with(requestParamContinued("var2")).toString())
.isEqualTo("/foo{?var,var2}");
assertThat(UriTemplate.of("/foo").with(requestParam("var")).with(requestParam("var2")).toString())
.isEqualTo("/foo{?var,var2}");

assertThat(UriTemplate.of("/foo").with(requestParamContinued("var2")).toString()).isEqualTo("/foo{&var2}");

assertThat(UriTemplate.of("/foo").with(segment("var")).toString()).isEqualTo("/foo{/var}");

assertThat(UriTemplate.of("/foo").with(fragment("var")).toString()).isEqualTo("/foo{#var}");

assertThat(UriTemplate.of("/foo").with(compositeParam("var")).toString()).isEqualTo("/foo{*var}");
assertThat(UriTemplate.of("/foo") //
.with(pathVariable("var")) //
.getVariableNames()) //
.containsExactly("var");

assertThat(UriTemplate.of("/foo") //
.with(requestParameter("var")) //
.with(requestParameterContinued("var2")) //
.toString()) //
.isEqualTo("/foo{?var,var2}");

assertThat(UriTemplate.of("/foo") //
.with(requestParameter("var")) //
.with(requestParameter("var2")) //
.toString()) //
.isEqualTo("/foo{?var,var2}");

assertThat(UriTemplate.of("/foo") //
.with(requestParameterContinued("var2")) //
.toString()).isEqualTo("/foo{&var2}");

assertThat(UriTemplate.of("/foo") //
.with(segment("var")) //
.toString()) //
.isEqualTo("/foo{/var}");

assertThat(UriTemplate.of("/foo") //
.with(fragment("var")) //
.toString()) //
.isEqualTo("/foo{#var}");

assertThat(UriTemplate.of("/foo") //
.with(compositeParameter("var")) //
.toString()) //
.isEqualTo("/foo{*var}");
}

private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
Expand Down

0 comments on commit d1c1fbb

Please sign in to comment.