public class RaftProperties extends Object
| 构造器和说明 |
|---|
RaftProperties()
A new configuration.
|
RaftProperties(boolean loadDefaults)
A new configuration where the behavior of reading from the default
resources can be turned off.
|
RaftProperties(RaftProperties other)
A new RaftProperties with the same settings cloned from another.
|
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
addDefaultResource(String name)
Add a default resource.
|
void |
addResource(InputStream in)
Add a configuration resource.
|
void |
addResource(InputStream in,
String name)
Add a configuration resource.
|
void |
addResource(RaftProperties conf)
Add a configuration resource.
|
void |
addResource(String name)
Add a configuration resource.
|
void |
addResource(URL path) |
void |
clear()
Clears all keys from the configuration.
|
String |
get(String name)
Get the value of the
name property, null if
no such property exists. |
String |
get(String name,
String defaultValue)
Get the value of the
name. |
boolean |
getBoolean(String name,
boolean defaultValue)
Get the value of the
name property as a boolean. |
Class<?> |
getClass(String name,
Class<?> defaultValue)
Get the value of the
name property as a Class. |
<BASE> Class<? extends BASE> |
getClass(String name,
Class<? extends BASE> defaultValue,
Class<BASE> xface)
Get the value of the
name property as a Class
implementing the interface specified by xface. |
Class<?>[] |
getClasses(String name,
Class<?>... defaultValue)
Get the value of the
name property
as an array of Class. |
double |
getDouble(String name,
double defaultValue)
Get the value of the
name property as a double. |
<T extends Enum<T>> |
getEnum(String name,
T defaultValue)
Return value matching this enumerated type.
|
File |
getFile(String name,
File defaultValue) |
List<File> |
getFiles(String name,
List<File> defaultValue)
Get the value of the
name property as a list
of File. |
float |
getFloat(String name,
float defaultValue)
Get the value of the
name property as a float. |
int |
getInt(String name,
int defaultValue)
Get the value of the
name property as an int. |
int[] |
getInts(String name)
Get the value of the
name property as a set of comma-delimited
int values. |
long |
getLong(String name,
long defaultValue)
Get the value of the
name property as a long. |
Pattern |
getPattern(String name,
Pattern defaultValue)
Get the value of the
name property as a Pattern. |
protected Properties |
getProps() |
String |
getRaw(String name)
Get the value of the
name property, without doing
variable expansion.If the key is
deprecated, it returns the value of the first key which replaces
the deprecated key and is not null. |
SizeInBytes |
getSizeInBytes(String name,
SizeInBytes defaultValue) |
TimeDuration |
getTimeDuration(String name,
TimeDuration defaultValue,
TimeUnit defaultUnit)
Return time duration in the given time unit.
|
BiFunction<String,TimeDuration,TimeDuration> |
getTimeDuration(TimeUnit defaultUnit) |
String |
getTrimmed(String name)
Get the value of the
name property as a trimmed String,
null if no such property exists. |
String |
getTrimmed(String name,
String defaultValue)
Get the value of the
name property as a trimmed String,
defaultValue if no such property exists. |
String[] |
getTrimmedStrings(String name)
Get the comma delimited values of the
name property as
an array of Strings, trimmed of the leading and trailing whitespace. |
Map<String,String> |
getValByRegex(String regex)
get keys matching the the regex
|
void |
reloadConfiguration()
Reload configuration from previously added resources.
|
void |
set(String name,
String value)
Set the
value of the name property. |
void |
setBoolean(String name,
boolean value)
Set the value of the
name property to a boolean. |
void |
setBooleanIfUnset(String name,
boolean value)
Set the given property, if it is currently unset.
|
void |
setClass(String name,
Class<?> theClass,
Class<?> xface)
Set the value of the
name property to the name of a
theClass implementing the given interface xface. |
void |
setDouble(String name,
double value)
Set the value of the
name property to a double. |
<T extends Enum<T>> |
setEnum(String name,
T value)
Set the value of the
name property to the given type. |
void |
setFile(String name,
File value) |
void |
setFiles(String name,
List<File> value) |
void |
setFloat(String name,
float value)
Set the value of the
name property to a float. |
void |
setIfUnset(String name,
String value)
Sets a property if it is currently unset.
|
void |
setInt(String name,
int value)
Set the value of the
name property to an int. |
void |
setLong(String name,
long value)
Set the value of the
name property to a long. |
void |
setPattern(String name,
Pattern pattern)
Set the given property to
Pattern. |
void |
setTimeDuration(String name,
TimeDuration value)
Set the value of
name to the given time duration. |
int |
size()
Return the number of keys in the configuration.
|
String |
toString() |
void |
unset(String name)
Unset a previously set property.
|
void |
writeXml(OutputStream out)
Write out the non-default properties in this configuration to the given
OutputStream using UTF-8 encoding. |
void |
writeXml(Writer out)
Write out the non-default properties in this configuration to the given
Writer. |
public RaftProperties()
public RaftProperties(boolean loadDefaults)
loadDefaults is false, the new instance
will not load resources from the default files.loadDefaults - specifies whether to load from the default filespublic RaftProperties(RaftProperties other)
other - the RaftProperties from which to clone settings.public static void addDefaultResource(String name)
name - file name. File should be present in the classpath.public void addResource(String name)
name - resource to be added, the classpath is examined for a file
with that name.public void addResource(URL path)
public void addResource(InputStream in)
in - InputStream to deserialize the object from. In will be read from
when a get or set is called next. After it is read the stream will be
closed.public void addResource(InputStream in, String name)
in - InputStream to deserialize the object from.name - the name of the resource because InputStream.toString is not
very descriptive some times.public void addResource(RaftProperties conf)
conf - Configuration object from which to load propertiespublic void reloadConfiguration()
public String get(String name)
name property, null if
no such property exists. If the key is deprecated, it returns the value of
the first key which replaces the deprecated key and is not null.
Values are processed for variable expansion
before being returned.name - the property name, will be trimmed before get value.name or its replacing property,
or null if no such property exists.public String getTrimmed(String name)
name property as a trimmed String,
null if no such property exists.
If the key is deprecated, it returns the value of
the first key which replaces the deprecated key and is not null
Values are processed for variable expansion
before being returned.name - the property name.name or its replacing property,
or null if no such property exists.public String getTrimmed(String name, String defaultValue)
name property as a trimmed String,
defaultValue if no such property exists.
See @{Configuration#getTrimmed} for more details.name - the property name.defaultValue - the property default value.name or defaultValue
if it is not set.public String getRaw(String name)
name property, without doing
variable expansion.If the key is
deprecated, it returns the value of the first key which replaces
the deprecated key and is not null.name - the property name.name property or
its replacing property and null if no such property exists.public void set(String name, String value)
value of the name property. If
name is deprecated, it also sets the value to
the keys that replace the deprecated key. Name will be trimmed before put
into configuration.name - property name.value - property value.IllegalArgumentException - when the value or name is null.public void unset(String name)
public void setIfUnset(String name, String value)
name - the property namevalue - the new valuepublic String get(String name, String defaultValue)
name. If the key is deprecated,
it returns the value of the first key which replaces the deprecated key
and is not null.
If no such property exists,
then defaultValue is returned.name - property name, will be trimmed before get value.defaultValue - default value.defaultValue if the property
doesn't exist.public int getInt(String name, int defaultValue)
name property as an int.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid int,
then an error is thrown.name - property name.defaultValue - default value.int,
or defaultValue.NumberFormatException - when the value is invalidpublic int[] getInts(String name)
name property as a set of comma-delimited
int values.
If no such property exists, an empty array is returned.name - property nameint valuespublic void setInt(String name, int value)
name property to an int.name - property name.value - int value of the property.public long getLong(String name, long defaultValue)
name property as a long.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid long,
then an error is thrown.name - property name.defaultValue - default value.long,
or defaultValue.NumberFormatException - when the value is invalidpublic File getFile(String name, File defaultValue)
public List<File> getFiles(String name, List<File> defaultValue)
name property as a list
of File.
The value of the property specifies a list of comma separated path names.
If no such property is specified, then defaultValue is
returned.name - the property name.defaultValue - default value.defaultValue.public SizeInBytes getSizeInBytes(String name, SizeInBytes defaultValue)
public void setLong(String name, long value)
name property to a long.name - property name.value - long value of the property.public float getFloat(String name, float defaultValue)
name property as a float.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid float,
then an error is thrown.name - property name.defaultValue - default value.float,
or defaultValue.NumberFormatException - when the value is invalidpublic void setFloat(String name, float value)
name property to a float.name - property name.value - property value.public double getDouble(String name, double defaultValue)
name property as a double.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid double,
then an error is thrown.name - property name.defaultValue - default value.double,
or defaultValue.NumberFormatException - when the value is invalidpublic void setDouble(String name, double value)
name property to a double.name - property name.value - property value.public boolean getBoolean(String name, boolean defaultValue)
name property as a boolean.
If no such property is specified, or if the specified value is not a valid
boolean, then defaultValue is returned.name - property name.defaultValue - default value.boolean,
or defaultValue.public void setBoolean(String name, boolean value)
name property to a boolean.name - property name.value - boolean value of the property.public void setBooleanIfUnset(String name, boolean value)
name - property namevalue - new valuepublic <T extends Enum<T>> void setEnum(String name, T value)
name property to the given type. This
is equivalent to set(<name>, value.toString()).name - property namevalue - new valuepublic <T extends Enum<T>> T getEnum(String name, T defaultValue)
name - Property namedefaultValue - Value returned if no mapping existsIllegalArgumentException - If mapping is illegal for the type
providedpublic void setTimeDuration(String name, TimeDuration value)
name to the given time duration. This
is equivalent to set(<name>, value + <time suffix>).name - Property namevalue - Time durationpublic TimeDuration getTimeDuration(String name, TimeDuration defaultValue, TimeUnit defaultUnit)
name - Property namedefaultValue - Value returned if no mapping exists.NumberFormatException - If the property stripped of its unit is not
a numberpublic BiFunction<String,TimeDuration,TimeDuration> getTimeDuration(TimeUnit defaultUnit)
public Pattern getPattern(String name, Pattern defaultValue)
name property as a Pattern.
If no such property is specified, or if the specified value is not a valid
Pattern, then DefaultValue is returned.
Note that the returned value is NOT trimmed by this method.name - property namedefaultValue - default valuepublic void setPattern(String name, Pattern pattern)
Pattern.
If the pattern is passed as null, sets the empty pattern which results in
further calls to getPattern(...) returning the default value.name - property namepattern - new valuepublic String[] getTrimmedStrings(String name)
name property as
an array of Strings, trimmed of the leading and trailing whitespace.
If no such property is specified then an empty array is returned.name - property name.Strings,
or empty array.public Class<?>[] getClasses(String name, Class<?>... defaultValue)
name property
as an array of Class.
The value of the property specifies a list of comma separated class names.
If no such property is specified, then defaultValue is
returned.name - the property name.defaultValue - default value.Class[],
or defaultValue.public Class<?> getClass(String name, Class<?> defaultValue)
name property as a Class.
If no such property is specified, then defaultValue is
returned.name - the class name.defaultValue - default value.Class,
or defaultValue.public <BASE> Class<? extends BASE> getClass(String name, Class<? extends BASE> defaultValue, Class<BASE> xface)
name property as a Class
implementing the interface specified by xface.
If no such property is specified, then defaultValue is
returned.
An exception is thrown if the returned class does not implement the named
interface.name - the class name.defaultValue - default value.xface - the interface implemented by the named class.Class,
or defaultValue.public void setClass(String name, Class<?> theClass, Class<?> xface)
name property to the name of a
theClass implementing the given interface xface.
An exception is thrown if theClass does not implement the
interface xface.name - property name.theClass - property value.xface - the interface implemented by the named class.protected Properties getProps()
public int size()
public void clear()
public void writeXml(OutputStream out) throws IOException
OutputStream using UTF-8 encoding.out - the output stream to write to.IOExceptionpublic void writeXml(Writer out) throws IOException
Writer.out - the writer to write to.IOExceptionCopyright © 2017–2021 The Apache Software Foundation. All rights reserved.