Class Parameters
- All Implemented Interfaces:
Serializable
Parameters
class represents a set of key-value
pairs.
The Parameters
object provides a mechanism to obtain
values based on a String
name. There are convenience
methods that allow you to use defaults if the value does not exist,
as well as obtain the value in any of the same formats that are in
the Configuration
interface.
While there are similarities between the Parameters
object and the java.util.Properties object, there are some
important semantic differences. First, Parameters
are
read-only. Second, Parameters
are easily
derived from Configuration
objects. Lastly, the
Parameters
object is derived from XML fragments that
look like this:
<parameter name="param-name" value="param-value" />
Note: this class is not thread safe by default. If you require thread safety please synchronize write access to this class to prevent potential data corruption.
- Version:
- CVS $Revision: 1.41 $ $Date: 2004/02/11 14:34:25 $
- Author:
- Avalon Development Team
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
Checks is thisParameters
object is writeable.static Parameters
fromConfiguration
(Configuration configuration) Create aParameters
object from aConfiguration
object.static Parameters
fromConfiguration
(Configuration configuration, String elementName) Create aParameters
object from aConfiguration
object using the supplied element name.static Parameters
fromProperties
(Properties properties) Create aParameters
object from aProperties
object.String[]
getNames()
Retrieve an array of all parameter names.getParameter
(String name) Retrieve theString
value of the specified parameter.getParameter
(String name, String defaultValue) Retrieve theString
value of the specified parameter.boolean
getParameterAsBoolean
(String name) Retrieve theboolean
value of the specified parameter.boolean
getParameterAsBoolean
(String name, boolean defaultValue) Retrieve theboolean
value of the specified parameter.float
getParameterAsFloat
(String name) Retrieve thefloat
value of the specified parameter.float
getParameterAsFloat
(String name, float defaultValue) Retrieve thefloat
value of the specified parameter.int
getParameterAsInteger
(String name) Retrieve theint
value of the specified parameter.int
getParameterAsInteger
(String name, int defaultValue) Retrieve theint
value of the specified parameter.long
getParameterAsLong
(String name) Retrieve thelong
value of the specified parameter.long
getParameterAsLong
(String name, long defaultValue) Retrieve thelong
value of the specified parameter.Deprecated.Use getNames() insteadboolean
isParameter
(String name) Test if the specified parameter can be retrieved.void
Make this Parameters read-only so that it will throw aIllegalStateException
if someone tries to modify it.merge
(Parameters other) Merge parameters from anotherParameters
instance into this.void
removeParameter
(String name) Remove a parameter from the parameters objectsetParameter
(String name, String value) Set theString
value of a specified parameter.static Properties
toProperties
(Parameters params) Creates ajava.util.Properties
object from an Avalon Parameters object.
-
Field Details
-
EMPTY_PARAMETERS
Empty Parameters object- Since:
- 4.1.2
-
-
Constructor Details
-
Parameters
public Parameters()
-
-
Method Details
-
setParameter
Set theString
value of a specified parameter. If the specified value is null the parameter is removed.- Parameters:
name
- aString
valuevalue
- aString
value- Returns:
- The previous value of the parameter or null.
- Throws:
IllegalStateException
- if the Parameters object is read-only
-
removeParameter
Remove a parameter from the parameters object- Parameters:
name
- aString
value- Since:
- 4.1
-
getParameterNames
Deprecated.Use getNames() insteadReturn anIterator
view of all parameter names.- Returns:
- a iterator of parameter names
-
getNames
Retrieve an array of all parameter names.- Returns:
- the parameters names
-
isParameter
Test if the specified parameter can be retrieved.- Parameters:
name
- the parameter name- Returns:
- true if parameter is a name
-
getParameter
Retrieve theString
value of the specified parameter. If the specified parameter cannot be found, an exception is thrown.- Parameters:
name
- the name of parameter- Returns:
- the value of parameter
- Throws:
ParameterException
- if the specified parameter cannot be found
-
getParameter
Retrieve theString
value of the specified parameter. If the specified parameter cannot be found,defaultValue
is returned.- Parameters:
name
- the name of parameterdefaultValue
- the default value, returned if parameter does not exist or parameter's name is null- Returns:
- the value of parameter
-
getParameterAsInteger
Retrieve theint
value of the specified parameter. If the specified parameter cannot be found, an exception is thrown. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.- Parameters:
name
- the name of parameter- Returns:
- the integer parameter type
- Throws:
ParameterException
- if the specified parameter cannot be found or is not an Integer value
-
getParameterAsInteger
Retrieve theint
value of the specified parameter. If the specified parameter cannot be found,defaultValue
is returned. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.- Parameters:
name
- the name of parameterdefaultValue
- value returned if parameter does not exist or is of wrong type- Returns:
- the integer parameter type
-
getParameterAsLong
Retrieve thelong
value of the specified parameter. If the specified parameter cannot be found, an exception is thrown. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.- Parameters:
name
- the name of parameter- Returns:
- the long parameter type
- Throws:
ParameterException
- if the specified parameter cannot be found or is not a Long value.
-
getParameterAsLong
Retrieve thelong
value of the specified parameter. If the specified parameter cannot be found,defaultValue
is returned. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.- Parameters:
name
- the name of parameterdefaultValue
- value returned if parameter does not exist or is of wrong type- Returns:
- the long parameter type
-
getParameterAsFloat
Retrieve thefloat
value of the specified parameter. If the specified parameter cannot be found, an exception is thrown.- Parameters:
name
- the parameter name- Returns:
- the value
- Throws:
ParameterException
- if the specified parameter cannot be found or is not a Float value
-
getParameterAsFloat
Retrieve thefloat
value of the specified parameter. If the specified parameter cannot be found,defaultValue
is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value if parameter does not exist or is of wrong type- Returns:
- the value
-
getParameterAsBoolean
Retrieve theboolean
value of the specified parameter. If the specified parameter cannot be found, an exception is thrown.- Parameters:
name
- the parameter name- Returns:
- the value
- Throws:
ParameterException
- if an error occursParameterException
-
getParameterAsBoolean
Retrieve theboolean
value of the specified parameter. If the specified parameter cannot be found,defaultValue
is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value if parameter does not exist or is of wrong type- Returns:
- the value
-
merge
Merge parameters from anotherParameters
instance into this.- Parameters:
other
- the other Parameters- Returns:
- This
Parameters
instance.
-
makeReadOnly
public void makeReadOnly()Make this Parameters read-only so that it will throw aIllegalStateException
if someone tries to modify it. -
checkWriteable
Checks is thisParameters
object is writeable.- Throws:
IllegalStateException
- if thisParameters
object is read-only
-
fromConfiguration
public static Parameters fromConfiguration(Configuration configuration) throws ConfigurationException Create aParameters
object from aConfiguration
object. This acts exactly like the following method call:Parameters.fromConfiguration(configuration, "parameter");
- Parameters:
configuration
- the Configuration- Returns:
- This
Parameters
instance. - Throws:
ConfigurationException
- if an error occurs
-
fromConfiguration
public static Parameters fromConfiguration(Configuration configuration, String elementName) throws ConfigurationException Create aParameters
object from aConfiguration
object using the supplied element name.- Parameters:
configuration
- the ConfigurationelementName
- the element name for the parameters- Returns:
- This
Parameters
instance. - Throws:
ConfigurationException
- if an error occurs- Since:
- 4.1
-
fromProperties
Create aParameters
object from aProperties
object.- Parameters:
properties
- the Properties- Returns:
- This
Parameters
instance.
-
toProperties
Creates ajava.util.Properties
object from an Avalon Parameters object.- Parameters:
params
- aParameters
instance- Returns:
- a
Properties
instance
-