Class Config
- Direct Known Subclasses:
BlobBasedConfig
,StoredConfig
.config
, .gitconfig
, .gitmodules
file.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Converts enumeration values into configuration options and vice-versa, allowing to match a config option with an enum value.static interface
Parses a section of the configuration into an application model object.private static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Config
private static final TypedConfigGetter
private static final String[]
private final ListenerList
the change listenersprivate static final int
private static final String
Magic value indicating a missing entry.private final AtomicReference<ConfigSnapshot>
Immutable current state of the configuration data.private static TypedConfigGetter
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddChangeListener
(ConfigChangedListener listener) Adds a listener to be notified about changes.private void
addIncludedConfig
(List<ConfigLine> newEntries, ConfigLine line, int depth) private static <T> T[]
allValuesOf
(T value) protected void
clear()
Clear the configuration fileprivate static List<ConfigLine>
copy
(ConfigSnapshot src, List<String> values) (package private) static String
(package private) static String
Escape the value before savingprivate static int
findSectionEnd
(List<ConfigLine> entries, String section, String subsection, boolean skipIncludedLines) protected void
Notifies the listenersvoid
Clear this configuration and reset to the contents of the parsed string.private List<ConfigLine>
fromTextRecurse
(String text, int depth, String includedFrom) <T> T
get
(Config.SectionParser<T> parser) Obtain a handle to a parsed set of configuration values.Retrieves this config's base config.private ConfigSnapshot
boolean
getBoolean
(String section, String name, boolean defaultValue) Get a boolean value from the git configboolean
getBoolean
(String section, String subsection, String name, boolean defaultValue) Get a boolean value from the git config<T extends Enum<?>>
TParse an enumeration from the configuration.<T extends Enum<?>>
TParse an enumeration from the configuration.int
Obtain an integer value from the configuration.int
Obtain an integer value from the configuration.int
getIntInRange
(String section, String name, int minValue, int maxValue, int defaultValue) Obtain an integer value from the configuration which must be inside given range.int
getIntInRange
(String section, String subsection, String name, int minValue, int maxValue, int defaultValue) Obtain an integer value from the configuration which must be inside given range.long
Obtain an integer value from the configuration.long
Obtain an integer value from the configuration.Get the list of names defined for this sectionGet the list of names defined for this sectionGet the list of names defined for this subsectionGet the list of names defined for this sectiongetPath
(String section, String subsection, String name, FS fs, File resolveAgainst, Path defaultValue) Parse a string value and treat it as a file path, replacing a ~/ prefix by the user's home directory.(package private) String
getRawString
(String section, String subsection, String name) private String[]
getRawStringList
(String section, String subsection, String name) getRefSpecs
(String section, String subsection, String name) Parse a list ofRefSpec
s from the configuration.Get the sections defined in thisConfig
.private ConfigSnapshot
getState()
Get string value or null if not found.String[]
getStringList
(String section, String subsection, String name) Get a list of string valuesgetSubsections
(String section) Get set of all subsections of specified section within this configuration and its base configurationlong
getTimeUnit
(String section, String subsection, String name, long defaultValue, TimeUnit wantUnit) Parse a numerical time unit, such as "1 minute", from the configuration.static boolean
Check if a given string is the "missing" value.protected boolean
isUtf8
(byte[] bytes) Check if bytes should be treated as UTF-8 or not.private ConfigSnapshot
newState()
private ConfigSnapshot
newState
(List<ConfigLine> entries) protected boolean
Determine whether to issue change events for transient changes.protected byte[]
readIncludedConfig
(String relPath) Read the included config from the specified (possibly) relative pathprivate static String
private static String
private static String
private static String
private ConfigSnapshot
replaceStringList
(ConfigSnapshot srcState, String section, String subsection, String name, List<String> values) void
setBoolean
(String section, String subsection, String name, boolean value) Add or modify a configuration value.<T extends Enum<?>>
voidAdd or modify a configuration value.void
Add or modify a configuration value.void
Add or modify a configuration value.void
Add or modify a configuration value.void
Set a configuration value.static void
Globally sets aTypedConfigGetter
that is subsequently used to read typed values from all git configs.toText()
Get this configuration, formatted as a Git style text file.private static String
toUnicodeLiteral
(int c) void
uncache
(Config.SectionParser<?> parser) Remove a cached configuration object.void
Remove a configuration value.void
unsetSection
(String section, String subsection) Remove all configuration values under a single section.private ConfigSnapshot
unsetSection
(ConfigSnapshot srcState, String section, String subsection)
-
Field Details
-
EMPTY_STRING_ARRAY
-
MAX_DEPTH
private static final int MAX_DEPTH- See Also:
-
DEFAULT_GETTER
-
typedGetter
-
listeners
the change listeners -
state
Immutable current state of the configuration data.This state is copy-on-write. It should always contain an immutable list of the configuration keys/values.
-
baseConfig
-
MISSING_ENTRY
Magic value indicating a missing entry.This value is tested for reference equality in some contexts, so we must ensure it is a special copy of the empty string. It also must be treated like the empty string.
-
-
Constructor Details
-
Config
public Config()Create a configuration with no default fallback. -
Config
Create an empty configuration with a fallback for missing keys.- Parameters:
defaultConfig
- the base configuration to be consulted when a key is missing from this configuration instance.
-
-
Method Details
-
getBaseConfig
Retrieves this config's base config.- Returns:
- the base configuration of this config.
- Since:
- 5.5.2
-
isMissing
Check if a given string is the "missing" value.- Parameters:
value
- string to be checked.- Returns:
- true if the given string is the "missing" value.
- Since:
- 5.4
-
setTypedConfigGetter
Globally sets aTypedConfigGetter
that is subsequently used to read typed values from all git configs.- Parameters:
getter
- to use; ifnull
use the default getter.- Since:
- 4.9
-
escapeValue
Escape the value before saving- Parameters:
x
- the value to escape- Returns:
- the escaped value
-
escapeSubsection
-
getInt
Obtain an integer value from the configuration.- Parameters:
section
- section the key is grouped within.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- an integer value from the configuration, or defaultValue.
-
getInt
Obtain an integer value from the configuration.- Parameters:
section
- section the key is grouped within.subsection
- subsection name, such a remote or branch name.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- an integer value from the configuration, or defaultValue.
-
getIntInRange
Obtain an integer value from the configuration which must be inside given range.- Parameters:
section
- section the key is grouped within.name
- name of the key to get.minValue
- minimum valuemaxValue
- maximum valuedefaultValue
- default value to return if no value was present.- Returns:
- an integer value from the configuration, or defaultValue.
- Since:
- 6.1
-
getIntInRange
public int getIntInRange(String section, String subsection, String name, int minValue, int maxValue, int defaultValue) Obtain an integer value from the configuration which must be inside given range.- Parameters:
section
- section the key is grouped within.subsection
- subsection name, such a remote or branch name.name
- name of the key to get.minValue
- minimum valuemaxValue
- maximum valuedefaultValue
- default value to return if no value was present.- Returns:
- an integer value from the configuration, or defaultValue.
- Since:
- 6.1
-
getLong
Obtain an integer value from the configuration.- Parameters:
section
- section the key is grouped within.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- an integer value from the configuration, or defaultValue.
-
getLong
Obtain an integer value from the configuration.- Parameters:
section
- section the key is grouped within.subsection
- subsection name, such a remote or branch name.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- an integer value from the configuration, or defaultValue.
-
getBoolean
Get a boolean value from the git config- Parameters:
section
- section the key is grouped within.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- true if any value or defaultValue is true, false for missing or explicit false
-
getBoolean
Get a boolean value from the git config- Parameters:
section
- section the key is grouped within.subsection
- subsection name, such a remote or branch name.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- true if any value or defaultValue is true, false for missing or explicit false
-
getEnum
public <T extends Enum<?>> T getEnum(String section, String subsection, String name, T defaultValue) Parse an enumeration from the configuration.- Parameters:
section
- section the key is grouped within.subsection
- subsection name, such a remote or branch name.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- the selected enumeration value, or
defaultValue
.
-
allValuesOf
private static <T> T[] allValuesOf(T value) -
getEnum
public <T extends Enum<?>> T getEnum(T[] all, String section, String subsection, String name, T defaultValue) Parse an enumeration from the configuration.- Parameters:
all
- all possible values in the enumeration which should be recognized. TypicallyEnumType.values()
.section
- section the key is grouped within.subsection
- subsection name, such a remote or branch name.name
- name of the key to get.defaultValue
- default value to return if no value was present.- Returns:
- the selected enumeration value, or
defaultValue
.
-
getString
Get string value or null if not found.- Parameters:
section
- the sectionsubsection
- the subsection for the valuename
- the key name- Returns:
- a String value from the config,
null
if not found
-
getStringList
Get a list of string valuesIf this instance was created with a base, the base's values are returned first (if any).
- Parameters:
section
- the sectionsubsection
- the subsection for the valuename
- the key name- Returns:
- array of zero or more values from the configuration.
-
getTimeUnit
public long getTimeUnit(String section, String subsection, String name, long defaultValue, TimeUnit wantUnit) Parse a numerical time unit, such as "1 minute", from the configuration.- Parameters:
section
- section the key is in.subsection
- subsection the key is in, or null if not in a subsection.name
- the key name.defaultValue
- default value to return if no value was present.wantUnit
- the units ofdefaultValue
and the return value, as well as the units to assume if the value does not contain an indication of the units.- Returns:
- the value, or
defaultValue
if not set, expressed inunits
. - Since:
- 4.5
-
getPath
public Path getPath(String section, String subsection, String name, @NonNull FS fs, File resolveAgainst, Path defaultValue) Parse a string value and treat it as a file path, replacing a ~/ prefix by the user's home directory.Note: this may throw
InvalidPathException
if the string is not a valid path.- Parameters:
section
- section the key is in.subsection
- subsection the key is in, or null if not in a subsection.name
- the key name.fs
- to use to convert the string into a path.resolveAgainst
- directory to resolve the path against if it is a relative path;null
to use the Java process's current directory.defaultValue
- to return if no value was present- Returns:
- the
Path
, ordefaultValue
if not set - Since:
- 5.10
-
getRefSpecs
Parse a list ofRefSpec
s from the configuration.- Parameters:
section
- section the key is in.subsection
- subsection the key is in, or null if not in a subsection.name
- the key name.- Returns:
- a possibly empty list of
RefSpec
s - Since:
- 4.9
-
getSubsections
Get set of all subsections of specified section within this configuration and its base configuration- Parameters:
section
- section to search for.- Returns:
- set of all subsections of specified section within this configuration and its base configuration; may be empty if no subsection exists. The set's iterator returns sections in the order they are declared by the configuration starting from this instance and progressing through the base.
-
getSections
Get the sections defined in thisConfig
.- Returns:
- the sections defined in this
Config
. The set's iterator returns sections in the order they are declared by the configuration starting from this instance and progressing through the base.
-
getNames
Get the list of names defined for this section- Parameters:
section
- the section- Returns:
- the list of names defined for this section
-
getNames
Get the list of names defined for this subsection- Parameters:
section
- the sectionsubsection
- the subsection- Returns:
- the list of names defined for this subsection
-
getNames
Get the list of names defined for this section- Parameters:
section
- the sectionrecursive
- iftrue
recursively adds the names defined in all base configurations- Returns:
- the list of names defined for this section
- Since:
- 3.2
-
getNames
Get the list of names defined for this section- Parameters:
section
- the sectionsubsection
- the subsectionrecursive
- iftrue
recursively adds the names defined in all base configurations- Returns:
- the list of names defined for this subsection
- Since:
- 3.2
-
get
Obtain a handle to a parsed set of configuration values.- Type Parameters:
T
- type of configuration model to return.- Parameters:
parser
- parser which can create the model if it is not already available in this configuration file. The parser is also used as the key into a cache and must obey the hashCode and equals contract in order to reuse a parsed model.- Returns:
- the parsed object instance, which is cached inside this config.
-
uncache
Remove a cached configuration object.If the associated configuration object has not yet been cached, this method has no effect.
- Parameters:
parser
- parser used to obtain the configuration object.- See Also:
-
addChangeListener
Adds a listener to be notified about changes.Clients are supposed to remove the listeners after they are done with them using the
ListenerHandle.remove()
method- Parameters:
listener
- the listener- Returns:
- the handle to the registered listener
-
notifyUponTransientChanges
protected boolean notifyUponTransientChanges()Determine whether to issue change events for transient changes.If
true
is returned (which is the default behavior),fireConfigChangedEvent()
will be called upon each change.Subclasses that override this to return
false
are responsible for issuingfireConfigChangedEvent()
calls themselves.- Returns:
-
fireConfigChangedEvent
protected void fireConfigChangedEvent()Notifies the listeners -
getRawString
-
getRawStringList
-
getState
-
getBaseState
-
setInt
Add or modify a configuration value. The parameters will result in a configuration entry like this.[section "subsection"] name = value
- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter value
-
setLong
Add or modify a configuration value. The parameters will result in a configuration entry like this.[section "subsection"] name = value
- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter value
-
setBoolean
Add or modify a configuration value. The parameters will result in a configuration entry like this.[section "subsection"] name = value
- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter value
-
setEnum
Add or modify a configuration value. The parameters will result in a configuration entry like this.[section "subsection"] name = value
- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter value
-
setString
Add or modify a configuration value. The parameters will result in a configuration entry like this.[section "subsection"] name = value
- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter value, e.g. "true"
-
unset
Remove a configuration value.- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"
-
unsetSection
Remove all configuration values under a single section.- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch name
-
unsetSection
-
setStringList
Set a configuration value.[section "subsection"] name = value1 name = value2
- Parameters:
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"values
- list of zero or more values for this key.
-
replaceStringList
private ConfigSnapshot replaceStringList(ConfigSnapshot srcState, String section, String subsection, String name, List<String> values) -
copy
-
findSectionEnd
private static int findSectionEnd(List<ConfigLine> entries, String section, String subsection, boolean skipIncludedLines) -
toText
Get this configuration, formatted as a Git style text file.- Returns:
- this configuration, formatted as a Git style text file.
-
fromText
Clear this configuration and reset to the contents of the parsed string.- Parameters:
text
- Git style text file listing configuration properties.- Throws:
ConfigInvalidException
- the text supplied is not formatted correctly. No changes were made tothis
.
-
fromTextRecurse
private List<ConfigLine> fromTextRecurse(String text, int depth, String includedFrom) throws ConfigInvalidException - Throws:
ConfigInvalidException
-
readIncludedConfig
Read the included config from the specified (possibly) relative path- Parameters:
relPath
- possibly relative path to the included config, as specified in this config- Returns:
- the read bytes, or null if the included config should be ignored
- Throws:
ConfigInvalidException
- if something went wrong while reading the config- Since:
- 4.10
-
addIncludedConfig
private void addIncludedConfig(List<ConfigLine> newEntries, ConfigLine line, int depth) throws ConfigInvalidException - Throws:
ConfigInvalidException
-
newState
-
newState
-
clear
protected void clear()Clear the configuration file -
isUtf8
protected boolean isUtf8(byte[] bytes) Check if bytes should be treated as UTF-8 or not.- Parameters:
bytes
- the bytes to check encoding for.- Returns:
- true if bytes should be treated as UTF-8, false otherwise.
- Since:
- 4.4
-
readSectionName
- Throws:
ConfigInvalidException
-
readKeyName
- Throws:
ConfigInvalidException
-
readSubsectionName
- Throws:
ConfigInvalidException
-
readValue
- Throws:
ConfigInvalidException
-
toUnicodeLiteral
-