public class Properties extends ObservableMap<String,List<String>> implements Serializable
This class is a enhanced version of Properties
that enforces the use of String keys and values internally, but offers many
convenience methods for storing and retrieving multiple values of different
types (e.g. Integer, Locale, File, etc). You can also see it as a
string-based multi-value map with helpful methods. While it does not extend
Properties
, it offers similar load and store
methods and can be used as a replacement for it in many cases.
As of 1.4, this class no longer extends TreeMap
.
It now extends ObservableMap
which means you can listen
for map changes.
To insert values, there are set methods and add methods.
The set methods will replace any value(s) already present under the
given key. It is essentially the same behavior as
Map.put(Object, Object)
. The add method will add the
new value(s) to the list of already existing ones (if any).
Upon encountering a problem in parsing the
data to its target format, a PropertiesException
is thrown.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_MULTIVALUE_DELIMITER
Default delimiter when storing/loading multi-values to/from
.properties files. |
Constructor and Description |
---|
Properties()
Create a new instance with case-sensitive keys.
|
Properties(boolean caseInsensitiveKeys)
Creates a new instance.
|
Properties(Map<String,List<String>> map)
Decorates
Map as a Properties . |
Properties(Map<String,List<String>> map,
boolean caseInsensitiveKeys)
Decorates a
Map argument as a Properties . |
Modifier and Type | Method and Description |
---|---|
void |
addBigDecimal(String key,
BigDecimal... values)
Add one or multiple BigDecimal values.
|
void |
addBoolean(String key,
boolean... values)
Adds one or multiple boolean values.
|
void |
addClass(String key,
Class<?>... values)
Adds one or multiple class values.
|
void |
addDate(String key,
Date... values)
Add one or multiple date values.
|
void |
addDouble(String key,
double... values)
Adds one or multiple double values.
|
void |
addFile(String key,
File... values)
Adds one or multiple file values.
|
void |
addFloat(String key,
float... values)
Adds one or multiple long values.
|
void |
addInt(String key,
int... values)
Adds one or multiple integer values values.
|
void |
addLocale(String key,
Locale... values)
Adds one or multiple locale values.
|
void |
addLong(String key,
long... values)
Add one or multiple long values.
|
void |
addString(String key,
String... values)
Adds one or multiple string values.
|
List<String> |
get(Object key) |
BigDecimal |
getBigDecimal(String key)
Gets value as a BigDecimal.
|
BigDecimal |
getBigDecimal(String key,
BigDecimal defaultValue)
Gets value as a BigDecimal.
|
List<BigDecimal> |
getBigDecimals(String key)
Gets values as a list of BigDecimals.
|
boolean |
getBoolean(String key)
Gets value as a boolean.
|
boolean |
getBoolean(String key,
boolean defaultValue)
Gets value as a boolean.
|
List<Boolean> |
getBooleans(String key)
Gets values as a list of booleans.
|
Class<?> |
getClass(String key)
Gets a class, assuming key value is a fully qualified class name
available in the classloader.
|
Class<?> |
getClass(String key,
Class<?> defaultValue)
Gets a class, assuming key value is a fully qualified class name
available in the classloader.
|
List<Class<?>> |
getClasses(String key)
Gets values as a list of initialized classes.
|
Date |
getDate(String key)
Gets value as a date.
|
Date |
getDate(String key,
Date defaultValue)
Gets value as a date.
|
List<Date> |
getDates(String key)
Gets values as a list of dates.
|
double |
getDouble(String key)
Gets value as a double.
|
double |
getDouble(String key,
double defaultValue)
Gets value as a double.
|
List<Double> |
getDoubles(String key)
Gets values as a list of doubles.
|
File |
getFile(String key)
Gets a file, assuming key value is a file system path.
|
File |
getFile(String key,
File defaultValue)
Gets a file, assuming key value is a file system path.
|
List<File> |
getFiles(String key)
Gets values as a list of files.
|
float |
getFloat(String key)
Gets value as a float.
|
float |
getFloat(String key,
float defaultValue)
Gets value as a float.
|
List<Float> |
getFloats(String key)
Gets values as a list of floats.
|
int |
getInt(String key)
Gets value as an integer.
|
int |
getInt(String key,
int defaultValue)
Gets value as an integer.
|
List<Integer> |
getInts(String key)
Gets values as a list of integers.
|
Locale |
getLocale(String key)
Gets value as a locale.
|
Locale |
getLocale(String key,
Locale defaultValue)
Gets value as a locale.
|
List<Locale> |
getLocales(String key)
Gets values as a list of locales.
|
long |
getLong(String key)
Gets value as a long.
|
long |
getLong(String key,
long defaultValue)
Gets value as a long.
|
List<Long> |
getLongs(String key)
Gets values as a list of longs.
|
String |
getMultiValueDelimiter()
Gets multiple value string delimiter.
|
String |
getString(String key)
Gets value as string.
|
String |
getString(String key,
String defaultValue)
Gets value as string.
|
List<String> |
getStrings(String key)
Gets values as a list of strings.
|
boolean |
isCaseInsensitiveKeys()
Gets whether keys are case sensitive or not.
|
boolean |
isCaseSensitiveKeys()
Deprecated.
Since 1.8.0, use
isCaseInsensitiveKeys() |
void |
load(InputStream inStream)
Reads a property list (key and element pairs) from the input
character stream (UTF-8) in a simple line-oriented format.
|
void |
load(InputStream inStream,
String delimiter)
Reads a property list (key and element pairs) from the input
character stream (UTF8) in a simple line-oriented format.
|
void |
load(Map<?,?> map)
Reads all key/value pairs in the given map, and
add them to this
Map . |
void |
load(Reader reader)
Reads a property list (key and element pairs) from the input
character stream in a simple line-oriented format.
|
void |
load(Reader reader,
String delimiter)
Reads a property list (key and element pairs) from the input
character stream in a simple line-oriented format.
|
void |
loadFromString(String str)
Reads a property list (key and element pairs) from the UTF-8 input
string.
|
void |
loadFromXML(InputStream in)
Loads all of the properties represented by the XML document on the
specified input stream into this instance.
|
void |
loadFromXML(InputStream in,
String delimiter)
Loads all of the properties represented by the XML document on the
specified input stream into this instance.
|
List<String> |
put(String key,
List<String> values) |
void |
putAll(Map<? extends String,? extends List<String>> m) |
List<String> |
remove(Object key) |
void |
setBigDecimal(String key,
BigDecimal... values)
Sets one or multiple BigDecimal values, replacing existing ones.
|
void |
setBoolean(String key,
boolean... values)
Sets one or multiple boolean values, replacing existing ones.
|
void |
setClass(String key,
Class<?>... values)
Sets one or multiple class values, replacing existing ones.
|
void |
setDate(String key,
Date... values)
Sets one or multiple date values, replacing existing ones.
|
void |
setDouble(String key,
double... values)
Sets one or multiple double values, replacing existing ones.
|
void |
setFile(String key,
File... values)
Sets one or multiple file values, replacing existing ones.
|
void |
setFloat(String key,
float... values)
Sets one or multiple float values, replacing existing ones.
|
void |
setInt(String key,
int... values)
Sets one or multiple integer values, replacing existing ones.
|
void |
setLocale(String key,
Locale... values)
Sets one or multiple locale values, replacing existing ones.
|
void |
setLong(String key,
long... values)
Sets one or multiple long values, replacing existing ones.
|
void |
setMultiValueDelimiter(String multiValueDelimiter)
Sets multiple value string delimiter.
|
void |
setString(String key,
String... values)
Sets one or multiple string values replacing existing ones.
|
void |
store(OutputStream out,
String comments)
Writes this
Map (key and element pairs) to the output character
stream in a format suitable for using the
load(InputStream) method. |
void |
store(OutputStream out,
String comments,
String delimiter)
Writes this
Map (key and element pairs) to the output character
stream as UTF-8 in a format suitable for using the
load(InputStream, String) method. |
void |
store(Writer writer,
String comments)
Writes this
Map (key and element pairs) to the output character
stream in a format suitable for using the
load(Reader) method. |
void |
store(Writer writer,
String comments,
String delimiter)
Writes this
Map (key and element pairs) to the output character
stream in a format suitable for using the
load(Reader, String) method. |
String |
storeToString(String comments)
Writes this property list (key and element pairs) in this
Properties table to the output stream as UTF-8 in a format
suitable for loading into a Properties table using the
load method. |
void |
storeToXML(OutputStream os,
String comment)
Emits an XML document representing all of the properties contained
in this
Map , using the specified encoding. |
void |
storeToXML(OutputStream os,
String comment,
String encoding)
Emits an XML document representing all of the properties contained
in this
Map , using the specified encoding. |
void |
storeToXML(OutputStream os,
String comment,
String encoding,
String delimiter)
Emits an XML document representing all of the properties contained
in this
Map , using the specified encoding. |
addMapChangeListener, clear, containsKey, containsValue, entrySet, isEmpty, keySet, removeMapChangeListener, size, toString, values
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
public static final String DEFAULT_MULTIVALUE_DELIMITER
.properties
files.public Properties()
HashMap
to store keys and values.public Properties(boolean caseInsensitiveKeys)
HashMap
to
store keys and values.caseInsensitiveKeys
- when true
methods taking a
key argument will consider the key being passed without
consideration for character case.public Properties(Map<String,List<String>> map)
Map
as a Properties
.
As of version 1.4 the Map
argument is decorated so that
modifications to this instance will also modify the supplied Map
.
To use a Map
to initialize values only, use the
load(Map)
method.map
- the Map to decoratepublic Properties(Map<String,List<String>> map, boolean caseInsensitiveKeys)
Map
argument as a Properties
.
As of version 1.4 the Map
argument is decorated so that
modifications to this instance will also modify the supplied Map
.
To use a Map
to initialize values only, use the
load(Map)
method.map
- the Map to decoratecaseInsensitiveKeys
- when true
methods taking a
key argument will consider the key being passed without
consideration for character case.@Deprecated public boolean isCaseSensitiveKeys()
isCaseInsensitiveKeys()
true
if case sensitivepublic boolean isCaseInsensitiveKeys()
true
if case insensitivepublic String getMultiValueDelimiter()
public void setMultiValueDelimiter(String multiValueDelimiter)
multiValueDelimiter
- multiple value string delimiterpublic String storeToString(String comments) throws IOException
Properties
table to the output stream as UTF-8 in a format
suitable for loading into a Properties
table using the
load
method.
Otherwise, the same considerations as
store(OutputStream, String)
apply.comments
- a description of the property list.IOException
- problem storing to stringpublic void store(Writer writer, String comments) throws IOException
Map
(key and element pairs) to the output character
stream in a format suitable for using the
load(Reader)
method.
If a key only has one value, then this method behavior is the
exact same as the store(Writer, String)
method.
Keys with multi-values are joined into a single string, using
the default delimiter:
DEFAULT_MULTIVALUE_DELIMITER
writer
- an output character stream writer.comments
- a description of the property list.IOException
- i/o problemstore(Writer, String)
public void store(Writer writer, String comments, String delimiter) throws IOException
Map
(key and element pairs) to the output character
stream in a format suitable for using the
load(Reader, String)
method.
If a key only has one value, then this method behavior is the
exact same as the store(Writer, String)
method.
Keys with multi-values are joined into a single string, using
the delimiter provided.writer
- an output character stream writer.comments
- a description of the property list.delimiter
- string to used as a separator when joining
multiple values for the same key.IOException
- i/o problemstore(Writer, String)
public void store(OutputStream out, String comments) throws IOException
Map
(key and element pairs) to the output character
stream in a format suitable for using the
load(InputStream)
method.
If a key only has one value, then this method behavior is the
exact same as the store(OutputStream, String)
method.
Keys with multi-values are joined into a single string, using
the default delimiter:
DEFAULT_MULTIVALUE_DELIMITER
out
- an output stream.comments
- a description of the property list.IOException
- i/o problemstore(OutputStream, String)
public void store(OutputStream out, String comments, String delimiter) throws IOException
Map
(key and element pairs) to the output character
stream as UTF-8 in a format suitable for using the
load(InputStream, String)
method.
If a key only has one value, then this method behavior is the
exact same as the store(OutputStream, String)
method.
Keys with multi-values are joined into a single string, using
the delimiter provided.out
- an output stream.comments
- a description of the property list.delimiter
- delimiter string to used as a separator when joining
multiple values for the same key.IOException
- i/o problemstore(OutputStream, String)
public void storeToXML(OutputStream os, String comment) throws IOException
Map
, using the specified encoding.
If a key only has one value, then this method behavior is the
exact same as the
storeToXML(OutputStream, String, String)
method,
where the character encoding is "UTF-8".
Keys with multi-values are joined into a single string, using
the default delimiter:
DEFAULT_MULTIVALUE_DELIMITER
os
- the output stream on which to emit the XML document.comment
- a description of the property list, or null
if no comment is desired.IOException
- i/o problemstoreToXML(OutputStream, String, String)
public void storeToXML(OutputStream os, String comment, String encoding) throws IOException
Map
, using the specified encoding.
If a key only has one value, then this method behavior is the
exact same as the
storeToXML(OutputStream, String, String)
method.
Keys with multi-values are joined into a single string, using
the default delimiter:
DEFAULT_MULTIVALUE_DELIMITER
os
- the output stream on which to emit the XML document.comment
- a description of the property list, or null
if no comment is desired.encoding
- character encodingIOException
- i/o problemstoreToXML(OutputStream, String, String)
public void storeToXML(OutputStream os, String comment, String encoding, String delimiter) throws IOException
Map
, using the specified encoding.
If a key only has one value, then this method behavior is the
exact same as the
storeToXML(OutputStream, String, String)
method.
Keys with multi-values are joined into a single string, using
the delimiter provided.os
- the output stream on which to emit the XML document.comment
- a description of the property list, or null
if no comment is desired.encoding
- character encodingdelimiter
- delimiter string to used as a separator when joining
multiple values for the same key.IOException
- i/o problemstoreToXML(OutputStream, String, String)
public void load(Reader reader) throws IOException
DEFAULT_MULTIVALUE_DELIMITER
If the key value was stored as a
single value, then this method behavior is the
exact same as the
load(Reader)
method.reader
- the input character stream.IOException
- i/o problemload(Reader)
public void load(Reader reader, String delimiter) throws IOException
load(Reader)
method.reader
- the input character stream.delimiter
- delimiter string to used to parse a multi value
key.IOException
- i/o problemload(Reader)
public void load(Map<?,?> map)
Reads all key/value pairs in the given map, and
add them to this Map
. Keys are converted to strings
using their toString() method, with exception
of values being arrays or collections. In such case, the entry
is considered a multi-value one and each value will be converted
to individual strings. null
keys are ignored.
null
values are converted to an empty string.
Changes to this instance
won't be reflected in the given Map
. If you want otherwise,
use invoke the constructor with a Map
argument.
map
- the map containing values to loadpublic void load(InputStream inStream) throws IOException
DEFAULT_MULTIVALUE_DELIMITER
If the key value was stored as a
single value, then this method behavior is the
exact same as the
load(InputStream)
method.inStream
- the input stream.IOException
- i/o problemload(InputStream)
public void load(InputStream inStream, String delimiter) throws IOException
load(InputStream)
method.inStream
- the input stream.delimiter
- delimiter string to used to parse a multi value
key.IOException
- i/o problemload(InputStream)
public void loadFromXML(InputStream in) throws IOException
DEFAULT_MULTIVALUE_DELIMITER
If the key value was stored as a
single value, then this method behavior is the
exact same as the
loadFromXML(InputStream)
method.in
- in the input stream from which to read the XML document.IOException
- i/o problempublic void loadFromXML(InputStream in, String delimiter) throws IOException
loadFromXML(InputStream)
method.in
- in the input stream from which to read the XML document.delimiter
- delimiter string to used to parse a multi value
key.IOException
- i/o problempublic void loadFromString(String str) throws IOException
load(InputStream)
apply.str
- the string to loadIOException
- problem loading stringpublic final String getString(String key)
key
- property keypublic final String getString(String key, String defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<String> getStrings(String key)
key
- property keypublic final void setString(String key, String... values)
null
value or an empty string array is
the same as calling remove(Object)
with the same key.
When setting multiple values, null
values are converted
to blank strings.key
- the key of the value to setvalues
- the values to setpublic final void addString(String key, String... values)
null
value has no effect.
When adding multiple values, null
values are converted
to blank strings.key
- the key of the value to setvalues
- the values to setpublic final int getInt(String key)
key
- property keypublic final int getInt(String key, int defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<Integer> getInts(String key)
key
- property keypublic final void setInt(String key, int... values)
key
- the key of the values to setvalues
- the values to setpublic final void addInt(String key, int... values)
key
- the key of the values to setvalues
- the values to setpublic final double getDouble(String key)
key
- property keypublic final double getDouble(String key, double defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<Double> getDoubles(String key)
key
- property keypublic final void setDouble(String key, double... values)
key
- the key of the values to setvalues
- the values to setpublic final void addDouble(String key, double... values)
key
- the key of the values to setvalues
- the values to setpublic final long getLong(String key)
key
- property keypublic final long getLong(String key, long defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<Long> getLongs(String key)
key
- property keypublic final void setLong(String key, long... values)
key
- the key of the values to setvalues
- the values to setpublic final void addLong(String key, long... values)
key
- the key of the values to setvalues
- the values to setpublic final float getFloat(String key)
key
- property keypublic final float getFloat(String key, float defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<Float> getFloats(String key)
key
- property keypublic final void setFloat(String key, float... values)
key
- the key of the values to setvalues
- the values to setpublic final void addFloat(String key, float... values)
key
- the key of the values to setvalues
- the values to setpublic final BigDecimal getBigDecimal(String key)
key
- property keypublic final BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<BigDecimal> getBigDecimals(String key)
key
- property keypublic final void setBigDecimal(String key, BigDecimal... values)
key
- the key of the values to setvalues
- the values to setpublic final void addBigDecimal(String key, BigDecimal... values)
key
- the key of the values to setvalues
- the values to setpublic final Date getDate(String key)
key
- property keypublic final Date getDate(String key, Date defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<Date> getDates(String key)
key
- property keypublic final void setDate(String key, Date... values)
key
- the key of the values to setvalues
- the values to setpublic final void addDate(String key, Date... values)
key
- the key of the values to setvalues
- the values to setpublic final boolean getBoolean(String key)
true
. Any other value (including null
)
will return false
.key
- property keypublic final boolean getBoolean(String key, boolean defaultValue)
true
. Any other value (including null
)
will return false
. If there are no entries for the given
key, the default value is returned instead.key
- property keydefaultValue
- default value to return when original value is null.public final List<Boolean> getBooleans(String key)
key
- property keypublic final void setBoolean(String key, boolean... values)
key
- the key of the values to setvalues
- the values to setpublic final void addBoolean(String key, boolean... values)
key
- the key of the values to setvalues
- the values to setpublic final Locale getLocale(String key)
key
- property keypublic final Locale getLocale(String key, Locale defaultValue)
key
- property keydefaultValue
- default value to return when original value is null.public final List<Locale> getLocales(String key)
key
- property keypublic final void setLocale(String key, Locale... values)
key
- the key of the values to setvalues
- the values to setpublic final void addLocale(String key, Locale... values)
key
- the key of the values to setvalues
- the values to setpublic final File getFile(String key)
key
- properties keypublic final File getFile(String key, File defaultValue)
key
- properties keydefaultValue
- default file being returned if no file has been
defined for the given key in the properties.public final List<File> getFiles(String key)
key
- property keypublic final void setFile(String key, File... values)
key
- the key of the values to setvalues
- the values to setpublic final void addFile(String key, File... values)
key
- the key of the values to setvalues
- the values to setpublic final Class<?> getClass(String key)
key
- properties keypublic final Class<?> getClass(String key, Class<?> defaultValue)
key
- properties keydefaultValue
- default file being returned if no class has been
defined for the given key in the properties.public final List<Class<?>> getClasses(String key)
key
- property keypublic final void setClass(String key, Class<?>... values)
key
- the key of the values to setvalues
- the values to setpublic final void addClass(String key, Class<?>... values)
key
- the key of the values to setvalues
- the values to setCopyright © 2008–2016 Norconex Inc.. All rights reserved.