Skip to content

Commit 36e923f

Browse files
authored
Replace instanceof check and cast with pattern matching (#3979)
Signed-off-by: Mengqi Xu <[email protected]>
1 parent 1f1e9c0 commit 36e923f

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public String render() {
111111
// Process internal variables to handle Resources before rendering
112112
Map<String, Object> processedVariables = new HashMap<>();
113113
for (Entry<String, Object> entry : this.variables.entrySet()) {
114-
if (entry.getValue() instanceof Resource) {
115-
processedVariables.put(entry.getKey(), renderResource((Resource) entry.getValue()));
114+
if (entry.getValue() instanceof Resource resource) {
115+
processedVariables.put(entry.getKey(), renderResource(resource));
116116
}
117117
else {
118118
processedVariables.put(entry.getKey(), entry.getValue());
@@ -126,8 +126,8 @@ public String render(Map<String, Object> additionalVariables) {
126126
Map<String, Object> combinedVariables = new HashMap<>(this.variables);
127127

128128
for (Entry<String, Object> entry : additionalVariables.entrySet()) {
129-
if (entry.getValue() instanceof Resource) {
130-
combinedVariables.put(entry.getKey(), renderResource((Resource) entry.getValue()));
129+
if (entry.getValue() instanceof Resource resource) {
130+
combinedVariables.put(entry.getKey(), renderResource(resource));
131131
}
132132
else {
133133
combinedVariables.put(entry.getKey(), entry.getValue());

spring-ai-model/src/main/java/org/springframework/ai/util/json/JsonParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ private static boolean isValidJson(String input) {
116116
* Converts a Java object to a JSON string if it's not already a valid JSON string.
117117
*/
118118
public static String toJson(@Nullable Object object) {
119-
if (object instanceof String && isValidJson((String) object)) {
120-
return (String) object;
119+
if (object instanceof String str && isValidJson(str)) {
120+
return str;
121121
}
122122
try {
123123
return OBJECT_MAPPER.writeValueAsString(object);

0 commit comments

Comments
 (0)