Class Configuration


  • public class Configuration
    extends Object
    提供多级JSON配置信息无损存储
    Author:
    lb
    Date:
    2024年1月26日
    • Method Detail

      • newDefault

        public static Configuration newDefault()
        初始化空白的Configuration
      • from

        public static Configuration from​(File file)
        从包括json的File对象加载Configuration
      • get

        public Object get​(String path)
        根据用户提供的json path,寻址具体的对象。


        NOTE: 目前仅支持Map以及List下标寻址, 例如:


        对于如下JSON

        {"a": {"b": {"c": [0,1,2,3]}}}

        config.get("") 返回整个Map
        config.get("a") 返回a下属整个Map
        config.get("a.b.c") 返回c对应的数组List
        config.get("a.b.c[0]") 返回数字0

        Returns:
        Java表示的JSON对象,如果path不存在或者对象不存在,均返回null。
      • getConfiguration

        public Configuration getConfiguration​(String path)
        用户指定部分path,获取Configuration的子集


        如果path获取的路径或者对象不存在,返回null

      • getConfigurationNotNull

        public Configuration getConfigurationNotNull​(String path)
        用户指定部分path,获取Configuration的子集


        如果path获取的路径或者对象不存在,返回null

      • getString

        public String getString​(String path)
        根据用户提供的json path,寻址String对象
        Returns:
        String对象,如果path不存在或者String不存在,返回null
      • getString

        public String getString​(String path,
                                String defaultValue)
        根据用户提供的json path,寻址String对象,如果对象不存在,返回默认字符串
        Returns:
        String对象,如果path不存在或者String不存在,返回默认字符串
      • getChar

        public Character getChar​(String path)
        根据用户提供的json path,寻址Character对象
        Returns:
        Character对象,如果path不存在或者Character不存在,返回null
      • getChar

        public Character getChar​(String path,
                                 char defaultValue)
        根据用户提供的json path,寻址Boolean对象,如果对象不存在,返回默认Character对象
        Returns:
        Character对象,如果path不存在或者Character不存在,返回默认Character对象
      • getBool

        public Boolean getBool​(String path)
        根据用户提供的json path,寻址Boolean对象
        Returns:
        Boolean对象,如果path值非true,false ,将报错.特别注意:当 path 不存在时,会返回:null.
      • getBool

        public Boolean getBool​(String path,
                               boolean defaultValue)
        根据用户提供的json path,寻址Boolean对象,如果对象不存在,返回默认Boolean对象
        Returns:
        Boolean对象,如果path不存在或者Boolean不存在,返回默认Boolean对象
      • getInt

        public Integer getInt​(String path)
        根据用户提供的json path,寻址Integer对象
        Returns:
        Integer对象,如果path不存在或者Integer不存在,返回null
      • getInt

        public Integer getInt​(String path,
                              int defaultValue)
        根据用户提供的json path,寻址Integer对象,如果对象不存在,返回默认Integer对象
        Returns:
        Integer对象,如果path不存在或者Integer不存在,返回默认Integer对象
      • getLong

        public Long getLong​(String path)
        根据用户提供的json path,寻址Long对象
        Returns:
        Long对象,如果path不存在或者Long不存在,返回null
      • getLong

        public Long getLong​(String path,
                            long defaultValue)
        根据用户提供的json path,寻址Long对象,如果对象不存在,返回默认Long对象
        Returns:
        Long对象,如果path不存在或者Integer不存在,返回默认Long对象
      • getDouble

        public Double getDouble​(String path)
        根据用户提供的json path,寻址Double对象
        Returns:
        Double对象,如果path不存在或者Double不存在,返回null
      • getDouble

        public Double getDouble​(String path,
                                double defaultValue)
        根据用户提供的json path,寻址Double对象,如果对象不存在,返回默认Double对象
        Returns:
        Double对象,如果path不存在或者Double不存在,返回默认Double对象
      • getList

        public List<Object> getList​(String path)
        根据用户提供的json path,寻址List对象,如果对象不存在,返回null
      • getList

        public <T> List<T> getList​(String path,
                                   Class<T> t)
        根据用户提供的json path,寻址List对象,如果对象不存在,返回null
      • getList

        public List<Object> getList​(String path,
                                    List<Object> defaultList)
        根据用户提供的json path,寻址List对象,如果对象不存在,返回默认List
      • getList

        public <T> List<T> getList​(String path,
                                   List<T> defaultList,
                                   Class<T> t)
        根据用户提供的json path,寻址List对象,如果对象不存在,返回默认List
      • getListConfiguration

        public List<Configuration> getListConfiguration​(String path)
        根据用户提供的json path,寻址包含Configuration的List,如果对象不存在,返回默认null
      • getMap

        public Map<String,​Object> getMap​(String path)
        根据用户提供的json path,寻址Map对象,如果对象不存在,返回null
      • getMap

        public <T> Map<String,​T> getMap​(String path,
                                              Class<T> t)
        根据用户提供的json path,寻址Map对象,如果对象不存在,返回null;
      • getMap

        public Map<String,​Object> getMap​(String path,
                                               Map<String,​Object> defaultMap)
        根据用户提供的json path,寻址Map对象,如果对象不存在,返回默认map
      • getMap

        public <T> Map<String,​T> getMap​(String path,
                                              Map<String,​T> defaultMap,
                                              Class<T> t)
        根据用户提供的json path,寻址Map对象,如果对象不存在,返回默认map
      • getMapConfiguration

        public Map<String,​Configuration> getMapConfiguration​(String path)
        根据用户提供的json path,寻址包含Configuration的Map,如果对象不存在,返回默认null
      • get

        public <T> T get​(String path,
                         Class<T> clazz)
        根据用户提供的json path,寻址具体的对象,并转为用户提供的类型


        NOTE: 目前仅支持Map以及List下标寻址, 例如:


        对于如下JSON

        {"a": {"b": {"c": [0,1,2,3]}}}

        config.get("") 返回整个Map
        config.get("a") 返回a下属整个Map
        config.get("a.b.c") 返回c对应的数组List
        config.get("a.b.c[0]") 返回数字0

        Returns:
        Java表示的JSON对象,如果转型失败,将抛出异常
      • beautify

        public String beautify()
        格式化Configuration输出
      • set

        public Object set​(String path,
                          Object object)
        根据用户提供的json path,插入指定对象,并返回之前存在的对象(如果存在)


        目前仅支持.以及数组下标寻址, 例如:


        config.set("a.b.c[3]", object);


        对于插入对象,Configuration不做任何限制,但是请务必保证该对象是简单对象(包括Map、List),不要使用自定义对象,否则后续对于JSON序列化等情况会出现未定义行为。
        Parameters:
        path - JSON path对象
        object - 需要插入的对象
        Returns:
        Java表示的JSON对象
        • getKeys

          public Set<String> getKeys()
          获取Configuration下所有叶子节点的key


          对于

          {"a": {"b": {"c": [0,1,2,3]}}, "x": "y"}

          下属的key包括: a.b.c[0],a.b.c[1],a.b.c[2],a.b.c[3],x

        • remove

          public Object remove​(String path)
          删除path对应的值,如果path不存在,将抛出异常。
        • merge

          public Configuration merge​(Configuration another,
                                     boolean updateWhenConflict)
          合并其他Configuration,并修改两者冲突的KV配置
          Parameters:
          another - 合并加入的第三方Configuration
          updateWhenConflict - 当合并双方出现KV冲突时候,选择更新当前KV,或者忽略该KV
          Returns:
          返回合并后对象
        • toJSON

          public String toJSON()
          将Configuration作为JSON输出
        • clone

          public Configuration clone()
          拷贝当前Configuration,注意,这里使用了深拷贝,避免冲突
          Overrides:
          clone in class Object
        • addSecretKeyPath

          public void addSecretKeyPath​(String path)
          按照configuration要求格式的path 比如: a.b.c a.b[2].c
          Parameters:
          path -
        • addSecretKeyPath

          public void addSecretKeyPath​(Set<String> pathSet)
        • setSecretKeyPathSet

          public void setSecretKeyPathSet​(Set<String> keyPathSet)
        • isSecretPath

          public boolean isSecretPath​(String path)
        • getInternal

          public Object getInternal()
        • getSecretKeyPathSet

          public Set<String> getSecretKeyPathSet()