public class TypedProperties extends java.lang.Object implements Displayable
Properties
but has support for
reading and writing primitive types. Internally, this handles converting specific types to
a string representation that can be saved and read from files.
In addition to primitive types, arrays of those primitives are also supported using either the
default "," separator or a user-configurable string. Leading and trailing whitespace is
automatically trimmed from each entry. Be mindful that values saved in arrays should
not include the separator character(s) - no escaping is performed!
From version 3.0+, keys are case-insensitive.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_SEPARATOR
The default separator for arrays.
|
Constructor and Description |
---|
TypedProperties()
Creates a new, empty instance of this class using the default "," separator for arrays.
|
TypedProperties(java.util.Properties properties)
Decorates a
Properties object to provide type-safe access using
the default "," separator for arrays. |
TypedProperties(java.util.Properties properties,
java.lang.String separator)
Decorates a
Properties object using the specified separator for
arrays. |
TypedProperties(java.lang.String separator)
Creates a new, empty instance of this class using the given separator string for arrays.
|
Modifier and Type | Method and Description |
---|---|
void |
addAll(java.util.Properties properties)
Adds all properties from the specified properties object.
|
void |
addAll(TypedProperties properties)
Adds all properties from the specified properties object.
|
void |
clear()
Clears all properties.
|
void |
clearAccessedProperties()
Clears the tracking information for properties that have
been accessed.
|
boolean |
contains(java.lang.String key)
Returns
true if the specified key is contained in this
properties object; false otherwise. |
void |
display(java.io.PrintStream out)
Prints the properties to standard output.
|
boolean |
equals(java.lang.Object obj) |
java.util.Set<java.lang.String> |
getAccessedProperties()
Returns the properties that were accessed since the last call to
clearAccessedProperties() or clear() . |
boolean |
getBoolean(java.lang.String key)
Returns the value of the property as a
boolean , or throws an exception. |
boolean |
getBoolean(java.lang.String key,
boolean defaultValue)
Returns the value of the property with the specified name as a
boolean ; or defaultValue if no property with the
specified name exists. |
byte |
getByte(java.lang.String key)
Returns the value of the property as a
byte , or throws an exception. |
byte |
getByte(java.lang.String key,
byte defaultValue)
Returns the value of the property with the specified name as a
byte ; or defaultValue if no property with the specified
name exists. |
byte[] |
getByteArray(java.lang.String key)
Returns the value of the property as a
byte array, or throws an exception. |
byte[] |
getByteArray(java.lang.String key,
byte[] defaultValues)
Returns the value of the property with the specified name as a
byte array; or defaultValues if no property with the
specified name exists. |
double |
getDouble(java.lang.String key)
Returns the value of the property as a
double , or throws an exception. |
double |
getDouble(java.lang.String key,
double defaultValue)
Returns the value of the property with the specified name as a
double ; or defaultValue if no property with the specified
name exists. |
double[] |
getDoubleArray(java.lang.String key)
Returns the value of the property as a
double array, or throws an exception. |
double[] |
getDoubleArray(java.lang.String key,
double[] defaultValues)
Returns the value of the property with the specified name as a
double array; or defaultValues if no property with the
specified name exists. |
<T extends java.lang.Enum<?>> |
getEnum(java.lang.String key,
java.lang.Class<T> enumType)
Returns the value of the property with the specified name as an Enum.
|
<T extends java.lang.Enum<?>> |
getEnum(java.lang.String key,
java.lang.Class<T> enumType,
T defaultValue)
Returns the value of the property with the specified name as an Enum; or
defaultValue if no property with the specified name exists. |
float |
getFloat(java.lang.String key)
Returns the value of the property as a
float , or throws an exception. |
float |
getFloat(java.lang.String key,
float defaultValue)
Returns the value of the property with the specified name as a
float ; or defaultValue if no property with the specified
name exists. |
float[] |
getFloatArray(java.lang.String key)
Returns the value of the property as a
float array, or throws an exception. |
float[] |
getFloatArray(java.lang.String key,
float[] defaultValues)
Returns the value of the property with the specified name as a
float array; or defaultValues if no property with the
specified name exists. |
int |
getInt(java.lang.String key)
Returns the value of the property as a
int , or throws an exception. |
int |
getInt(java.lang.String key,
int defaultValue)
Returns the value of the property with the specified name as an
int ; or defaultValue if no property with the specified
name exists. |
int[] |
getIntArray(java.lang.String key)
Returns the value of the property as a
int array, or throws an exception. |
int[] |
getIntArray(java.lang.String key,
int[] defaultValues)
Returns the value of the property with the specified name as an
int array; or defaultValues if no property with the
specified name exists. |
long |
getLong(java.lang.String key)
Returns the value of the property as a
long , or throws an exception. |
long |
getLong(java.lang.String key,
long defaultValue)
Returns the value of the property with the specified name as a
long ; or defaultValue if no property with the specified
name exists. |
long[] |
getLongArray(java.lang.String key)
Returns the value of the property as a
long array, or throws an exception. |
long[] |
getLongArray(java.lang.String key,
long[] defaultValues)
Returns the value of the property with the specified name as a
long array; or defaultValues if no property with the
specified name exists. |
short |
getShort(java.lang.String key)
Returns the value of the property as a
short , or throws an exception. |
short |
getShort(java.lang.String key,
short defaultValue)
Returns the value of the property with the specified name as a
short ; or defaultValue if no property with the specified
name exists. |
short[] |
getShortArray(java.lang.String key)
Returns the value of the property as a
short array, or throws an exception. |
short[] |
getShortArray(java.lang.String key,
short[] defaultValues)
Returns the value of the property with the specified name as a
short array; or defaultValues if no property with the
specified name exists. |
java.lang.String |
getString(java.lang.String key)
Returns the value of the property as a string, or throws an exception.
|
java.lang.String |
getString(java.lang.String key,
java.lang.String defaultValue)
Returns the value of the property with the specified name as a string; or
defaultValue if no property with the specified name exists. |
java.lang.String[] |
getStringArray(java.lang.String key)
Returns the value of the property as a
String array, or throws an exception. |
java.lang.String[] |
getStringArray(java.lang.String key,
java.lang.String[] defaultValues)
Returns the value of the property with the specified name as a
String array; or defaultValues if no property with the
specified name exists. |
int |
getTruncatedInt(java.lang.String key)
Returns the value of the property as a
int , or throws an exception. |
int |
getTruncatedInt(java.lang.String key,
int defaultValue)
Returns the value of the property with the specified name as an
int ; or defaultValue
if no property with the specified name exists. |
long |
getTruncatedLong(java.lang.String key)
Returns the value of the property as a
long , or throws an exception. |
long |
getTruncatedLong(java.lang.String key,
long defaultValue)
Returns the value of the property with the specified name as a
long ; or defaultValue
if no property with the specified name exists. |
java.util.Set<java.lang.String> |
getUnaccessedProperties()
Returns the properties that were never accessed since the last call to
clearAccessedProperties() or clear() |
int |
hashCode() |
boolean |
isEmpty()
Returns
true if there are no properties set. |
void |
load(java.io.Reader reader)
Loads the properties from a reader.
|
static TypedProperties |
loadBuildProperties()
Loads the contents of
META-INF/build.properties and evaluates any
string substitutions in the form ${token} . |
void |
remove(java.lang.String key)
Removes the property with the specified name.
|
void |
setBoolean(java.lang.String key,
boolean value)
Sets the value of the property to the given
boolean . |
void |
setByte(java.lang.String key,
byte value)
Sets the value of the property to the given
byte . |
void |
setByteArray(java.lang.String key,
byte[] values)
Sets the value of the property with the specified name as a
byte array. |
void |
setDouble(java.lang.String key,
double value)
Sets the value of the property to the given
double . |
void |
setDoubleArray(java.lang.String key,
double[] values)
Sets the value of the property with the specified name as a
double array. |
<T extends java.lang.Enum<?>> |
setEnum(java.lang.String key,
T value)
Sets the value of the property to the given enum value.
|
void |
setFloat(java.lang.String key,
float value)
Sets the value of the property to the given
float . |
void |
setFloatArray(java.lang.String key,
float[] values)
Sets the value of the property with the specified name as a
float array. |
void |
setInt(java.lang.String key,
int value)
Sets the value of the property to the given
int . |
void |
setIntArray(java.lang.String key,
int[] values)
Sets the value of the property with the specified name as a
int array. |
void |
setLong(java.lang.String key,
long value)
Sets the value of the property to the given
long . |
void |
setLongArray(java.lang.String key,
long[] values)
Sets the value of the property with the specified name as a
long array. |
void |
setShort(java.lang.String key,
short value)
Sets the value of the property to the given
short . |
void |
setShortArray(java.lang.String key,
short[] values)
Sets the value of the property with the specified name as a
short array. |
void |
setString(java.lang.String key,
java.lang.String value)
Sets the value of the property to the given
String . |
void |
setStringArray(java.lang.String key,
java.lang.String[] values)
Sets the value of the property with the specified name as a
String array. |
int |
size()
Returns the number of properties that are defined.
|
void |
store(java.io.Writer writer)
Writes the properties to a writer.
|
void |
throwIfUnaccessedProperties()
Throws a
ConfigurationException if any properties were not accessed. |
void |
warnIfUnaccessedProperties()
Prints a warning if any properties were not accessed.
|
static TypedProperties |
withProperty(java.lang.String key,
java.lang.String value)
Convenience method to quickly construct a typed properties instance with
a single key-value pair.
|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
display
public static final java.lang.String DEFAULT_SEPARATOR
public TypedProperties()
public TypedProperties(java.lang.String separator)
separator
- the separator stringpublic TypedProperties(java.util.Properties properties)
Properties
object to provide type-safe access using
the default "," separator for arrays.properties
- the existing Properties
objectpublic TypedProperties(java.util.Properties properties, java.lang.String separator)
Properties
object using the specified separator for
arrays.properties
- the existing Properties
objectseparator
- the separator for arrayspublic static TypedProperties withProperty(java.lang.String key, java.lang.String value)
TypedProperties.withProperty("epsilon", commandLine.getOptionValue("epsilon")).getDoubleArray("epsilon");
key
- the keyvalue
- the value assigned to the keypublic static TypedProperties loadBuildProperties() throws java.io.IOException
META-INF/build.properties
and evaluates any
string substitutions in the form ${token}
.java.io.IOException
- if an error occurred loading the filepublic boolean contains(java.lang.String key)
true
if the specified key is contained in this
properties object; false
otherwise.key
- the property nametrue
if the specified key is contained in this
properties object; false
otherwisepublic java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
defaultValue
if no property with the specified name exists.key
- the property namedefaultValue
- the default valuedefaultValue
if no property with the specified name
existspublic java.lang.String getString(java.lang.String key)
contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic double getDouble(java.lang.String key, double defaultValue)
double
; or defaultValue
if no property with the specified
name exists.key
- the property namedefaultValue
- the default valuedouble
; or defaultValue
if no property with the
specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable
double
public double getDouble(java.lang.String key)
double
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic float getFloat(java.lang.String key, float defaultValue)
float
; or defaultValue
if no property with the specified
name exists.key
- the property namedefaultValue
- the default valuefloat
; or defaultValue
if no property with the
specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable
float
public float getFloat(java.lang.String key)
float
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic long getLong(java.lang.String key, long defaultValue)
long
; or defaultValue
if no property with the specified
name exists.key
- the property namedefaultValue
- the default valuelong
; or defaultValue
if no property with the
specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable
long
public long getLong(java.lang.String key)
long
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic int getInt(java.lang.String key, int defaultValue)
int
; or defaultValue
if no property with the specified
name exists.key
- the property namedefaultValue
- the default valueint
; or defaultValue
if no property with the
specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable
integerpublic int getInt(java.lang.String key)
int
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic int getTruncatedInt(java.lang.String key, int defaultValue)
int
; or defaultValue
if no property with the specified name exists. Any decimal places will be truncated.key
- the property namedefaultValue
- the default valueint
; or defaultValue
if no property with the specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable integerpublic int getTruncatedInt(java.lang.String key)
int
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists. Any decimal places will be truncated.key
- the property namePropertyNotFoundException
- if the property was not foundpublic long getTruncatedLong(java.lang.String key, long defaultValue)
long
; or defaultValue
if no property with the specified name exists. Any decimal places will be truncated.key
- the property namedefaultValue
- the default valuelong
; or defaultValue
if no property with the specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable longpublic long getTruncatedLong(java.lang.String key)
long
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists. Any decimal places will be truncated.key
- the property namePropertyNotFoundException
- if the property was not foundpublic short getShort(java.lang.String key, short defaultValue)
short
; or defaultValue
if no property with the specified
name exists.key
- the property namedefaultValue
- the default valueshort
; or defaultValue
if no property with the
specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable
short
public short getShort(java.lang.String key)
short
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic byte getByte(java.lang.String key, byte defaultValue)
byte
; or defaultValue
if no property with the specified
name exists.key
- the property namedefaultValue
- the default valuebyte
; or defaultValue
if no property with the
specified name existsjava.lang.NumberFormatException
- if the property value is not a parseable
byte
public byte getByte(java.lang.String key)
byte
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic boolean getBoolean(java.lang.String key, boolean defaultValue)
boolean
; or defaultValue
if no property with the
specified name exists.key
- the property namedefaultValue
- the default valueboolean
; or defaultValue
if no property with the
specified name existspublic boolean getBoolean(java.lang.String key)
boolean
, or throws an exception. One should either
use the variant that takes a default value or check contains(String)
to ensure the
property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic <T extends java.lang.Enum<?>> T getEnum(java.lang.String key, java.lang.Class<T> enumType)
T
- the Enum typekey
- the property nameenumType
- the Enum classpublic <T extends java.lang.Enum<?>> T getEnum(java.lang.String key, java.lang.Class<T> enumType, T defaultValue)
defaultValue
if no property with the specified name exists. Unlike using
Enum.valueOf(Class, String)
, this version is case-insensitive.T
- the Enum typekey
- the property nameenumType
- the Enum classdefaultValue
- the default valuepublic java.lang.String[] getStringArray(java.lang.String key, java.lang.String[] defaultValues)
String
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valuesString
array; or defaultValues
if no property
with the specified name existspublic java.lang.String[] getStringArray(java.lang.String key)
String
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic double[] getDoubleArray(java.lang.String key, double[] defaultValues)
double
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valuesdouble
array; or defaultValues
if no property
with the specified name existspublic double[] getDoubleArray(java.lang.String key)
double
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic float[] getFloatArray(java.lang.String key, float[] defaultValues)
float
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valuesfloat
array; or defaultValues
if no property with
the specified name existspublic float[] getFloatArray(java.lang.String key)
float
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic long[] getLongArray(java.lang.String key, long[] defaultValues)
long
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valueslong
array; or defaultValues
if no property with
the specified name existspublic long[] getLongArray(java.lang.String key)
long
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic int[] getIntArray(java.lang.String key, int[] defaultValues)
int
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valuesint
array; or defaultValues
if no property with
the specified name existspublic int[] getIntArray(java.lang.String key)
int
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic short[] getShortArray(java.lang.String key, short[] defaultValues)
short
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valuesshort
array; or defaultValues
if no property with
the specified name existspublic short[] getShortArray(java.lang.String key)
short
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic byte[] getByteArray(java.lang.String key, byte[] defaultValues)
byte
array; or defaultValues
if no property with the
specified name exists.key
- the property namedefaultValues
- the default valuesbyte
array; or defaultValues
if no property with
the specified name existspublic byte[] getByteArray(java.lang.String key)
byte
array, or throws an exception.
One should either use the variant that takes a default value or check contains(String)
to ensure the property exists.key
- the property namePropertyNotFoundException
- if the property was not foundpublic void setString(java.lang.String key, java.lang.String value)
String
.key
- the property namevalue
- the property valuepublic void setFloat(java.lang.String key, float value)
float
.key
- the property namevalue
- the property valuepublic void setDouble(java.lang.String key, double value)
double
.key
- the property namevalue
- the property valuepublic void setByte(java.lang.String key, byte value)
byte
.key
- the property namevalue
- the property valuepublic void setShort(java.lang.String key, short value)
short
.key
- the property namevalue
- the property valuepublic void setInt(java.lang.String key, int value)
int
.key
- the property namevalue
- the property valuepublic void setLong(java.lang.String key, long value)
long
.key
- the property namevalue
- the property valuepublic void setBoolean(java.lang.String key, boolean value)
boolean
.key
- the property namevalue
- the property valuepublic <T extends java.lang.Enum<?>> void setEnum(java.lang.String key, T value)
T
- the type of the enumkey
- the property namevalue
- the property valuepublic void setStringArray(java.lang.String key, java.lang.String[] values)
String
array.key
- the property namevalues
- the property valuepublic void setFloatArray(java.lang.String key, float[] values)
float
array.key
- the property namevalues
- the property valuepublic void setDoubleArray(java.lang.String key, double[] values)
double
array.key
- the property namevalues
- the property valuepublic void setByteArray(java.lang.String key, byte[] values)
byte
array.key
- the property namevalues
- the property valuepublic void setShortArray(java.lang.String key, short[] values)
short
array.key
- the property namevalues
- the property valuepublic void setIntArray(java.lang.String key, int[] values)
int
array.key
- the property namevalues
- the property valuepublic void setLongArray(java.lang.String key, long[] values)
long
array.key
- the property namevalues
- the property valuepublic void clear()
public void remove(java.lang.String key)
key
- the property namepublic void addAll(java.util.Properties properties)
properties
- the propertiespublic void addAll(TypedProperties properties)
properties
- the propertiespublic int size()
public boolean isEmpty()
true
if there are no properties set.true
if no properties are set; false
otherwisepublic int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public void load(java.io.Reader reader) throws java.io.IOException
reader
- the readerjava.io.IOException
- if an I/O error occurredpublic void store(java.io.Writer writer) throws java.io.IOException
writer
- the writerjava.io.IOException
- if an I/O error occurredpublic void display(java.io.PrintStream out)
display
in interface Displayable
out
- the output streampublic void clearAccessedProperties()
public java.util.Set<java.lang.String> getAccessedProperties()
clearAccessedProperties()
or clear()
.public java.util.Set<java.lang.String> getUnaccessedProperties()
clearAccessedProperties()
or clear()
public void warnIfUnaccessedProperties()
TypedProperties properties = new TypedProperties(); ... write properties ... properties.clearAccessedProperties(); ... read properties ... properties.warnIfUnaccessedProperties();
public void throwIfUnaccessedProperties()
ConfigurationException
if any properties were not accessed.Copyright 2009-2024 David Hadka and other contributors. All rights reserved.
Licensed under the GNU Lesser General Public License.
Return to the MOEA Framework homepage.