From 5d1f86f2858709705b8910c8142e5515155d72b4 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 13 Nov 2023 15:58:12 +0100 Subject: [PATCH] #2041 - Polishing. --- .../springframework/hateoas/TemplateVariable.java | 11 +++++++++++ .../org/springframework/hateoas/UriTemplate.java | 12 +----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/TemplateVariable.java b/src/main/java/org/springframework/hateoas/TemplateVariable.java index 9c9eb3f9c..651481218 100644 --- a/src/main/java/org/springframework/hateoas/TemplateVariable.java +++ b/src/main/java/org/springframework/hateoas/TemplateVariable.java @@ -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; @@ -558,6 +559,16 @@ int findIndexWithin(String template) { return template.indexOf(key); } + Stream 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. empty(); + }; + } + /** * Returns the {@link VariableType} for the given variable key. * diff --git a/src/main/java/org/springframework/hateoas/UriTemplate.java b/src/main/java/org/springframework/hateoas/UriTemplate.java index e1c1f7599..e7766fc45 100644 --- a/src/main/java/org/springframework/hateoas/UriTemplate.java +++ b/src/main/java/org/springframework/hateoas/UriTemplate.java @@ -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; @@ -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; @@ -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. 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))