diff --git a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/collection/MapModifier.java b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/collection/MapModifier.java index 7266b71..5cf5e97 100644 --- a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/collection/MapModifier.java +++ b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/collection/MapModifier.java @@ -87,45 +87,65 @@ public class MapModifier { } /** - * 添加一个键值对 + * 添加一个键值对。 + * + *

+ * 注意:键值对是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 * * @param key 要添加的 {@code key} * @param value 要添加的 {@code value} * @return MapModifier */ - public MapModifier put(K key, V value) { + public MapModifier put(@Nullable K key, @Nullable V value) { return addOperationInternal(map -> map.put(key, value)); } /** - * 添加一个键值对,如果 key 已经存在,则不添加 + * 添加一个键值对,如果 key 已经存在,则不添加。 + * + *

+ * 注意:键值对是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 * * @param key 要添加的 {@code key} * @param value 要添加的 {@code value} * @return MapModifier */ - public MapModifier putIfAbsent(K key, V value) { + public MapModifier putIfAbsent(@Nullable K key, @Nullable V value) { return addOperationInternal(map -> map.putIfAbsent(key, value)); } /** - * 添加多个键值对 + * 添加多个键值对。 + * + *

+ * 注意:键值对是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 + * + * @param otherMap 要添加的键值对集合。 + * 如果为 {@code null},则什么都不做。 * - * @param otherMap 要添加的键值对集合 * @return MapModifier */ - public MapModifier putAll(Map otherMap) { + public MapModifier putAll(@Nullable Map otherMap) { + if (otherMap == null || otherMap.isEmpty()) { + return this; + } return addOperationInternal(map -> map.putAll(otherMap)); } /** - * 添加多个键值对 + * 添加多个键值对。 + * + *

+ * 注意:键值对是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 * * @param entries 要添加的键值对集合 * @return MapModifier */ @SafeVarargs public final MapModifier putAll(Map.Entry... entries) { + if (entries.length == 0) { + return this; + } return addOperationInternal(map -> { for (Map.Entry entry : entries) { map.put(entry.getKey(), entry.getValue()); @@ -134,8 +154,13 @@ public class MapModifier { } /** - * 当 {@code key} 不存在时,计算对应的值,并添加到 {@code map} 中 - * 调用 {@link Map#computeIfAbsent(Object, Function)} + * 当 {@code key} 不存在时,计算对应的值,并添加到 {@code map} 中。 + * + *

+ * 调用 {@link Map#computeIfAbsent(Object, Function)}。 + * + *

+ * 注意:键值对是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 * * @param key 要添加的 {@code key} * @param mappingFunction 计算 {@code key} 对应的值 @@ -147,8 +172,13 @@ public class MapModifier { } /** - * 当 {@code key} 存在时,计算对应的值,并添加到 {@code map} 中 - * 调用 {@link Map#computeIfPresent(Object, BiFunction)} + * 当 {@code key} 存在时,计算对应的值,并添加到 {@code map} 中。 + * + *

+ * 调用 {@link Map#computeIfPresent(Object, BiFunction)}。 + * + *

+ * 注意:键值对是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 * * @param key 要添加的 {@code key} * @param remappingFunction 计算 {@code key} 对应的值 @@ -160,7 +190,10 @@ public class MapModifier { } /** - * 删除 {@code key} + * 删除 {@code key}。 + * + *

+ * 注意:key 是否允许为 {@code null},由最终作用的 {@link Map} 类型决定。 * * @param key 要删除的 {@code key} * @return MapModifier