public class ExtendedProperties
extends java.util.Hashtable
Please consider using the PropertiesConfiguration
class in
Commons-Configuration as soon as it is released.
The Extended Properties syntax is explained here:
key = value
Here is an example of a valid extended properties file:
# lines starting with # are comments # This is the simplest property key = value # A long property may be separated on multiple lines longvalue = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # This is a property with many tokens tokens_on_a_line = first token, second token # This sequence generates exactly the same result tokens_on_multiple_lines = first token tokens_on_multiple_lines = second token # commas may be escaped in tokens commas.escaped = Hi\, what'up?
NOTE: this class has not been written for performance nor low memory usage. In fact, it's way slower than it could be and generates too much memory garbage. But since performance is not an issue during intialization (and there is not much time to improve it), I wrote it this way. If you don't like it, go ahead and tune it up!
Constructor and Description |
---|
ExtendedProperties()
Creates an empty extended properties object.
|
ExtendedProperties(java.lang.String file)
Creates and loads the extended properties from the specified file.
|
ExtendedProperties(java.lang.String file,
java.lang.String defaultFile)
Creates and loads the extended properties from the specified file.
|
Modifier and Type | Method and Description |
---|---|
void |
addProperty(java.lang.String key,
java.lang.Object value)
Add a property to the configuration.
|
void |
clearProperty(java.lang.String key)
Clear a property in the configuration.
|
void |
combine(ExtendedProperties props)
Combines an existing Hashtable with this Hashtable.
|
static ExtendedProperties |
convertProperties(java.util.Properties props)
Convert a standard properties class into a configuration class.
|
void |
display()
Display the configuration for debugging purposes to System.out.
|
boolean |
getBoolean(java.lang.String key)
Get a boolean associated with the given configuration key.
|
boolean |
getBoolean(java.lang.String key,
boolean defaultValue)
Get a boolean associated with the given configuration key.
|
java.lang.Boolean |
getBoolean(java.lang.String key,
java.lang.Boolean defaultValue)
Get a boolean associated with the given configuration key.
|
byte |
getByte(java.lang.String key)
Get a byte associated with the given configuration key.
|
byte |
getByte(java.lang.String key,
byte defaultValue)
Get a byte associated with the given configuration key.
|
java.lang.Byte |
getByte(java.lang.String key,
java.lang.Byte defaultValue)
Get a byte associated with the given configuration key.
|
double |
getDouble(java.lang.String key)
Get a double associated with the given configuration key.
|
double |
getDouble(java.lang.String key,
double defaultValue)
Get a double associated with the given configuration key.
|
java.lang.Double |
getDouble(java.lang.String key,
java.lang.Double defaultValue)
Get a double associated with the given configuration key.
|
float |
getFloat(java.lang.String key)
Get a float associated with the given configuration key.
|
float |
getFloat(java.lang.String key,
float defaultValue)
Get a float associated with the given configuration key.
|
java.lang.Float |
getFloat(java.lang.String key,
java.lang.Float defaultValue)
Get a float associated with the given configuration key.
|
java.lang.String |
getInclude()
Gets the property value for including other properties files.
|
int |
getInt(java.lang.String name)
The purpose of this method is to get the configuration resource
with the given name as an integer.
|
int |
getInt(java.lang.String name,
int def)
The purpose of this method is to get the configuration resource
with the given name as an integer, or a default value.
|
int |
getInteger(java.lang.String key)
Get a int associated with the given configuration key.
|
int |
getInteger(java.lang.String key,
int defaultValue)
Get a int associated with the given configuration key.
|
java.lang.Integer |
getInteger(java.lang.String key,
java.lang.Integer defaultValue)
Get a int associated with the given configuration key.
|
java.util.Iterator |
getKeys()
Get the list of the keys contained in the configuration
repository.
|
java.util.Iterator |
getKeys(java.lang.String prefix)
Get the list of the keys contained in the configuration
repository that match the specified prefix.
|
java.util.List |
getList(java.lang.String key)
Get a List of strings associated with the given configuration key.
|
java.util.List |
getList(java.lang.String key,
java.util.List defaultValue)
Get a List of strings associated with the given configuration key.
|
long |
getLong(java.lang.String key)
Get a long associated with the given configuration key.
|
long |
getLong(java.lang.String key,
long defaultValue)
Get a long associated with the given configuration key.
|
java.lang.Long |
getLong(java.lang.String key,
java.lang.Long defaultValue)
Get a long associated with the given configuration key.
|
java.util.Properties |
getProperties(java.lang.String key)
Get a list of properties associated with the given
configuration key.
|
java.util.Properties |
getProperties(java.lang.String key,
java.util.Properties defaults)
Get a list of properties associated with the given
configuration key.
|
java.lang.Object |
getProperty(java.lang.String key)
Gets a property from the configuration.
|
short |
getShort(java.lang.String key)
Get a short associated with the given configuration key.
|
short |
getShort(java.lang.String key,
short defaultValue)
Get a short associated with the given configuration key.
|
java.lang.Short |
getShort(java.lang.String key,
java.lang.Short defaultValue)
Get a short associated with the given configuration key.
|
java.lang.String |
getString(java.lang.String key)
Get a string associated with the given configuration key.
|
java.lang.String |
getString(java.lang.String key,
java.lang.String defaultValue)
Get a string associated with the given configuration key.
|
java.lang.String[] |
getStringArray(java.lang.String key)
Get an array of strings associated with the given configuration
key.
|
java.util.Vector |
getVector(java.lang.String key)
Get a Vector of strings associated with the given configuration
key.
|
java.util.Vector |
getVector(java.lang.String key,
java.util.Vector defaultValue)
Get a Vector of strings associated with the given configuration key.
|
boolean |
isInitialized()
Indicate to client code whether property
resources have been initialized or not.
|
void |
load(java.io.InputStream input)
Load the properties from the given input stream.
|
void |
load(java.io.InputStream input,
java.lang.String enc)
Load the properties from the given input stream
and using the specified encoding.
|
void |
save(java.io.OutputStream output,
java.lang.String header)
Save the properties to the given output stream.
|
void |
setInclude(java.lang.String inc)
Sets the property value for including other properties files.
|
void |
setProperty(java.lang.String key,
java.lang.Object value)
Set a property, this will replace any previously
set values.
|
ExtendedProperties |
subset(java.lang.String prefix)
Create an ExtendedProperties object that is a subset
of this one.
|
java.lang.String |
testBoolean(java.lang.String value)
Test whether the string represent by value maps to a boolean
value or not.
|
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, toString, values
public ExtendedProperties()
public ExtendedProperties(java.lang.String file) throws java.io.IOException
file
- the filename to loadjava.io.IOException
- if a file error occurspublic ExtendedProperties(java.lang.String file, java.lang.String defaultFile) throws java.io.IOException
file
- the filename to loaddefaultFile
- a second filename to load default values fromjava.io.IOException
- if a file error occurspublic boolean isInitialized()
public java.lang.String getInclude()
public void setInclude(java.lang.String inc)
inc
- A String.public void load(java.io.InputStream input) throws java.io.IOException
input
- the InputStream to load fromjava.io.IOException
- if an IO error occurspublic void load(java.io.InputStream input, java.lang.String enc) throws java.io.IOException
input
- the InputStream to load fromenc
- the encoding to usejava.io.IOException
- if an IO error occurspublic java.lang.Object getProperty(java.lang.String key)
key
- property to retrievepublic void addProperty(java.lang.String key, java.lang.Object value)
resource.loader = file
is already present in the configuration and you
addProperty("resource.loader", "classpath")
Then you will end up with a Vector like the
following:
["file", "classpath"]
key
- the key to addvalue
- the value to addpublic void setProperty(java.lang.String key, java.lang.Object value)
key
- the key to setvalue
- the value to setpublic void save(java.io.OutputStream output, java.lang.String header) throws java.io.IOException
The stream is not closed, but it is flushed.
output
- an OutputStream, may be nullheader
- a textual comment to act as a file headerjava.io.IOException
- if an IO error occurspublic void combine(ExtendedProperties props)
Warning: It will overwrite previous entries without warning.
props
- the properties to combinepublic void clearProperty(java.lang.String key)
key
- the property key to remove along with corresponding valuepublic java.util.Iterator getKeys()
public java.util.Iterator getKeys(java.lang.String prefix)
prefix
- the prefix to matchpublic ExtendedProperties subset(java.lang.String prefix)
prefix
- the prefix to get a subset forpublic void display()
public java.lang.String getString(java.lang.String key)
key
- The configuration key.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a String.public java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a String.public java.util.Properties getProperties(java.lang.String key)
key
- The configuration key.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a String/List.java.lang.IllegalArgumentException
- if one of the tokens is
malformed (does not contain an equals sign).public java.util.Properties getProperties(java.lang.String key, java.util.Properties defaults)
key
- The configuration key.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a String/List.java.lang.IllegalArgumentException
- if one of the tokens is
malformed (does not contain an equals sign).public java.lang.String[] getStringArray(java.lang.String key)
key
- The configuration key.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a String/List.public java.util.Vector getVector(java.lang.String key)
key
- The configuration key.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Vector.public java.util.Vector getVector(java.lang.String key, java.util.Vector defaultValue)
The list is a copy of the internal data of this object, and as such you may alter it freely.
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Vector.public java.util.List getList(java.lang.String key)
The list is a copy of the internal data of this object, and as such you may alter it freely.
key
- The configuration key.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a List.public java.util.List getList(java.lang.String key, java.util.List defaultValue)
The list is a copy of the internal data of this object, and as such you may alter it freely.
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a List.public boolean getBoolean(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Boolean.public boolean getBoolean(java.lang.String key, boolean defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Boolean.public java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Boolean.public java.lang.String testBoolean(java.lang.String value)
true
, on
,
and yes
for a true
boolean value, and
false
, off
, and no
for
false
boolean values. Case of value to test for
boolean status is ignored.value
- the value to test for boolean statetrue
or false
if the supplied
text maps to a boolean value, or null
otherwise.public byte getByte(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Byte.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public byte getByte(java.lang.String key, byte defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Byte.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public java.lang.Byte getByte(java.lang.String key, java.lang.Byte defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Byte.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public short getShort(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Short.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public short getShort(java.lang.String key, short defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Short.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public java.lang.Short getShort(java.lang.String key, java.lang.Short defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Short.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public int getInt(java.lang.String name)
name
- The resource name.public int getInt(java.lang.String name, int def)
name
- The resource namedef
- The default value of the resource.public int getInteger(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Integer.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public int getInteger(java.lang.String key, int defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Integer.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public java.lang.Integer getInteger(java.lang.String key, java.lang.Integer defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Integer.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public long getLong(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Long.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public long getLong(java.lang.String key, long defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Long.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public java.lang.Long getLong(java.lang.String key, java.lang.Long defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Long.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public float getFloat(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Float.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public float getFloat(java.lang.String key, float defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Float.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public java.lang.Float getFloat(java.lang.String key, java.lang.Float defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Float.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public double getDouble(java.lang.String key)
key
- The configuration key.java.util.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Double.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public double getDouble(java.lang.String key, double defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Double.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public java.lang.Double getDouble(java.lang.String key, java.lang.Double defaultValue)
key
- The configuration key.defaultValue
- The default value.java.lang.ClassCastException
- is thrown if the key maps to an
object that is not a Double.java.lang.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public static ExtendedProperties convertProperties(java.util.Properties props)
NOTE: From Commons Collections 3.2 this method will pick up any default parent Properties of the specified input object.
props
- the properties object to convert"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"