diff --git a/spring-core/src/main/java/org/springframework/core/codec/Hints.java b/spring-core/src/main/java/org/springframework/core/codec/Hints.java index 48991486e4..2d91bb68ba 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/Hints.java +++ b/spring-core/src/main/java/org/springframework/core/codec/Hints.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.lang.Nullable; import org.springframework.util.CollectionUtils; +import org.springframework.util.ObjectUtils; /** * Constants and convenience methods for working with hints. @@ -111,22 +112,22 @@ public abstract class Hints { * @param hints2 2nd map of hints * @return a single map with hints from both */ - public static Map merge(Map hints1, Map hints2) { - if (hints1.isEmpty() && hints2.isEmpty()) { + public static Map merge( + @Nullable Map hints1, @Nullable Map hints2) { + + if (ObjectUtils.isEmpty(hints1) && ObjectUtils.isEmpty(hints2)) { return Collections.emptyMap(); } - else if (hints2.isEmpty()) { - return hints1; + else if (ObjectUtils.isEmpty(hints2)) { + return (hints1 != null ? hints1 : Collections.emptyMap()); } - else if (hints1.isEmpty()) { + else if (ObjectUtils.isEmpty(hints1)) { return hints2; } - else { - Map result = CollectionUtils.newHashMap(hints1.size() + hints2.size()); - result.putAll(hints1); - result.putAll(hints2); - return result; - } + Map result = CollectionUtils.newHashMap(hints1.size() + hints2.size()); + result.putAll(hints1); + result.putAll(hints2); + return result; } /** @@ -138,16 +139,14 @@ public abstract class Hints { * @param hintValue the hint value to merge * @return a single map with all hints */ - public static Map merge(Map hints, String hintName, Object hintValue) { - if (hints.isEmpty()) { + public static Map merge(@Nullable Map hints, String hintName, Object hintValue) { + if (ObjectUtils.isEmpty(hints)) { return Collections.singletonMap(hintName, hintValue); } - else { - Map result = CollectionUtils.newHashMap(hints.size() + 1); - result.putAll(hints); - result.put(hintName, hintValue); - return result; - } + Map result = CollectionUtils.newHashMap(hints.size() + 1); + result.putAll(hints); + result.put(hintName, hintValue); + return result; } /**