Class Parameters
- java.lang.Object
-
- org.apache.avalon.framework.parameters.Parameters
-
- All Implemented Interfaces:
java.io.Serializable
public class Parameters extends java.lang.Object implements java.io.Serializable
TheParameters
class represents a set of key-value pairs.The
Parameters
object provides a mechanism to obtain values based on aString
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 theConfiguration
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 fromConfiguration
objects. Lastly, theParameters
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.38 $ $Date: 2003/02/11 16:25:49 $
- Author:
- Avalon Development Team
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static Parameters
EMPTY_PARAMETERS
Empty Parameters object
-
Constructor Summary
Constructors Constructor Description Parameters()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
checkWriteable()
Checks is thisParameters
object is writeable.static Parameters
fromConfiguration(Configuration configuration)
Create aParameters
object from aConfiguration
object.static Parameters
fromConfiguration(Configuration configuration, java.lang.String elementName)
Create aParameters
object from aConfiguration
object using the supplied element name.static Parameters
fromProperties(java.util.Properties properties)
Create aParameters
object from aProperties
object.java.lang.String[]
getNames()
Retrieve an array of all parameter names.java.lang.String
getParameter(java.lang.String name)
Retrieve theString
value of the specified parameter.java.lang.String
getParameter(java.lang.String name, java.lang.String defaultValue)
Retrieve theString
value of the specified parameter.boolean
getParameterAsBoolean(java.lang.String name)
Retrieve theboolean
value of the specified parameter.boolean
getParameterAsBoolean(java.lang.String name, boolean defaultValue)
Retrieve theboolean
value of the specified parameter.float
getParameterAsFloat(java.lang.String name)
Retrieve thefloat
value of the specified parameter.float
getParameterAsFloat(java.lang.String name, float defaultValue)
Retrieve thefloat
value of the specified parameter.int
getParameterAsInteger(java.lang.String name)
Retrieve theint
value of the specified parameter.int
getParameterAsInteger(java.lang.String name, int defaultValue)
Retrieve theint
value of the specified parameter.long
getParameterAsLong(java.lang.String name)
Retrieve thelong
value of the specified parameter.long
getParameterAsLong(java.lang.String name, long defaultValue)
Retrieve thelong
value of the specified parameter.java.util.Iterator
getParameterNames()
Deprecated.Use getNames() insteadboolean
isParameter(java.lang.String name)
Test if the specified parameter can be retrieved.void
makeReadOnly()
Make this Parameters read-only so that it will throw aIllegalStateException
if someone tries to modify it.Parameters
merge(Parameters other)
Merge parameters from anotherParameters
instance into this.void
removeParameter(java.lang.String name)
Remove a parameter from the parameters objectjava.lang.String
setParameter(java.lang.String name, java.lang.String value)
Set theString
value of a specified parameter.static java.util.Properties
toProperties(Parameters params)
Creates ajava.util.Properties
object from an Avalon Parameters object.
-
-
-
Field Detail
-
EMPTY_PARAMETERS
public static final Parameters EMPTY_PARAMETERS
Empty Parameters object- Since:
- 4.1.2
-
-
Method Detail
-
setParameter
public java.lang.String setParameter(java.lang.String name, java.lang.String value) throws java.lang.IllegalStateException
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:
java.lang.IllegalStateException
- if the Parameters object is read-only
-
removeParameter
public void removeParameter(java.lang.String name)
Remove a parameter from the parameters object- Parameters:
name
- aString
value- Since:
- 4.1
-
getParameterNames
public java.util.Iterator getParameterNames()
Deprecated.Use getNames() insteadReturn anIterator
view of all parameter names.- Returns:
- a iterator of parameter names
-
getNames
public java.lang.String[] getNames()
Retrieve an array of all parameter names.- Returns:
- the parameters names
-
isParameter
public boolean isParameter(java.lang.String name)
Test if the specified parameter can be retrieved.- Parameters:
name
- the parameter name- Returns:
- true if parameter is a name
-
getParameter
public java.lang.String getParameter(java.lang.String name) throws ParameterException
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
public java.lang.String getParameter(java.lang.String name, java.lang.String defaultValue)
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
public int getParameterAsInteger(java.lang.String name) throws ParameterException
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
public int getParameterAsInteger(java.lang.String name, int defaultValue)
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
public long getParameterAsLong(java.lang.String name) throws ParameterException
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
public long getParameterAsLong(java.lang.String name, long defaultValue)
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
public float getParameterAsFloat(java.lang.String name) throws ParameterException
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
public float getParameterAsFloat(java.lang.String name, float defaultValue)
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
public boolean getParameterAsBoolean(java.lang.String name) throws ParameterException
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
public boolean getParameterAsBoolean(java.lang.String name, boolean defaultValue)
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
public Parameters merge(Parameters other)
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
protected final void checkWriteable() throws java.lang.IllegalStateException
Checks is thisParameters
object is writeable.- Throws:
java.lang.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, java.lang.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
public static Parameters fromProperties(java.util.Properties properties)
Create aParameters
object from aProperties
object.- Parameters:
properties
- the Properties- Returns:
- This
Parameters
instance.
-
toProperties
public static java.util.Properties toProperties(Parameters params)
Creates ajava.util.Properties
object from an Avalon Parameters object.- Parameters:
params
- aParameters
instance- Returns:
- a
Properties
instance
-
-