java.lang.Object
org.miaixz.bus.setting.magic.AbstractSetting
org.miaixz.bus.setting.Setting
- All Implemented Interfaces:
Serializable,Map<String,,String> org.miaixz.bus.core.lang.getter.GroupedTypeGetter<CharSequence,,CharSequence> org.miaixz.bus.core.lang.getter.TypeGetter<CharSequence>
A utility class for handling
.setting files, which are an enhanced version of Java's .properties
files with backward compatibility.
Features:
- Supports variable substitution using the
${variable_name}syntax. - Supports grouping of properties under section headers (e.g.,
[group_name]). Keys under a group are accessed asgroup.key. - Treats lines starting with '#' as comments.
store methods do not preserve comments.- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CharsetThe character set for this settings instance.static final CharsetThe default character set (UTF-8).static final StringThe default file extension for settings files.protected booleanWhether variable substitution is enabled.protected org.miaixz.bus.core.io.resource.ResourceThe resource representing the settings file.Fields inherited from class org.miaixz.bus.setting.magic.AbstractSetting
DEFAULT_DELIMITER, DEFAULT_GROUP -
Constructor Summary
ConstructorsConstructorDescriptionSetting()Constructs a new, emptySettinginstance.Constructs aSettingfrom aFile.Constructs aSettingby loading a file from a relative or absolute path.Constructs aSettingby loading a file.Constructs aSettingby loading a file from the classpath.Constructs aSettingfrom aResource.Constructs aSettingfrom aResourceusing a custom loader. -
Method Summary
Modifier and TypeMethodDescriptionaddSetting(Setting setting) Merges all groups and settings from anotherSettinginstance into this one.voidautoLoad()Enables automatic reloading of the configuration file when it changes on the filesystem.voidEnables automatic reloading of the configuration file when it changes, with a callback.voidclear()Clears all key-value pairs from a specific group.booleancontainsKey(Object key) booleancontainsKey(String group, String key) Checks if a specific group contains a given key.booleancontainsValue(Object value) booleancontainsValue(String group, String value) Checks if a specific group contains a given value.entrySet()Returns a set of all key-value entries within a specific group.booleangetAndRemove(String... keys) Gets and removes a property value.Gets the underlyingGroupedMapthat stores the settings.Gets a list of all group names defined in this setting.Gets all key-value pairs for a specific group as a mutable map.getObjectByGroup(CharSequence key, CharSequence group, Object defaultValue) getProperties(String group) Gets all settings under a specific group as aPropertiesobject.Gets all settings under a specific group as aPropsobject.getSetting(String group) Gets all settings under a specific group as a newSettingobject.Gets the file path of the loaded settings file.Gets the URL of the loaded settings file.inthashCode()booleanisEmpty()booleanChecks if a specific group is empty.keySet()Returns a set of all keys within a specific group.load()Reloads the configuration from the original resource file.static Settingof()Creates a new, emptySettinginstance for manual population.Puts all key-value pairs from a map into a specific group.voidputByGroup(String key, String group, String value) Puts a key-value pair into a specific group.Removes a key from a specific group.Sets a value in the default (empty) group.setByGroup(String key, String group, String value) Sets a value in a specific group.Sets multiple properties using an array of lambda method reference suppliers.setLogIfNull(boolean logIfNull) Sets whether to log a debug message when a requested key is not found.setVarRegex(String regex) Sets the regular expression for identifying variables.intsize()voidStops the automatic reloading of the configuration file.voidstore()Stores the current settings to the original file, overwriting its content.voidStores the current settings to the specified file.voidStores the current settings to a file at the specified absolute path.toProps()Converts thisSettingto aPropsobject, flattening the groups into keys with prefixes (e.g., "group.key").toString()values()Returns a collection of all values within a specific group.Methods inherited from class org.miaixz.bus.setting.magic.AbstractSetting
get, getByGroupNotEmpty, getObject, getStrs, getStrs, getStrsByGroup, getStrsByGroup, toBean, toBean, toBean, toBeanMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.miaixz.bus.core.lang.getter.GroupedTypeGetter
getBigDecimalByGroup, getBigDecimalByGroup, getBigIntegerByGroup, getBigIntegerByGroup, getBooleanByGroup, getBooleanByGroup, getByGroup, getByGroup, getByteByGroup, getByteByGroup, getCharByGroup, getCharByGroup, getDoubleByGroup, getDoubleByGroup, getIntByGroup, getIntByGroup, getLongByGroup, getLongByGroup, getObjectByGroup, getShortByGroup, getShortByGroup, getStringByGroup, getStringByGroupMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllMethods inherited from interface org.miaixz.bus.core.lang.getter.TypeGetter
get, get, get, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getByte, getByte, getBytes, getBytes, getChar, getChar, getDate, getDate, getDouble, getDouble, getEnum, getEnum, getFloat, getFloat, getInt, getInt, getLocalDate, getLocalDate, getLocalDateTime, getLocalDateTime, getLocalTime, getLocalTime, getLong, getLong, getNumber, getNumber, getObject, getShort, getShort, getSqlTime, getSqlTime, getSqlTimestamp, getSqlTimestamp, getString, getString
-
Field Details
-
DEFAULT_CHARSET
The default character set (UTF-8). -
EXT_NAME
The default file extension for settings files.- See Also:
-
charset
The character set for this settings instance. -
isUseVariable
protected boolean isUseVariableWhether variable substitution is enabled. -
resource
protected org.miaixz.bus.core.io.resource.Resource resourceThe resource representing the settings file.
-
-
Constructor Details
-
Setting
public Setting()Constructs a new, emptySettinginstance. -
Setting
Constructs aSettingby loading a file from a relative or absolute path.- Parameters:
path- The path to the settings file.
-
Setting
Constructs aSettingby loading a file.- Parameters:
path- The path to the settings file.isUseVariable-trueto enable variable substitution.
-
Setting
Constructs aSettingby loading a file from the classpath.- Parameters:
path- The path to the file (relative or absolute).charset- The character set to use.isUseVariable-trueto enable variable substitution.
-
Setting
Constructs aSettingfrom aFile.- Parameters:
configFile- The configuration file.charset- The character set to use.isUseVariable-trueto enable variable substitution.
-
Setting
public Setting(org.miaixz.bus.core.io.resource.Resource resource, Charset charset, boolean isUseVariable) Constructs aSettingfrom aResource.- Parameters:
resource- The resource representing the settings file.charset- The character set to use.isUseVariable-trueto enable variable substitution.
-
Setting
Constructs aSettingfrom aResourceusing a custom loader.- Parameters:
resource- The resource representing the settings file.loader- The custom loader to use for parsing.
-
-
Method Details
-
of
Creates a new, emptySettinginstance for manual population.- Returns:
- A new
Settinginstance.
-
load
Reloads the configuration from the original resource file.- Returns:
- This
Settinginstance for chaining.
-
autoLoad
public void autoLoad()Enables automatic reloading of the configuration file when it changes on the filesystem. -
autoLoad
Enables automatic reloading of the configuration file when it changes, with a callback.- Parameters:
callback- A consumer to be called after the file has been successfully reloaded.
-
stopAutoLoad
public void stopAutoLoad()Stops the automatic reloading of the configuration file. -
getSettingUrl
Gets the URL of the loaded settings file.- Returns:
- The URL of the settings file.
-
getSettingPath
Gets the file path of the loaded settings file.- Returns:
- The path of the settings file.
-
size
public int size() -
getObjectByGroup
- Specified by:
getObjectByGroupin interfaceorg.miaixz.bus.core.lang.getter.GroupedTypeGetter<CharSequence,CharSequence>
-
getAndRemove
Gets and removes a property value. It tries each key in the provided list until a non-null value is found, which is then returned and removed.- Parameters:
keys- A list of keys to try, often used for aliases.- Returns:
- The string value, or null if no key is found.
-
getMap
Gets all key-value pairs for a specific group as a mutable map.- Parameters:
group- The group name.- Returns:
- A map of the settings in the group.
-
getSetting
Gets all settings under a specific group as a newSettingobject.- Parameters:
group- The group name.- Returns:
- A new
Settinginstance containing the properties of the group.
-
getProperties
Gets all settings under a specific group as aPropertiesobject.- Parameters:
group- The group name.- Returns:
- A new
Propertiesobject.
-
getProps
Gets all settings under a specific group as aPropsobject.- Parameters:
group- The group name.- Returns:
- A new
Propsobject.
-
store
public void store()Stores the current settings to the original file, overwriting its content. This will not work if the file is inside a JAR. -
store
Stores the current settings to a file at the specified absolute path.- Parameters:
absolutePath- The absolute path to the destination file.
-
store
Stores the current settings to the specified file.- Parameters:
file- The destination file.
-
toProps
Converts thisSettingto aPropsobject, flattening the groups into keys with prefixes (e.g., "group.key").- Returns:
- A
Propsobject.
-
getGroupedMap
Gets the underlyingGroupedMapthat stores the settings.- Returns:
- The underlying
GroupedMapused for storage.
-
getGroups
Gets a list of all group names defined in this setting.- Returns:
- A list of all group names in this setting.
-
setVarRegex
Sets the regular expression for identifying variables.- Parameters:
regex- The regular expression.- Returns:
- this
Settinginstance for chaining.
-
setLogIfNull
Sets whether to log a debug message when a requested key is not found.- Parameters:
logIfNull-trueto enable logging.- Returns:
- this
Settinginstance for chaining.
-
isEmpty
Checks if a specific group is empty.- Parameters:
group- The group name.- Returns:
trueif the group is empty or does not exist.
-
containsKey
Checks if a specific group contains a given key.- Parameters:
group- The group name.key- The key.- Returns:
trueif the key exists in the group.
-
containsValue
Checks if a specific group contains a given value.- Parameters:
group- The group name.value- The value.- Returns:
trueif the value exists in the group.
-
putByGroup
Puts a key-value pair into a specific group.- Parameters:
key- The key.group- The group name.value- The value.- Returns:
- The previous value associated with the key, or null.
-
remove
Removes a key from a specific group.- Parameters:
group- The group name.key- The key to remove.- Returns:
- The removed value, or null if not found.
-
putAll
Puts all key-value pairs from a map into a specific group.- Parameters:
group- The group name.m- The map of key-value pairs.- Returns:
- this
Settinginstance for chaining.
-
addSetting
Merges all groups and settings from anotherSettinginstance into this one.- Parameters:
setting- TheSettinginstance to merge.- Returns:
- this
Settinginstance for chaining.
-
clear
Clears all key-value pairs from a specific group.- Parameters:
group- The group name.- Returns:
- this
Settinginstance for chaining.
-
keySet
Returns a set of all keys within a specific group.- Parameters:
group- The group name.- Returns:
- The set of keys.
-
values
Returns a collection of all values within a specific group.- Parameters:
group- The group name.- Returns:
- The collection of values.
-
entrySet
Returns a set of all key-value entries within a specific group.- Parameters:
group- The group name.- Returns:
- The set of entries.
-
set
Sets a value in the default (empty) group.- Parameters:
key- The key.value- The value.- Returns:
- this
Settinginstance for chaining.
-
setFields
Sets multiple properties using an array of lambda method reference suppliers.Example:
User user = new User("test", "Test User"); Setting.of().setFields(user::getUsername, user::getNickname);- Parameters:
fields- An array of suppliers, where each supplier returns a property value.- Returns:
- this
Settinginstance for chaining.
-
setByGroup
Sets a value in a specific group.- Parameters:
key- The key.group- The group name.value- The value.- Returns:
- this
Settinginstance for chaining.
-
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<String,String>
-
containsValue
- Specified by:
containsValuein interfaceMap<String,String>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
hashCode
public int hashCode() -
equals
-
toString
-