From 52218dc2141b25010faaaac4c00dce149421d169 Mon Sep 17 00:00:00 2001 From: Cjt Date: Tue, 28 Dec 2021 23:55:12 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 05944199b87e90303c842b9bb2cdf5040bd3ecd4 Former-commit-id: 1a752b8053529f494781c4514466019e43894b97 --- .../src/main/java/dev/base/DevIntent.java | 209 ++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 lib/DevAssist/src/main/java/dev/base/DevIntent.java diff --git a/lib/DevAssist/src/main/java/dev/base/DevIntent.java b/lib/DevAssist/src/main/java/dev/base/DevIntent.java new file mode 100644 index 0000000000..ce35fc2f28 --- /dev/null +++ b/lib/DevAssist/src/main/java/dev/base/DevIntent.java @@ -0,0 +1,209 @@ +package dev.base; + +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import dev.utils.common.StringUtils; + +/** + * detail: DevFinal.STR Intent 传参读写辅助类 + * @author Ttt + *
+ *     统一存储为 String 需要转换其他类型则可通过
+ *     {@link dev.utils.common.ConvertUtils} 进行转换
+ *     或者自行通过 JSON 映射实体类等
+ *     

+ * 可存储 key、value 为 null 数据 ( 提供方法清除 null 数据 ) + *

+ * 根据 DevFinal.STR 自动生成通用方法 + *
+ */ +public final class DevIntent { + + // 存储数据 Map + private final LinkedHashMap mDataMaps = new LinkedHashMap<>(); + + // ========== + // = 内部方法 = + // ========== + + // ============= + // = 对外公开方法 = + // ============= + + // ======= + // = 通用 = + // ======= + + /** + * 获取存储数据 Map + * @return 存储数据 Map + */ + public Map getDataMaps() { + return mDataMaps; + } + + /** + * 是否存在 Key + * @param key 保存的 key + * @return {@code true} yes, {@code false} no + */ + public boolean containsKey(final String key) { + return mDataMaps.containsKey(key); + } + + /** + * 是否存在 Value + * @param value 保存的 value + * @return {@code true} yes, {@code false} no + */ + public boolean containsValue(final String value) { + return mDataMaps.containsValue(value); + } + + /** + * 保存数据 + * @param key 保存的 key + * @param value 保存的 value + * @return {@link DevIntent} + */ + public DevIntent put( + final String key, + final String value + ) { + mDataMaps.put(key, value); + return this; + } + + /** + * 保存集合数据 + * @param map {@link Map} + * @return {@link DevIntent} + */ + public DevIntent putAll(final Map map) { + if (map != null) mDataMaps.putAll(map); + return this; + } + + /** + * 移除数据 + * @param key 保存的 key + * @return {@link DevIntent} + */ + public DevIntent remove(final String key) { + mDataMaps.remove(key); + return this; + } + + /** + * 移除集合数据 + * @param keys 保存的 key 集合 + * @return {@link DevIntent} + */ + public DevIntent removeAll(final List keys) { + if (keys != null) { + for (String key : keys) { + mDataMaps.remove(key); + } + } + return this; + } + + /** + * 获取对应 Key 保存的 Value + * @param key 保存的 key + * @return 保存的 value + */ + public String get(final String key) { + return mDataMaps.get(key); + } + + /** + * 清空数据 + * @return {@link DevIntent} + */ + public DevIntent clear() { + mDataMaps.clear(); + return this; + } + + // = + + /** + * 清除 null 数据 + *
+     *     key、value 只要其中一个为 null 就清除
+     * 
+ * @return {@link DevIntent} + */ + public DevIntent clearNull() { + return clearNullKey().clearNullValue(); + } + + /** + * 清除 null Key 数据 + * @return {@link DevIntent} + */ + public DevIntent clearNullKey() { + return remove(null); + } + + /** + * 清除 null Value 数据 + *
+     *     value 只要为 null 就清除
+     * 
+ * @return {@link DevIntent} + */ + public DevIntent clearNullValue() { + Iterator> iterator = mDataMaps.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if (entry.getValue() == null) { + iterator.remove(); + } + } + return this; + } + + // = + + /** + * 清除 empty 数据 + *
+     *     key、value 只要其中一个为 empty ( null、"" ) 就清除
+     * 
+ * @return {@link DevIntent} + */ + public DevIntent clearEmpty() { + return clearEmptyKey().clearEmptyValue(); + } + + /** + * 清除 empty Key 数据 + * @return {@link DevIntent} + */ + public DevIntent clearEmptyKey() { + return remove(null).remove(""); + } + + /** + * 清除 empty Value 数据 + *
+     *     value 只要为 empty ( null、"" ) 就清除
+     * 
+ * @return {@link DevIntent} + */ + public DevIntent clearEmptyValue() { + Iterator> iterator = mDataMaps.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if (StringUtils.isEmpty(entry.getValue())) { + iterator.remove(); + } + } + return this; + } +} \ No newline at end of file