Package risesoft.data.transfer.core.util
Class Configuration
- java.lang.Object
-
- risesoft.data.transfer.core.util.Configuration
-
public class Configuration extends Object
提供多级JSON配置信息无损存储- Author:
- lb
- Date:
- 2024年1月26日
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSecretKeyPath(String path)按照configuration要求格式的path 比如: a.b.c a.b[2].cvoidaddSecretKeyPath(Set<String> pathSet)Stringbeautify()格式化Configuration输出Configurationclone()拷贝当前Configuration,注意,这里使用了深拷贝,避免冲突static Configurationfrom(File file)从包括json的File对象加载Configurationstatic Configurationfrom(InputStream is)从包括json的InputStream对象加载Configurationstatic Configurationfrom(String json)从JSON字符串加载Configurationstatic Configurationfrom(List<Object> object)从List对象加载Configurationstatic Configurationfrom(Map<String,Object> object)从Map对象加载ConfigurationObjectget(String path)根据用户提供的json path,寻址具体的对象。<T> Tget(String path, Class<T> clazz)根据用户提供的json path,寻址具体的对象,并转为用户提供的类型BooleangetBool(String path)根据用户提供的json path,寻址Boolean对象BooleangetBool(String path, boolean defaultValue)根据用户提供的json path,寻址Boolean对象,如果对象不存在,返回默认Boolean对象CharactergetChar(String path)根据用户提供的json path,寻址Character对象CharactergetChar(String path, char defaultValue)根据用户提供的json path,寻址Boolean对象,如果对象不存在,返回默认Character对象ConfigurationgetConfiguration(String path)用户指定部分path,获取Configuration的子集ConfigurationgetConfigurationNotNull(String path)用户指定部分path,获取Configuration的子集DoublegetDouble(String path)根据用户提供的json path,寻址Double对象DoublegetDouble(String path, double defaultValue)根据用户提供的json path,寻址Double对象,如果对象不存在,返回默认Double对象IntegergetInt(String path)根据用户提供的json path,寻址Integer对象IntegergetInt(String path, int defaultValue)根据用户提供的json path,寻址Integer对象,如果对象不存在,返回默认Integer对象ObjectgetInternal()Set<String>getKeys()获取Configuration下所有叶子节点的keyList<Object>getList(String path)根据用户提供的json path,寻址List对象,如果对象不存在,返回null<T> List<T>getList(String path, Class<T> t)根据用户提供的json path,寻址List对象,如果对象不存在,返回nullList<Object>getList(String path, List<Object> defaultList)根据用户提供的json path,寻址List对象,如果对象不存在,返回默认List<T> List<T>getList(String path, List<T> defaultList, Class<T> t)根据用户提供的json path,寻址List对象,如果对象不存在,返回默认ListList<Configuration>getListConfiguration(String path)根据用户提供的json path,寻址包含Configuration的List,如果对象不存在,返回默认nullLonggetLong(String path)根据用户提供的json path,寻址Long对象LonggetLong(String path, long defaultValue)根据用户提供的json path,寻址Long对象,如果对象不存在,返回默认Long对象Map<String,Object>getMap(String path)根据用户提供的json path,寻址Map对象,如果对象不存在,返回null<T> Map<String,T>getMap(String path, Class<T> t)根据用户提供的json path,寻址Map对象,如果对象不存在,返回null;Map<String,Object>getMap(String path, Map<String,Object> defaultMap)根据用户提供的json path,寻址Map对象,如果对象不存在,返回默认map<T> Map<String,T>getMap(String path, Map<String,T> defaultMap, Class<T> t)根据用户提供的json path,寻址Map对象,如果对象不存在,返回默认mapMap<String,Configuration>getMapConfiguration(String path)根据用户提供的json path,寻址包含Configuration的Map,如果对象不存在,返回默认nullBooleangetNecessaryBool(String key, ErrorCode errorCode)StringgetNecessaryValue(String key, ErrorCode errorCode)Set<String>getSecretKeyPathSet()StringgetString(String path)根据用户提供的json path,寻址String对象StringgetString(String path, String defaultValue)根据用户提供的json path,寻址String对象,如果对象不存在,返回默认字符串StringgetUnnecessaryValue(String key, String defaultValue, ErrorCode errorCode)booleanisSecretPath(String path)Configurationmerge(Configuration another, boolean updateWhenConflict)合并其他Configuration,并修改两者冲突的KV配置static ConfigurationnewDefault()初始化空白的ConfigurationObjectremove(String path)删除path对应的值,如果path不存在,将抛出异常。Objectset(String path, Object object)根据用户提供的json path,插入指定对象,并返回之前存在的对象(如果存在)voidsetSecretKeyPathSet(Set<String> keyPathSet)StringtoJSON()将Configuration作为JSON输出StringtoString()
-
-
-
Method Detail
-
newDefault
public static Configuration newDefault()
初始化空白的Configuration
-
from
public static Configuration from(String json)
从JSON字符串加载Configuration
-
from
public static Configuration from(File file)
从包括json的File对象加载Configuration
-
from
public static Configuration from(InputStream is)
从包括json的InputStream对象加载Configuration
-
from
public static Configuration from(Map<String,Object> object)
从Map对象加载Configuration
-
from
public static Configuration from(List<Object> object)
从List对象加载Configuration
-
getUnnecessaryValue
public String getUnnecessaryValue(String key, String defaultValue, ErrorCode errorCode)
-
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 <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 <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 - 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
-
merge
public Configuration merge(Configuration another, boolean updateWhenConflict)
合并其他Configuration,并修改两者冲突的KV配置- Parameters:
another- 合并加入的第三方ConfigurationupdateWhenConflict- 当合并双方出现KV冲突时候,选择更新当前KV,或者忽略该KV- Returns:
- 返回合并后对象
-
toJSON
public String toJSON()
将Configuration作为JSON输出
-
clone
public Configuration clone()
拷贝当前Configuration,注意,这里使用了深拷贝,避免冲突
-
addSecretKeyPath
public void addSecretKeyPath(String path)
按照configuration要求格式的path 比如: a.b.c a.b[2].c- Parameters:
path-
-
isSecretPath
public boolean isSecretPath(String path)
-
getInternal
public Object getInternal()
-
-