Forráskód Böngészése

feat(utils): 添加 JHashMap 和 JMap 接口

- 新增 JHashMap 类,实现 JMap接口并扩展 HashMap 功能- 创建 JMap 接口,定义通用的 Map 操作方法- 添加多种数据类型获取方法,简化类型转换
- 实现条件性设置和批量设置方法,提高灵活性和效率
hechunping 1 hónapja
szülő
commit
00b13b757a

+ 117 - 0
src/main/java/com/xynet/marketing/utils/collections/map/JHashMap.java

@@ -0,0 +1,117 @@
+package com.xynet.marketing.utils.collections.map;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.util.*;
+
+/**
+ * HashMap
+ */
+public class JHashMap<K, V> extends HashMap<K, V> implements JMap<K, V>, Serializable {
+
+    public JHashMap() {
+
+    }
+
+    public JHashMap(int initialCapacity) {
+        super(initialCapacity);
+    }
+
+    public JHashMap(Map<K, V> map) {
+        super(map);
+    }
+
+    @Override
+    public JMap<K, V> set(K key, V value) {
+        this.put(key, value);
+        return this;
+    }
+
+    @Override
+    public JMap<K, V> set(boolean condition, K key, V value) {
+        if (condition) {
+            this.put(key, value);
+        }
+        return this;
+    }
+
+
+    @Override
+    public V setIfAbsent(K key, V value) {
+        return this.putIfAbsent(key, value);
+    }
+
+    @Override
+    public JMap<K, V> setAll(Map<K, V> map) {
+        this.putAll(map);
+        return this;
+    }
+
+    @Override
+    public JMap<K, V> setAll(boolean condition, Map<K, V> map) {
+        if (condition) {
+            this.putAll(map);
+        }
+        return this;
+    }
+
+    @Override
+    public V del(K key) {
+        return this.remove(key);
+    }
+
+    @Override
+    public String getString(K key) {
+        return this.get(key) == null ? null : this.get(key).toString();
+    }
+
+
+    @Override
+    public Integer getInt(K key) {
+        return this.get(key) == null ? null : Integer.parseInt(this.get(key).toString());
+    }
+
+
+    @Override
+    public Long getLong(K key) {
+        return this.get(key) == null ? null : Long.parseLong(this.get(key).toString());
+    }
+
+    @Override
+    public Double getDouble(K key) {
+        return this.get(key) == null ? null : Double.parseDouble(this.get(key).toString());
+    }
+
+
+    @Override
+    public BigDecimal getBigDecimal(K key) {
+        return this.get(key) == null ? null : new BigDecimal(this.get(key).toString());
+    }
+
+
+    @Override
+    public Boolean getBoolean(K key) {
+        return this.get(key) == null ? null : Boolean.parseBoolean(this.get(key).toString());
+    }
+
+
+    @Override
+    public LocalDateTime getLocalDateTime(K key) {
+        return this.get(key) == null ? null : (LocalDateTime) this.get(key);
+    }
+
+
+    @Override
+    public Timestamp getTimestamp(K key) {
+        return this.get(key) == null ? null : (Timestamp) this.get(key);
+    }
+
+
+    @Override
+    public V get(K key, Class<V> t) {
+        V v = this.get(key);
+        return v == null ? null : v;
+    }
+}

+ 76 - 0
src/main/java/com/xynet/marketing/utils/collections/map/JMap.java

@@ -0,0 +1,76 @@
+package com.xynet.marketing.utils.collections.map;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.util.Map;
+
+/**
+ * map接口
+ * function并不区分key的数据类型,假设泛型K为Integer,实际入参为String,而function同样可以获取到值
+ */
+public interface JMap<K, V> extends Map<K, V>, Serializable {
+
+    /**
+     * 覆盖写入
+     */
+    JMap<K, V> set(K key, V value);
+
+    /**
+     * 覆盖写入
+     */
+    JMap<K, V> set(boolean condition, K key, V value);
+
+
+
+
+    /**
+     * 不覆盖写入,key存在直接返回旧value,不存在则新增且返回null
+     */
+    V setIfAbsent(K key, V value);
+
+
+    /**
+     * 写入全部
+     *
+     * @param map
+     * @return
+     */
+    JMap<K, V> setAll(Map<K, V> map);
+
+    /**
+     * 写入全部
+     *
+     * @param map
+     * @return
+     */
+    JMap<K, V> setAll(boolean condition, Map<K, V> map);
+
+    /**
+     * 删除并返回value
+     */
+    V del(K key);
+
+
+
+    String getString(K key);
+
+    Integer getInt(K key);
+
+
+    Long getLong(K key);
+
+    Double getDouble(K key);
+
+    BigDecimal getBigDecimal(K key);
+
+    Boolean getBoolean(K key);
+
+    LocalDateTime getLocalDateTime(K key);
+
+    Timestamp getTimestamp(K key);
+
+    V get(K key, Class<V> t);
+
+}