public class Config
extends java.lang.Object
.config
, .gitconfig
, .gitmodules
file.Modifier and Type | Class and Description |
---|---|
static interface |
Config.ConfigEnum
Converts enumeration values into configuration options and vice-versa,
allowing to match a config option with an enum value.
|
static interface |
Config.SectionParser<T>
Parses a section of the configuration into an application model object.
|
private static class |
Config.StringReader |
Modifier and Type | Field and Description |
---|---|
private Config |
baseConfig |
private static TypedConfigGetter |
DEFAULT_GETTER |
private static java.lang.String[] |
EMPTY_STRING_ARRAY |
(package private) static long |
GiB |
(package private) static long |
KiB |
private ListenerList |
listeners
the change listeners
|
private static int |
MAX_DEPTH |
(package private) static long |
MiB |
private static java.lang.String |
MISSING_ENTRY
Magic value indicating a missing entry.
|
private java.util.concurrent.atomic.AtomicReference<ConfigSnapshot> |
state
Immutable current state of the configuration data.
|
private static TypedConfigGetter |
typedGetter |
Constructor and Description |
---|
Config()
Create a configuration with no default fallback.
|
Config(Config defaultConfig)
Create an empty configuration with a fallback for missing keys.
|
Modifier and Type | Method and Description |
---|---|
ListenerHandle |
addChangeListener(ConfigChangedListener listener)
Adds a listener to be notified about changes.
|
private void |
addIncludedConfig(java.util.List<ConfigLine> newEntries,
ConfigLine line,
int depth) |
private static <T> T[] |
allValuesOf(T value) |
protected void |
clear()
Clear the configuration file
|
private static java.util.List<ConfigLine> |
copy(ConfigSnapshot src,
java.util.List<java.lang.String> values) |
(package private) static java.lang.String |
escapeSubsection(java.lang.String x) |
(package private) static java.lang.String |
escapeValue(java.lang.String x)
Escape the value before saving
|
private static int |
findSectionEnd(java.util.List<ConfigLine> entries,
java.lang.String section,
java.lang.String subsection,
boolean skipIncludedLines) |
protected void |
fireConfigChangedEvent()
Notifies the listeners
|
void |
fromText(java.lang.String text)
Clear this configuration and reset to the contents of the parsed string.
|
private java.util.List<ConfigLine> |
fromTextRecurse(java.lang.String text,
int depth,
java.lang.String includedFrom) |
<T> T |
get(Config.SectionParser<T> parser)
Obtain a handle to a parsed set of configuration values.
|
Config |
getBaseConfig()
Retrieves this config's base config.
|
private ConfigSnapshot |
getBaseState() |
boolean |
getBoolean(java.lang.String section,
java.lang.String name,
boolean defaultValue)
Get a boolean value from the git config
|
boolean |
getBoolean(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
boolean defaultValue)
Get a boolean value from the git config
|
<T extends java.lang.Enum<?>> |
getEnum(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
T defaultValue)
Parse an enumeration from the configuration.
|
<T extends java.lang.Enum<?>> |
getEnum(T[] all,
java.lang.String section,
java.lang.String subsection,
java.lang.String name,
T defaultValue)
Parse an enumeration from the configuration.
|
int |
getInt(java.lang.String section,
java.lang.String name,
int defaultValue)
Obtain an integer value from the configuration.
|
int |
getInt(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
int defaultValue)
Obtain an integer value from the configuration.
|
long |
getLong(java.lang.String section,
java.lang.String name,
long defaultValue)
Obtain an integer value from the configuration.
|
long |
getLong(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
long defaultValue)
Obtain an integer value from the configuration.
|
java.util.Set<java.lang.String> |
getNames(java.lang.String section)
Get the list of names defined for this section
|
java.util.Set<java.lang.String> |
getNames(java.lang.String section,
boolean recursive)
Get the list of names defined for this section
|
java.util.Set<java.lang.String> |
getNames(java.lang.String section,
java.lang.String subsection)
Get the list of names defined for this subsection
|
java.util.Set<java.lang.String> |
getNames(java.lang.String section,
java.lang.String subsection,
boolean recursive)
Get the list of names defined for this section
|
(package private) java.lang.String |
getRawString(java.lang.String section,
java.lang.String subsection,
java.lang.String name) |
private java.lang.String[] |
getRawStringList(java.lang.String section,
java.lang.String subsection,
java.lang.String name) |
java.util.List<RefSpec> |
getRefSpecs(java.lang.String section,
java.lang.String subsection,
java.lang.String name)
Parse a list of
RefSpec s from the
configuration. |
java.util.Set<java.lang.String> |
getSections()
Get the sections defined in this
Config . |
private ConfigSnapshot |
getState() |
java.lang.String |
getString(java.lang.String section,
java.lang.String subsection,
java.lang.String name)
Get string value or null if not found.
|
java.lang.String[] |
getStringList(java.lang.String section,
java.lang.String subsection,
java.lang.String name)
Get a list of string values
|
java.util.Set<java.lang.String> |
getSubsections(java.lang.String section)
Get set of all subsections of specified section within this configuration
and its base configuration
|
long |
getTimeUnit(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
long defaultValue,
java.util.concurrent.TimeUnit wantUnit)
Parse a numerical time unit, such as "1 minute", from the configuration.
|
static boolean |
isMissing(java.lang.String value)
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(java.util.List<ConfigLine> entries) |
protected boolean |
notifyUponTransientChanges()
Determine whether to issue change events for transient changes.
|
protected byte[] |
readIncludedConfig(java.lang.String relPath)
Read the included config from the specified (possibly) relative path
|
private static java.lang.String |
readKeyName(Config.StringReader in) |
private static java.lang.String |
readSectionName(Config.StringReader in) |
private static java.lang.String |
readSubsectionName(Config.StringReader in) |
private static java.lang.String |
readValue(Config.StringReader in) |
private ConfigSnapshot |
replaceStringList(ConfigSnapshot srcState,
java.lang.String section,
java.lang.String subsection,
java.lang.String name,
java.util.List<java.lang.String> values) |
void |
setBoolean(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
boolean value)
Add or modify a configuration value.
|
<T extends java.lang.Enum<?>> |
setEnum(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
T value)
Add or modify a configuration value.
|
void |
setInt(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
int value)
Add or modify a configuration value.
|
void |
setLong(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
long value)
Add or modify a configuration value.
|
void |
setString(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
java.lang.String value)
Add or modify a configuration value.
|
void |
setStringList(java.lang.String section,
java.lang.String subsection,
java.lang.String name,
java.util.List<java.lang.String> values)
Set a configuration value.
|
static void |
setTypedConfigGetter(TypedConfigGetter getter)
Globally sets a
TypedConfigGetter that is
subsequently used to read typed values from all git configs. |
java.lang.String |
toText()
Get this configuration, formatted as a Git style text file.
|
private static java.lang.String |
toUnicodeLiteral(int c) |
void |
uncache(Config.SectionParser<?> parser)
Remove a cached configuration object.
|
void |
unset(java.lang.String section,
java.lang.String subsection,
java.lang.String name)
Remove a configuration value.
|
private ConfigSnapshot |
unsetSection(ConfigSnapshot srcState,
java.lang.String section,
java.lang.String subsection) |
void |
unsetSection(java.lang.String section,
java.lang.String subsection)
Remove all configuration values under a single section.
|
private static final java.lang.String[] EMPTY_STRING_ARRAY
static final long KiB
static final long MiB
static final long GiB
private static final int MAX_DEPTH
private static final TypedConfigGetter DEFAULT_GETTER
private static TypedConfigGetter typedGetter
private final ListenerList listeners
private final java.util.concurrent.atomic.AtomicReference<ConfigSnapshot> state
This state is copy-on-write. It should always contain an immutable list of the configuration keys/values.
private final Config baseConfig
private static final java.lang.String 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.
public Config()
public Config(Config defaultConfig)
defaultConfig
- the base configuration to be consulted when a key is missing
from this configuration instance.public Config getBaseConfig()
public static boolean isMissing(java.lang.String value)
value
- string to be checked.public static void setTypedConfigGetter(TypedConfigGetter getter)
TypedConfigGetter
that is
subsequently used to read typed values from all git configs.getter
- to use; if null
use the default getter.static java.lang.String escapeValue(java.lang.String x)
x
- the value to escapestatic java.lang.String escapeSubsection(java.lang.String x)
public int getInt(java.lang.String section, java.lang.String name, int defaultValue)
section
- section the key is grouped within.name
- name of the key to get.defaultValue
- default value to return if no value was present.public int getInt(java.lang.String section, java.lang.String subsection, java.lang.String name, int defaultValue)
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.public long getLong(java.lang.String section, java.lang.String name, long defaultValue)
section
- section the key is grouped within.name
- name of the key to get.defaultValue
- default value to return if no value was present.public long getLong(java.lang.String section, java.lang.String subsection, java.lang.String name, long defaultValue)
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.public boolean getBoolean(java.lang.String section, java.lang.String name, boolean defaultValue)
section
- section the key is grouped within.name
- name of the key to get.defaultValue
- default value to return if no value was present.public boolean getBoolean(java.lang.String section, java.lang.String subsection, java.lang.String name, boolean defaultValue)
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.public <T extends java.lang.Enum<?>> T getEnum(java.lang.String section, java.lang.String subsection, java.lang.String name, T defaultValue)
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.defaultValue
.private static <T> T[] allValuesOf(T value)
public <T extends java.lang.Enum<?>> T getEnum(T[] all, java.lang.String section, java.lang.String subsection, java.lang.String name, T defaultValue)
all
- all possible values in the enumeration which should be
recognized. Typically EnumType.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.defaultValue
.public java.lang.String getString(java.lang.String section, java.lang.String subsection, java.lang.String name)
section
- the sectionsubsection
- the subsection for the valuename
- the key namenull
if not foundpublic java.lang.String[] getStringList(java.lang.String section, java.lang.String subsection, java.lang.String name)
If this instance was created with a base, the base's values are returned first (if any).
section
- the sectionsubsection
- the subsection for the valuename
- the key namepublic long getTimeUnit(java.lang.String section, java.lang.String subsection, java.lang.String name, long defaultValue, java.util.concurrent.TimeUnit wantUnit)
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 of defaultValue
and the return value, as
well as the units to assume if the value does not contain an
indication of the units.defaultValue
if not set, expressed in
units
.public java.util.List<RefSpec> getRefSpecs(java.lang.String section, java.lang.String subsection, java.lang.String name)
RefSpec
s from the
configuration.section
- section the key is in.subsection
- subsection the key is in, or null if not in a subsection.name
- the key name.RefSpec
spublic java.util.Set<java.lang.String> getSubsections(java.lang.String section)
section
- section to search for.public java.util.Set<java.lang.String> getSections()
Config
.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.public java.util.Set<java.lang.String> getNames(java.lang.String section)
section
- the sectionpublic java.util.Set<java.lang.String> getNames(java.lang.String section, java.lang.String subsection)
section
- the sectionsubsection
- the subsectionpublic java.util.Set<java.lang.String> getNames(java.lang.String section, boolean recursive)
section
- the sectionrecursive
- if true
recursively adds the names defined in all base
configurationspublic java.util.Set<java.lang.String> getNames(java.lang.String section, java.lang.String subsection, boolean recursive)
section
- the sectionsubsection
- the subsectionrecursive
- if true
recursively adds the names defined in all base
configurationspublic <T> T get(Config.SectionParser<T> parser)
T
- type of configuration model to return.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.public void uncache(Config.SectionParser<?> parser)
If the associated configuration object has not yet been cached, this method has no effect.
parser
- parser used to obtain the configuration object.get(SectionParser)
public ListenerHandle addChangeListener(ConfigChangedListener listener)
Clients are supposed to remove the listeners after they are done with
them using the ListenerHandle.remove()
method
listener
- the listenerprotected boolean notifyUponTransientChanges()
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 issuing fireConfigChangedEvent()
calls
themselves.
protected void fireConfigChangedEvent()
java.lang.String getRawString(java.lang.String section, java.lang.String subsection, java.lang.String name)
private java.lang.String[] getRawStringList(java.lang.String section, java.lang.String subsection, java.lang.String name)
private ConfigSnapshot getState()
private ConfigSnapshot getBaseState()
public void setInt(java.lang.String section, java.lang.String subsection, java.lang.String name, int value)
[section "subsection"] name = value
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter valuepublic void setLong(java.lang.String section, java.lang.String subsection, java.lang.String name, long value)
[section "subsection"] name = value
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter valuepublic void setBoolean(java.lang.String section, java.lang.String subsection, java.lang.String name, boolean value)
[section "subsection"] name = value
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter valuepublic <T extends java.lang.Enum<?>> void setEnum(java.lang.String section, java.lang.String subsection, java.lang.String name, T value)
[section "subsection"] name = value
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"value
- parameter valuepublic void setString(java.lang.String section, java.lang.String subsection, java.lang.String name, java.lang.String value)
[section "subsection"] name = value
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"public void unset(java.lang.String section, java.lang.String subsection, java.lang.String name)
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch namename
- parameter name, e.g. "filemode"public void unsetSection(java.lang.String section, java.lang.String subsection)
section
- section name, e.g "branch"subsection
- optional subsection value, e.g. a branch nameprivate ConfigSnapshot unsetSection(ConfigSnapshot srcState, java.lang.String section, java.lang.String subsection)
public void setStringList(java.lang.String section, java.lang.String subsection, java.lang.String name, java.util.List<java.lang.String> values)
[section "subsection"] name = value1 name = value2
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.private ConfigSnapshot replaceStringList(ConfigSnapshot srcState, java.lang.String section, java.lang.String subsection, java.lang.String name, java.util.List<java.lang.String> values)
private static java.util.List<ConfigLine> copy(ConfigSnapshot src, java.util.List<java.lang.String> values)
private static int findSectionEnd(java.util.List<ConfigLine> entries, java.lang.String section, java.lang.String subsection, boolean skipIncludedLines)
public java.lang.String toText()
public void fromText(java.lang.String text) throws ConfigInvalidException
text
- Git style text file listing configuration properties.ConfigInvalidException
- the text supplied is not formatted correctly. No changes were
made to this
.private java.util.List<ConfigLine> fromTextRecurse(java.lang.String text, int depth, java.lang.String includedFrom) throws ConfigInvalidException
ConfigInvalidException
protected byte[] readIncludedConfig(java.lang.String relPath) throws ConfigInvalidException
relPath
- possibly relative path to the included config, as specified in
this configConfigInvalidException
- if something went wrong while reading the configprivate void addIncludedConfig(java.util.List<ConfigLine> newEntries, ConfigLine line, int depth) throws ConfigInvalidException
ConfigInvalidException
private ConfigSnapshot newState()
private ConfigSnapshot newState(java.util.List<ConfigLine> entries)
protected void clear()
protected boolean isUtf8(byte[] bytes)
bytes
- the bytes to check encoding for.private static java.lang.String readSectionName(Config.StringReader in) throws ConfigInvalidException
ConfigInvalidException
private static java.lang.String readKeyName(Config.StringReader in) throws ConfigInvalidException
ConfigInvalidException
private static java.lang.String readSubsectionName(Config.StringReader in) throws ConfigInvalidException
ConfigInvalidException
private static java.lang.String readValue(Config.StringReader in) throws ConfigInvalidException
ConfigInvalidException
private static java.lang.String toUnicodeLiteral(int c)