Package org.moeaframework.util
Class TypedProperties
java.lang.Object
org.moeaframework.util.TypedProperties
- All Implemented Interfaces:
Displayable
Stores a collection of key-value pairs similar to
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.-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new, empty instance of this class.TypedProperties
(Properties properties) Creates a new typed properties instance initialized with the content of the properties. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAll
(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
Clears the tracking information for properties that have been accessed.boolean
Returnstrue
if the specified key is contained in this properties object;false
otherwise.Creates a new scope that allows making temporary changes to the properties.void
display
(PrintStream out) Prints the properties to standard output.boolean
Returns the properties that were accessed since the last call toclearAccessedProperties()
orclear()
.boolean
getBoolean
(String key) Returns the value of the property as aboolean
, or throws an exception.boolean
getBoolean
(String key, boolean defaultValue) Returns the value of the property with the specified name as aboolean
; ordefaultValue
if no property with the specified name exists.byte
Returns the value of the property as abyte
, or throws an exception.byte
Returns the value of the property with the specified name as abyte
; ordefaultValue
if no property with the specified name exists.byte[]
getByteArray
(String key) Returns the value of the property as abyte
array, or throws an exception.byte[]
getByteArray
(String key, byte[] defaultValues) Returns the value of the property with the specified name as abyte
array; ordefaultValues
if no property with the specified name exists.double
Returns the value of the property as adouble
, or throws an exception.double
Returns the value of the property with the specified name as adouble
; ordefaultValue
if no property with the specified name exists.double[]
getDoubleArray
(String key) Returns the value of the property as adouble
array, or throws an exception.double[]
getDoubleArray
(String key, double[] defaultValues) Returns the value of the property with the specified name as adouble
array; ordefaultValues
if no property with the specified name exists.<T extends Enum<?>>
TReturns the value of the property with the specified name as an Enum.<T extends Enum<?>>
TReturns the value of the property with the specified name as an Enum; ordefaultValue
if no property with the specified name exists.float
Returns the value of the property as afloat
, or throws an exception.float
Returns the value of the property with the specified name as afloat
; ordefaultValue
if no property with the specified name exists.float[]
getFloatArray
(String key) Returns the value of the property as afloat
array, or throws an exception.float[]
getFloatArray
(String key, float[] defaultValues) Returns the value of the property with the specified name as afloat
array; ordefaultValues
if no property with the specified name exists.int
Returns the value of the property as aint
, or throws an exception.int
Returns the value of the property with the specified name as anint
; ordefaultValue
if no property with the specified name exists.int[]
getIntArray
(String key) Returns the value of the property as aint
array, or throws an exception.int[]
getIntArray
(String key, int[] defaultValues) Returns the value of the property with the specified name as anint
array; ordefaultValues
if no property with the specified name exists.long
Returns the value of the property as along
, or throws an exception.long
Returns the value of the property with the specified name as along
; ordefaultValue
if no property with the specified name exists.long[]
getLongArray
(String key) Returns the value of the property as along
array, or throws an exception.long[]
getLongArray
(String key, long[] defaultValues) Returns the value of the property with the specified name as along
array; ordefaultValues
if no property with the specified name exists.short
Returns the value of the property as ashort
, or throws an exception.short
Returns the value of the property with the specified name as ashort
; ordefaultValue
if no property with the specified name exists.short[]
getShortArray
(String key) Returns the value of the property as ashort
array, or throws an exception.short[]
getShortArray
(String key, short[] defaultValues) Returns the value of the property with the specified name as ashort
array; ordefaultValues
if no property with the specified name exists.Returns the value of the property as a string, or throws an exception.Returns the value of the property with the specified name as a string; ordefaultValue
if no property with the specified name exists.String[]
getStringArray
(String key) Returns the value of the property as aString
array, or throws an exception.String[]
getStringArray
(String key, String[] defaultValues) Returns the value of the property with the specified name as aString
array; ordefaultValues
if no property with the specified name exists.int
getTruncatedInt
(String key) Returns the value of the property as aint
, or throws an exception.int
getTruncatedInt
(String key, int defaultValue) Returns the value of the property with the specified name as anint
; ordefaultValue
if no property with the specified name exists.long
getTruncatedLong
(String key) Returns the value of the property as along
, or throws an exception.long
getTruncatedLong
(String key, long defaultValue) Returns the value of the property with the specified name as along
; ordefaultValue
if no property with the specified name exists.Returns the properties that were never accessed since the last call toclearAccessedProperties()
orclear()
int
hashCode()
boolean
isEmpty()
Returnstrue
if there are no properties set.void
Loads the properties from a reader.static TypedProperties
Loads the contents ofMETA-INF/build.properties
and evaluates any string substitutions in the form${token}
.void
Removes the property with the specified name.void
setBoolean
(String key, boolean value) Sets the value of the property to the givenboolean
.void
Sets the value of the property to the givenbyte
.void
setByteArray
(String key, byte[] values) Sets the value of the property with the specified name as abyte
array.void
Sets the value of the property to the givendouble
.void
setDoubleArray
(String key, double[] values) Sets the value of the property with the specified name as adouble
array.<T extends Enum<?>>
voidSets the value of the property to the given enum value.void
Sets the value of the property to the givenfloat
.void
setFloatArray
(String key, float[] values) Sets the value of the property with the specified name as afloat
array.void
Sets the value of the property to the givenint
.void
setIntArray
(String key, int[] values) Sets the value of the property with the specified name as aint
array.void
Sets the value of the property to the givenlong
.void
setLongArray
(String key, long[] values) Sets the value of the property with the specified name as along
array.void
Sets the value of the property to the givenshort
.void
setShortArray
(String key, short[] values) Sets the value of the property with the specified name as ashort
array.void
Sets the value of the property to the givenString
.void
setStringArray
(String key, String[] values) Sets the value of the property with the specified name as aString
array.int
size()
Returns the number of properties that are defined.void
Writes the properties to a writer.void
Throws aConfigurationException
if any properties were not accessed.void
Prints a warning if any properties were not accessed.static TypedProperties
withProperty
(String key, String value) Convenience method to quickly construct a typed properties instance with a single key-value pair.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.moeaframework.util.format.Displayable
display
-
Field Details
-
DEFAULT_SEPARATOR
The default separator for arrays.- See Also:
-
-
Constructor Details
-
TypedProperties
public TypedProperties()Creates a new, empty instance of this class. -
TypedProperties
Creates a new typed properties instance initialized with the content of the properties.- Parameters:
properties
- the existingProperties
object
-
-
Method Details
-
withProperty
Convenience method to quickly construct a typed properties instance with a single key-value pair. This is particularly useful for parsing, for instance, command line arguments:TypedProperties.withProperty("epsilon", commandLine.getOptionValue("epsilon")).getDoubleArray("epsilon");
- Parameters:
key
- the keyvalue
- the value assigned to the key- Returns:
- a typed properties instance with the specified key-value pair
-
loadBuildProperties
Loads the contents ofMETA-INF/build.properties
and evaluates any string substitutions in the form${token}
.- Returns:
- the build properties
- Throws:
IOException
- if an error occurred loading the file
-
contains
Returnstrue
if the specified key is contained in this properties object;false
otherwise.- Parameters:
key
- the property name- Returns:
true
if the specified key is contained in this properties object;false
otherwise
-
getString
Returns the value of the property with the specified name as a string; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a string; or
defaultValue
if no property with the specified name exists
-
getString
Returns the value of the property as a string, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getDouble
Returns the value of the property with the specified name as adouble
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
double
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseabledouble
-
getDouble
Returns the value of the property as adouble
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getFloat
Returns the value of the property with the specified name as afloat
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
float
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseablefloat
-
getFloat
Returns the value of the property as afloat
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getLong
Returns the value of the property with the specified name as along
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
long
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseablelong
-
getLong
Returns the value of the property as along
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getInt
Returns the value of the property with the specified name as anint
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as an
int
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseable integer
-
getInt
Returns the value of the property as aint
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getTruncatedInt
Returns the value of the property with the specified name as anint
; ordefaultValue
if no property with the specified name exists. Any decimal places will be truncated.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as an
int
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseable integer
-
getTruncatedInt
Returns the value of the property as aint
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists. Any decimal places will be truncated.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getTruncatedLong
Returns the value of the property with the specified name as along
; ordefaultValue
if no property with the specified name exists. Any decimal places will be truncated.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
long
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseable long
-
getTruncatedLong
Returns the value of the property as along
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists. Any decimal places will be truncated.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getShort
Returns the value of the property with the specified name as ashort
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
short
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseableshort
-
getShort
Returns the value of the property as ashort
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getByte
Returns the value of the property with the specified name as abyte
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
byte
; ordefaultValue
if no property with the specified name exists - Throws:
NumberFormatException
- if the property value is not a parseablebyte
-
getByte
Returns the value of the property as abyte
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getBoolean
Returns the value of the property with the specified name as aboolean
; ordefaultValue
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValue
- the default value- Returns:
- the value of the property with the specified name as a
boolean
; ordefaultValue
if no property with the specified name exists
-
getBoolean
Returns the value of the property as aboolean
, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property
- Throws:
PropertyNotFoundException
- if the property was not found
-
getEnum
Returns the value of the property with the specified name as an Enum. If no such property is set, returns the default Enum value.- Type Parameters:
T
- the Enum type- Parameters:
key
- the property nameenumType
- the Enum class- Returns:
- the value of the property with the specified name as an Enum
-
getEnum
Returns the value of the property with the specified name as an Enum; ordefaultValue
if no property with the specified name exists. Unlike usingEnum.valueOf(Class, String)
, this version is case-insensitive.- Type Parameters:
T
- the Enum type- Parameters:
key
- the property nameenumType
- the Enum classdefaultValue
- the default value- Returns:
- the value of the property with the specified name as an Enum
-
getStringArray
Returns the value of the property with the specified name as aString
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as a
String
array; ordefaultValues
if no property with the specified name exists
-
getStringArray
Returns the value of the property as aString
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
getDoubleArray
Returns the value of the property with the specified name as adouble
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as a
double
array; ordefaultValues
if no property with the specified name exists
-
getDoubleArray
Returns the value of the property as adouble
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
getFloatArray
Returns the value of the property with the specified name as afloat
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as a
float
array; ordefaultValues
if no property with the specified name exists
-
getFloatArray
Returns the value of the property as afloat
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
getLongArray
Returns the value of the property with the specified name as along
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as a
long
array; ordefaultValues
if no property with the specified name exists
-
getLongArray
Returns the value of the property as along
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
getIntArray
Returns the value of the property with the specified name as anint
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as an
int
array; ordefaultValues
if no property with the specified name exists
-
getIntArray
Returns the value of the property as aint
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
getShortArray
Returns the value of the property with the specified name as ashort
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as a
short
array; ordefaultValues
if no property with the specified name exists
-
getShortArray
Returns the value of the property as ashort
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
getByteArray
Returns the value of the property with the specified name as abyte
array; ordefaultValues
if no property with the specified name exists.- Parameters:
key
- the property namedefaultValues
- the default values- Returns:
- the value of the property with the specified name as a
byte
array; ordefaultValues
if no property with the specified name exists
-
getByteArray
Returns the value of the property as abyte
array, or throws an exception. One should either use the variant that takes a default value or checkcontains(String)
to ensure the property exists.- Parameters:
key
- the property name- Returns:
- the value of the property as an array
- Throws:
PropertyNotFoundException
- if the property was not found
-
setString
Sets the value of the property to the givenString
.- Parameters:
key
- the property namevalue
- the property value
-
setFloat
Sets the value of the property to the givenfloat
.- Parameters:
key
- the property namevalue
- the property value
-
setDouble
Sets the value of the property to the givendouble
.- Parameters:
key
- the property namevalue
- the property value
-
setByte
Sets the value of the property to the givenbyte
.- Parameters:
key
- the property namevalue
- the property value
-
setShort
Sets the value of the property to the givenshort
.- Parameters:
key
- the property namevalue
- the property value
-
setInt
Sets the value of the property to the givenint
.- Parameters:
key
- the property namevalue
- the property value
-
setLong
Sets the value of the property to the givenlong
.- Parameters:
key
- the property namevalue
- the property value
-
setBoolean
Sets the value of the property to the givenboolean
.- Parameters:
key
- the property namevalue
- the property value
-
setEnum
Sets the value of the property to the given enum value.- Type Parameters:
T
- the type of the enum- Parameters:
key
- the property namevalue
- the property value
-
setStringArray
Sets the value of the property with the specified name as aString
array.- Parameters:
key
- the property namevalues
- the property value
-
setFloatArray
Sets the value of the property with the specified name as afloat
array.- Parameters:
key
- the property namevalues
- the property value
-
setDoubleArray
Sets the value of the property with the specified name as adouble
array.- Parameters:
key
- the property namevalues
- the property value
-
setByteArray
Sets the value of the property with the specified name as abyte
array.- Parameters:
key
- the property namevalues
- the property value
-
setShortArray
Sets the value of the property with the specified name as ashort
array.- Parameters:
key
- the property namevalues
- the property value
-
setIntArray
Sets the value of the property with the specified name as aint
array.- Parameters:
key
- the property namevalues
- the property value
-
setLongArray
Sets the value of the property with the specified name as along
array.- Parameters:
key
- the property namevalues
- the property value
-
clear
public void clear()Clears all properties. -
remove
Removes the property with the specified name.- Parameters:
key
- the property name
-
addAll
Adds all properties from the specified properties object.- Parameters:
properties
- the properties
-
addAll
Adds all properties from the specified properties object.- Parameters:
properties
- the properties
-
size
public int size()Returns the number of properties that are defined.- Returns:
- the number of properties
-
isEmpty
public boolean isEmpty()Returnstrue
if there are no properties set.- Returns:
true
if no properties are set;false
otherwise
-
hashCode
public int hashCode() -
equals
-
load
Loads the properties from a reader.- Parameters:
reader
- the reader- Throws:
IOException
- if an I/O error occurred
-
store
Writes the properties to a writer.- Parameters:
writer
- the writer- Throws:
IOException
- if an I/O error occurred
-
display
Prints the properties to standard output.- Specified by:
display
in interfaceDisplayable
- Parameters:
out
- the output stream
-
clearAccessedProperties
public void clearAccessedProperties()Clears the tracking information for properties that have been accessed. -
getAccessedProperties
Returns the properties that were accessed since the last call toclearAccessedProperties()
orclear()
.- Returns:
- the accessed properties
-
getUnaccessedProperties
Returns the properties that were never accessed since the last call toclearAccessedProperties()
orclear()
- Returns:
- the unaccessed or orphaned properties
-
warnIfUnaccessedProperties
public void warnIfUnaccessedProperties()Prints a warning if any properties were not accessed. For example:TypedProperties properties = new TypedProperties(); ... write properties ... properties.clearAccessedProperties(); ... read properties ... properties.warnIfUnaccessedProperties();
-
throwIfUnaccessedProperties
public void throwIfUnaccessedProperties()Throws aConfigurationException
if any properties were not accessed. -
createScope
Creates a new scope that allows making temporary changes to the properties. When the scope is closed, the original properties are restored. Typically, you should create scopes within try-with-resources blocks so they are automatically closed.- Returns:
- the scope
-