Skip to content

Commit

Permalink
#2041 - Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Nov 13, 2023
1 parent a636130 commit 5d1f86f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/springframework/hateoas/TemplateVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -558,6 +559,16 @@ int findIndexWithin(String template) {
return template.indexOf(key);
}

Stream<VariableType> getFollowingTypes() {

return switch (this) {
case PATH_SEGMENT -> Stream.of(PATH_STYLE_PARAMETER, REQUEST_PARAM, FRAGMENT);
case PATH_STYLE_PARAMETER -> Stream.of(REQUEST_PARAM, FRAGMENT);
case REQUEST_PARAM, REQUEST_PARAM_CONTINUED -> Stream.of(FRAGMENT);
default -> Stream.<VariableType> empty();
};
}

/**
* Returns the {@link VariableType} for the given variable key.
*
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/org/springframework/hateoas/UriTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.hateoas;

import static org.springframework.hateoas.TemplateVariable.VariableType.*;

import java.io.Serializable;
import java.net.URI;
import java.nio.charset.StandardCharsets;
Expand All @@ -30,7 +28,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.hateoas.TemplateVariable.VariableType;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -481,14 +478,7 @@ boolean canBeCombinedWith(VariableType type) {
*/
String insertInto(String template) {

var followingTypes = switch (type) {
case PATH_SEGMENT -> Stream.of(PATH_STYLE_PARAMETER, REQUEST_PARAM, FRAGMENT);
case PATH_STYLE_PARAMETER -> Stream.of(REQUEST_PARAM, FRAGMENT);
case REQUEST_PARAM, REQUEST_PARAM_CONTINUED -> Stream.of(FRAGMENT);
default -> Stream.<VariableType> empty();
};

return followingTypes.map(it -> it.findIndexWithin(template))
return type.getFollowingTypes().map(it -> it.findIndexWithin(template))
.filter(it -> it != -1)
.findFirst()
.map(it -> template.substring(0, it) + toString() + template.substring(it))
Expand Down

0 comments on commit 5d1f86f

Please sign in to comment.