Class Dictionary

All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>, BasicType<String>

public class Dictionary extends LinkedHashMap<String,Object> implements BasicType<String>
字典对象,扩充了HashMap中的方法
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • Dictionary

      public Dictionary()
      构造
    • Dictionary

      public Dictionary(boolean caseInsensitive)
      构造
      Parameters:
      caseInsensitive - 是否大小写不敏感
    • Dictionary

      public Dictionary(int initialCapacity)
      构造
      Parameters:
      initialCapacity - 初始容量
    • Dictionary

      public Dictionary(Map<String,Object> map)
      构造
      Parameters:
      map - Map
    • Dictionary

      public Dictionary(int initialCapacity, boolean caseInsensitive)
      构造
      Parameters:
      initialCapacity - 初始容量
      caseInsensitive - 是否大小写不敏感
    • Dictionary

      public Dictionary(int initialCapacity, float loadFactor)
      构造
      Parameters:
      initialCapacity - 初始容量
      loadFactor - 容量增长因子,0~1,即达到容量的百分之多少时扩容
    • Dictionary

      public Dictionary(int initialCapacity, float loadFactor, boolean caseInsensitive)
      构造
      Parameters:
      initialCapacity - 初始容量
      loadFactor - 容量增长因子,0~1,即达到容量的百分之多少时扩容
      caseInsensitive - 是否大小写不敏感
  • Method Details

    • create

      public static Dictionary create()
      创建Dict
      Returns:
      Dict
    • parse

      public static <T> Dictionary parse(T bean)
      将PO对象转为Dict
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean对象
      Returns:
      Vo
    • of

      public static Dictionary of(Pair<String,Object>... pairs)
      根据给定的Pair数组创建Dict对象
      Parameters:
      pairs - 键值对
      Returns:
      Dict
    • of

      public static Dictionary of(Object... keysAndValues)
      根据给定的键值对数组创建Dict对象,传入参数必须为key,value,key,value... 奇数参数必须为key,key最后会转换为String类型 奇数参数必须为value,可以为任意类型
       Dict dict = Dict.of(
              "RED", "#FF0000",
              "GREEN", "#00FF00",
              "BLUE", "#0000FF"
       );
       
      Parameters:
      keysAndValues - 键值对列表,必须奇数参数为key,偶数参数为value
      Returns:
      Dict
    • get

      public Object get(Object key)
      Specified by:
      get in interface Map<String,Object>
      Overrides:
      get in class LinkedHashMap<String,Object>
    • put

      public Object put(String key, Object value)
      Specified by:
      put in interface Map<String,Object>
      Overrides:
      put in class HashMap<String,Object>
    • clone

      public Dictionary clone()
      Overrides:
      clone in class HashMap<String,Object>
    • getObject

      public Object getObject(String key)
      Description copied from interface: BasicType
      获取Object属性值
      Specified by:
      getObject in interface BasicType<String>
      Parameters:
      key - 属性名
      Returns:
      属性值
    • getString

      public String getString(String attr)
      Description copied from interface: BasicType
      获取字符串型属性值
      Specified by:
      getString in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getInt

      public Integer getInt(String attr)
      Description copied from interface: BasicType
      获取int型属性值
      Specified by:
      getInt in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getLong

      public Long getLong(String attr)
      Description copied from interface: BasicType
      获取long型属性值
      Specified by:
      getLong in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getFloat

      public Float getFloat(String attr)
      Description copied from interface: BasicType
      获取float型属性值
      Specified by:
      getFloat in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getShort

      public Short getShort(String attr)
      Description copied from interface: BasicType
      获取short型属性值
      Specified by:
      getShort in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getChar

      public Character getChar(String attr)
      Description copied from interface: BasicType
      获取char型属性值
      Specified by:
      getChar in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getDouble

      public Double getDouble(String attr)
      Description copied from interface: BasicType
      获取double型属性值
      Specified by:
      getDouble in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getByte

      public Byte getByte(String attr)
      Description copied from interface: BasicType
      获取byte型属性值
      Specified by:
      getByte in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getBoolean

      public Boolean getBoolean(String attr)
      Description copied from interface: BasicType
      获取boolean型属性值
      Specified by:
      getBoolean in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getBigDecimal

      public BigDecimal getBigDecimal(String attr)
      Description copied from interface: BasicType
      获取BigDecimal型属性值
      Specified by:
      getBigDecimal in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getBigInteger

      public BigInteger getBigInteger(String attr)
      Description copied from interface: BasicType
      获取BigInteger型属性值
      Specified by:
      getBigInteger in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      属性值
    • getEnum

      public <E extends Enum<E>> E getEnum(Class<E> clazz, String key)
      Description copied from interface: BasicType
      获得Enum类型的值
      Specified by:
      getEnum in interface BasicType<String>
      Type Parameters:
      E - 枚举类型
      Parameters:
      clazz - Enum的Class
      key - KEY
      Returns:
      Enum类型的值, 无则返回Null
    • getDate

      public Date getDate(String attr)
      Description copied from interface: BasicType
      获取Date类型值
      Specified by:
      getDate in interface BasicType<String>
      Parameters:
      attr - 属性名
      Returns:
      Date类型属性值
    • putAll

      public void putAll(Map<? extends String,?> m)
      Specified by:
      putAll in interface Map<String,Object>
      Overrides:
      putAll in class HashMap<String,Object>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,Object>
      Overrides:
      containsKey in class HashMap<String,Object>
    • remove

      public Object remove(Object key)
      Specified by:
      remove in interface Map<String,Object>
      Overrides:
      remove in class HashMap<String,Object>
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface Map<String,Object>
      Overrides:
      remove in class HashMap<String,Object>
    • replace

      public boolean replace(String key, Object oldValue, Object newValue)
      Specified by:
      replace in interface Map<String,Object>
      Overrides:
      replace in class HashMap<String,Object>
    • replace

      public Object replace(String key, Object value)
      Specified by:
      replace in interface Map<String,Object>
      Overrides:
      replace in class HashMap<String,Object>
    • getOrDefault

      public Object getOrDefault(Object key, Object defaultValue)
      Specified by:
      getOrDefault in interface Map<String,Object>
      Overrides:
      getOrDefault in class LinkedHashMap<String,Object>
    • computeIfPresent

      public Object computeIfPresent(String key, BiFunction<? super String,? super Object,?> remappingFunction)
      Specified by:
      computeIfPresent in interface Map<String,Object>
      Overrides:
      computeIfPresent in class HashMap<String,Object>
    • compute

      public Object compute(String key, BiFunction<? super String,? super Object,?> remappingFunction)
      Specified by:
      compute in interface Map<String,Object>
      Overrides:
      compute in class HashMap<String,Object>
    • merge

      public Object merge(String key, Object value, BiFunction<? super Object,? super Object,?> remappingFunction)
      Specified by:
      merge in interface Map<String,Object>
      Overrides:
      merge in class HashMap<String,Object>
    • putIfAbsent

      public Object putIfAbsent(String key, Object value)
      Specified by:
      putIfAbsent in interface Map<String,Object>
      Overrides:
      putIfAbsent in class HashMap<String,Object>
    • computeIfAbsent

      public Object computeIfAbsent(String key, Function<? super String,?> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<String,Object>
      Overrides:
      computeIfAbsent in class HashMap<String,Object>
    • getBytes

      public byte[] getBytes(String attr)
      Parameters:
      attr - 字段名
      Returns:
      字段值
    • getTime

      public Time getTime(String attr)
      Parameters:
      attr - 字段名
      Returns:
      字段值
    • getTimestamp

      public Timestamp getTimestamp(String attr)
      Parameters:
      attr - 字段名
      Returns:
      字段值
    • getNumber

      public Number getNumber(String attr)
      Parameters:
      attr - 字段名
      Returns:
      字段值
    • getByPath

      public <T> T getByPath(String expression)
      通过表达式获取JSON中嵌套的对象
      1. .表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值
      2. []表达式,可以获取集合等对象中对应index的值

      表达式栗子:

       persion
       persion.name
       persons[3]
       person.friends[5].name
       
      Type Parameters:
      T - 泛型对象
      Parameters:
      expression - 表达式
      Returns:
      对象
      See Also:
    • getByPath

      public <T> T getByPath(String expression, Class<T> resultType)
      通过表达式获取JSON中嵌套的对象
      1. .表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值
      2. []表达式,可以获取集合等对象中对应index的值

      表达式栗子:

       persion
       persion.name
       persons[3]
       person.friends[5].name
       

      获取表达式对应值后转换为对应类型的值

      Type Parameters:
      T - 返回值类型
      Parameters:
      expression - 表达式
      resultType - 返回值类型
      Returns:
      对象
      See Also:
    • toBean

      public <T> T toBean(T bean)
      转换为Bean对象
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean
      Returns:
      Bean
    • toBean

      public <T> T toBean(T bean, boolean isToCamelCase)
      转换为Bean对象
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean
      isToCamelCase - 是否转换为驼峰模式
      Returns:
      Bean
    • toBeanIgnoreCase

      public <T> T toBeanIgnoreCase(T bean)
      转换为Bean对象
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean
      Returns:
      Bean
    • toBeanWithCamelCase

      public <T> T toBeanWithCamelCase(T bean)
      转换为Bean对象,并使用驼峰法模式转换
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean
      Returns:
      Bean
    • toBeanIgnoreCase

      public <T> T toBeanIgnoreCase(Class<T> clazz)
      填充Value Object对象,忽略大小写
      Type Parameters:
      T - Bean类型
      Parameters:
      clazz - Value Object(或者POJO)的类
      Returns:
      vo
    • parseBean

      public <T> Dictionary parseBean(T bean)
      将值对象转换为Dict 类名会被当作表名,小写第一个字母
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - 值对象
      Returns:
      自己
    • parseBean

      public <T> Dictionary parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue)
      将值对象转换为Dict 类名会被当作表名,小写第一个字母
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - 值对象
      isToUnderlineCase - 是否转换为下划线模式
      ignoreNullValue - 是否忽略值为空的字段
      Returns:
      自己
    • removeEqual

      public <T extends Dictionary> void removeEqual(T dict, String... withoutNames)
      与给定实体对比并去除相同的部分 此方法用于在更新操作时避免所有字段被更新,跳过不需要更新的字段 version from 2.0.0
      Type Parameters:
      T - 字典对象类型
      Parameters:
      dict - 字典对象
      withoutNames - 不需要去除的字段名
    • filter

      public Dictionary filter(String... keys)
      过滤Map保留指定键值对,如果键不存在跳过
      Parameters:
      keys - 键列表
      Returns:
      Dict 结果
    • getBean

      public <T> T getBean(String attr)
      获得特定类型值
      Type Parameters:
      T - 值类型
      Parameters:
      attr - 字段名
      Returns:
      字段值
    • setIgnoreNull

      public Dictionary setIgnoreNull(String attr, Object value)
      设置列,当键或值为null时忽略
      Parameters:
      attr - 属性
      value - 值
      Returns:
      本身
    • set

      public Dictionary set(String attr, Object value)
      设置列
      Parameters:
      attr - 属性
      value - 值
      Returns:
      本身
    • get

      public <T> T get(String attr, T defaultValue)
      获得特定类型值
      Type Parameters:
      T - 值类型
      Parameters:
      attr - 字段名
      defaultValue - 默认值
      Returns:
      字段值
    • setFields

      public Dictionary setFields(Func0<?>... fields)
      通过lambda批量设置值 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
           User user = GenericBuilder.of(User::new).with(User::setUsername, "bus").build();
           Dict.create().setFields(user::getNickname, user::getUsername);
       
      Parameters:
      fields - lambda,不能为空
      Returns:
      this