public class Config
extends java.lang.Object
Constructor and Description |
---|
Config()
Create an empty config.
|
Config(Config other)
Copy constructor.
|
Config(java.io.File file)
Create a config that reads from a given file.
|
Modifier and Type | Method and Description |
---|---|
void |
addConstraint(ConfigConstraint c)
Add a constraint.
|
void |
appendValue(java.lang.String key,
java.lang.String value)
Append a value to a key.
|
void |
appendValue(java.lang.String key,
java.lang.String value,
java.lang.String separator)
Append a value to a key.
|
java.util.Set<java.lang.String> |
getAllKeys()
Get all keys in this config.
|
java.util.List<java.lang.String> |
getArrayValue(java.lang.String key)
Get the value of a key as an array of strings.
|
java.util.List<java.lang.String> |
getArrayValue(java.lang.String key,
java.lang.String defaultValue)
Get the value of a key (or a default value) as an array of strings.
|
boolean |
getBooleanValue(java.lang.String key)
Get the value of a key as a boolean.
|
boolean |
getBooleanValue(java.lang.String key,
boolean defaultValue)
Get the value of a key as a boolean or use a default value.
|
double |
getFloatValue(java.lang.String key)
Get the value of a key as a floating point number.
|
double |
getFloatValue(java.lang.String key,
double defaultValue)
Get the value of a key as a floating point number or use a default value.
|
int |
getIntValue(java.lang.String key)
Get the value of a key as an integer.
|
int |
getIntValue(java.lang.String key,
int defaultValue)
Get the value of a key as an integer or use a default value.
|
java.util.Random |
getRandom()
Get the random number generator defined by this config.
|
java.lang.String |
getValue(java.lang.String key)
Get the value of a key as a String.
|
java.lang.String |
getValue(java.lang.String key,
java.lang.String defaultValue)
Get the value of a key as a String or use a default value.
|
java.util.Set<ConfigConstraint> |
getViolatedConstraints()
Get all violated constraints.
|
boolean |
isDefined(java.lang.String key)
Find out if a key is defined.
|
void |
merge(Config other)
Merge all keys and values from another Config into this one.
|
void |
read(java.io.File file)
Read a config file and add its contents.
|
void |
read(java.io.Reader reader,
java.lang.String name)
Read config information from a Reader and add its contents.
|
void |
read(java.lang.String resource)
Read a config file from a resource that this class' classloader can find.
|
void |
removeAllKeys()
Remove all keys.
|
void |
removeConstraint(ConfigConstraint c)
Remove a constraint.
|
void |
removeExcept(java.util.Collection<java.lang.String> exceptions)
Remove all except a set of keys.
|
void |
removeExcept(java.lang.String... exceptions)
Remove all except a set of keys.
|
void |
removeExceptRegex(java.util.Collection<java.lang.String> exceptions)
Remove all keys that do not match any of the given regular expressions.
|
void |
removeKey(java.lang.String key)
Remove a key from the config.
|
void |
setBooleanValue(java.lang.String key,
boolean value)
Set the value of a key as a boolean.
|
void |
setFloatValue(java.lang.String key,
double value)
Set the value of a key as a floating point number.
|
void |
setIntValue(java.lang.String key,
int value)
Set the value of a key as an integer.
|
void |
setValue(java.lang.String key,
java.lang.String value)
Set the value of a key.
|
void |
write(java.io.PrintWriter out)
Write this config to a PrintWriter.
|
public Config()
public Config(java.io.File file) throws ConfigException
read(String)
method.file
- The config file to read. Must not be null.ConfigException
- If there is an error parsing the config file or one of its descendants.public Config(Config other)
other
- The Config to copy.public void read(java.lang.String resource) throws ConfigException
resource
- The name of the resource.ConfigException
- If there is an error parsing the config file or one of its descendants.public void read(java.io.File file) throws ConfigException
file
- The config file to read. Must not be null. If this is a directory then all files in the directory tree will be read.ConfigException
- If there is an error parsing the config file or one of its descendants.public void read(java.io.Reader reader, java.lang.String name) throws ConfigException
reader
- The Reader to read from. Must not be null.name
- The name of the reader being read. This is used when reporting errors in the file.ConfigException
- If there is an error parsing the config file or one of its descendants.public void write(java.io.PrintWriter out) throws java.io.IOException
out
- The PrintWriter to write to. Must not be null.java.io.IOException
- If there is an error writing to the stream.public void merge(Config other)
other
- The Config to merge from.public void addConstraint(ConfigConstraint c)
c
- The constraint to add.public void removeConstraint(ConfigConstraint c)
c
- The constraint to remove.public java.util.Set<ConfigConstraint> getViolatedConstraints()
public java.util.Set<java.lang.String> getAllKeys()
public boolean isDefined(java.lang.String key)
key
- The key to test.public java.lang.String getValue(java.lang.String key)
key
- The key to look up. Must not be null.NoSuchConfigOptionException
- If the key is not defined.public java.lang.String getValue(java.lang.String key, java.lang.String defaultValue)
key
- The key to look up. Must not be null.defaultValue
- The default value to return if the key has no defined value.public int getIntValue(java.lang.String key)
key
- The key to look up. Must not be null.NoSuchConfigOptionException
- If the key is not defined.java.lang.NumberFormatException
- If the value of the key cannot be interpreted as an integer.public int getIntValue(java.lang.String key, int defaultValue)
key
- The key to look up. Must not be null.defaultValue
- The default value to return if the key has no defined value.java.lang.NumberFormatException
- If the value of the key cannot be interpreted as an integer.public double getFloatValue(java.lang.String key)
key
- The key to look up. Must not be null.NoSuchConfigOptionException
- If the key is not defined.java.lang.NumberFormatException
- If the value of the key cannot be interpreted as a floating point number.public double getFloatValue(java.lang.String key, double defaultValue)
key
- The key to look up. Must not be null.defaultValue
- The default value to return if the key has no defined value.java.lang.NumberFormatException
- If the value of the key cannot be interpreted as a floating point number.public boolean getBooleanValue(java.lang.String key)
key
- The key to look up. Must not be null.NoSuchConfigOptionException
- If the key is not defined.public boolean getBooleanValue(java.lang.String key, boolean defaultValue)
key
- The key to look up. Must not be null.defaultValue
- The default value to return if the key has no defined value.public java.util.List<java.lang.String> getArrayValue(java.lang.String key)
key
- The key to look up. Must not be null.NoSuchConfigOptionException
- If the key is not defined.public java.util.List<java.lang.String> getArrayValue(java.lang.String key, java.lang.String defaultValue)
key
- The key to look up. Must not be null.defaultValue
- The default value to use if the key is not defined. If this is null then null will be returned, otherwise it will be split according to the usual rules.public void setValue(java.lang.String key, java.lang.String value)
key
- The key to set. Must not be null.value
- The new value. If this is null then removeKey(String)
is called with the given key.public void appendValue(java.lang.String key, java.lang.String value)
setValue(String, String)
. This method calls appendValue(String, String, String)
with a space character as the separator.key
- The key to append.value
- The value to append.public void appendValue(java.lang.String key, java.lang.String value, java.lang.String separator)
setValue(String, String)
.key
- The key to append.value
- The value to append.separator
- A string to add before the new value to separate it from the previous value.public void setIntValue(java.lang.String key, int value)
key
- The key to set. Must not be null.value
- The new value.public void setFloatValue(java.lang.String key, double value)
key
- The key to set. Must not be null.value
- The new value.public void setBooleanValue(java.lang.String key, boolean value)
key
- The key to set. Must not be null.value
- The new value.public void removeKey(java.lang.String key)
key
- The key to remove. Must not be null.public void removeAllKeys()
public void removeExcept(java.lang.String... exceptions)
exceptions
- The keys to keep.public void removeExcept(java.util.Collection<java.lang.String> exceptions)
exceptions
- The keys to keep.public void removeExceptRegex(java.util.Collection<java.lang.String> exceptions)
exceptions
- The regular expressions describing keys to keep.public java.util.Random getRandom()