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.SerializableTheParametersclass represents a set of key-value pairs.The
Parametersobject provides a mechanism to obtain values based on aStringname. 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 theConfigurationinterface.While there are similarities between the
Parametersobject and the java.util.Properties object, there are some important semantic differences. First,Parametersare read-only. Second,Parametersare easily derived fromConfigurationobjects. Lastly, theParametersobject 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:
- $Id: Parameters.java 30977 2004-07-30 03:57:54 -0500 (Fri, 30 Jul 2004) niclas $
- Author:
- Avalon Development Team
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ParametersEMPTY_PARAMETERSEmpty Parameters objectprivate java.util.Mapm_parametersprivate booleanm_readOnly
-
Constructor Summary
Constructors Constructor Description Parameters()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidcheckWriteable()Checks is thisParametersobject is writeable.booleanequals(java.lang.Object other)Compare this parameters instance with the supplied object for equality.static ParametersfromConfiguration(Configuration configuration)Create aParametersobject from aConfigurationobject.static ParametersfromConfiguration(Configuration configuration, java.lang.String elementName)Create aParametersobject from aConfigurationobject using the supplied element name.static ParametersfromProperties(java.util.Properties properties)Create aParametersobject from aPropertiesobject.java.lang.String[]getNames()Retrieve an array of all parameter names.java.lang.StringgetParameter(java.lang.String name)Retrieve theStringvalue of the specified parameter.java.lang.StringgetParameter(java.lang.String name, java.lang.String defaultValue)Retrieve theStringvalue of the specified parameter.booleangetParameterAsBoolean(java.lang.String name)Retrieve thebooleanvalue of the specified parameter.booleangetParameterAsBoolean(java.lang.String name, boolean defaultValue)Retrieve thebooleanvalue of the specified parameter.floatgetParameterAsFloat(java.lang.String name)Retrieve thefloatvalue of the specified parameter.floatgetParameterAsFloat(java.lang.String name, float defaultValue)Retrieve thefloatvalue of the specified parameter.intgetParameterAsInteger(java.lang.String name)Retrieve theintvalue of the specified parameter.intgetParameterAsInteger(java.lang.String name, int defaultValue)Retrieve theintvalue of the specified parameter.longgetParameterAsLong(java.lang.String name)Retrieve thelongvalue of the specified parameter.longgetParameterAsLong(java.lang.String name, long defaultValue)Retrieve thelongvalue of the specified parameter.java.util.IteratorgetParameterNames()Deprecated.Use getNames() insteadinthashCode()Returns a hashed value of the Parameters instance.booleanisParameter(java.lang.String name)Test if the specified parameter can be retrieved.voidmakeReadOnly()Make this Parameters read-only so that it will throw aIllegalStateExceptionif someone tries to modify it.Parametersmerge(Parameters other)Merge parameters from anotherParametersinstance into this.private intparseInt(java.lang.String value)Parses string represenation of theintvalue.private longparseLong(java.lang.String value)Parses string represenation of thelongvalue.voidremoveParameter(java.lang.String name)Remove a parameter from the parameters objectjava.lang.StringsetParameter(java.lang.String name, java.lang.String value)Set theStringvalue of a specified parameter.static java.util.PropertiestoProperties(Parameters params)Creates ajava.util.Propertiesobject from an Avalon Parameters object.java.lang.StringtoString()
-
-
-
Field Detail
-
EMPTY_PARAMETERS
public static final Parameters EMPTY_PARAMETERS
Empty Parameters object- Since:
- 4.1.2
-
m_parameters
private java.util.Map m_parameters
-
m_readOnly
private boolean m_readOnly
-
-
Method Detail
-
setParameter
public java.lang.String setParameter(java.lang.String name, java.lang.String value) throws java.lang.IllegalStateExceptionSet theStringvalue of a specified parameter. If the specified value is null the parameter is removed.- Parameters:
name- aStringvaluevalue- aStringvalue- 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- aStringvalue- Since:
- 4.1
-
getParameterNames
public java.util.Iterator getParameterNames()
Deprecated.Use getNames() insteadReturn anIteratorview 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 ParameterExceptionRetrieve theStringvalue 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 theStringvalue of the specified parameter. If the specified parameter cannot be found,defaultValueis 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
-
parseInt
private int parseInt(java.lang.String value) throws java.lang.NumberFormatExceptionParses string represenation of theintvalue. 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:
value- the value to parse- Returns:
- the integer value
- Throws:
java.lang.NumberFormatException- if the specified value can not be parsed
-
getParameterAsInteger
public int getParameterAsInteger(java.lang.String name) throws ParameterExceptionRetrieve theintvalue 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 theintvalue of the specified parameter. If the specified parameter cannot be found,defaultValueis 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
-
parseLong
private long parseLong(java.lang.String value) throws java.lang.NumberFormatExceptionParses string represenation of thelongvalue. 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:
value- the value to parse- Returns:
- the long value
- Throws:
java.lang.NumberFormatException- if the specified value can not be parsed
-
getParameterAsLong
public long getParameterAsLong(java.lang.String name) throws ParameterExceptionRetrieve thelongvalue 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 thelongvalue of the specified parameter. If the specified parameter cannot be found,defaultValueis 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 ParameterExceptionRetrieve thefloatvalue 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 thefloatvalue of the specified parameter. If the specified parameter cannot be found,defaultValueis 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 ParameterExceptionRetrieve thebooleanvalue 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 thebooleanvalue of the specified parameter. If the specified parameter cannot be found,defaultValueis 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 anotherParametersinstance into this.- Parameters:
other- the other Parameters- Returns:
- This
Parametersinstance.
-
makeReadOnly
public void makeReadOnly()
Make this Parameters read-only so that it will throw aIllegalStateExceptionif someone tries to modify it.
-
equals
public boolean equals(java.lang.Object other)
Compare this parameters instance with the supplied object for equality. The equality is mainly driven by the underlying HashMap which forms the basis of the class. I.e. if the underlying HashMaps are equal and the Readonly attributes are equal in the Parameters instances being compared, then this method returns equality.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- the object to compare this parameters instance with- Returns:
- true if this parameters instance is equal to the supplied object
- Since:
- 4.3
-
hashCode
public int hashCode()
Returns a hashed value of the Parameters instance. This method returns a semi-unique value for all instances, yet an identical value for instances where equals() returns true.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hashed value of the instance
- Since:
- 4.3
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
checkWriteable
protected final void checkWriteable() throws java.lang.IllegalStateExceptionChecks is thisParametersobject is writeable.- Throws:
java.lang.IllegalStateException- if thisParametersobject is read-only
-
fromConfiguration
public static Parameters fromConfiguration(Configuration configuration) throws ConfigurationException
Create aParametersobject from aConfigurationobject. This acts exactly like the following method call:Parameters.fromConfiguration(configuration, "parameter");- Parameters:
configuration- the Configuration- Returns:
- This
Parametersinstance. - Throws:
ConfigurationException- if an error occurs
-
fromConfiguration
public static Parameters fromConfiguration(Configuration configuration, java.lang.String elementName) throws ConfigurationException
Create aParametersobject from aConfigurationobject using the supplied element name.- Parameters:
configuration- the ConfigurationelementName- the element name for the parameters- Returns:
- This
Parametersinstance. - Throws:
ConfigurationException- if an error occurs- Since:
- 4.1
-
fromProperties
public static Parameters fromProperties(java.util.Properties properties)
Create aParametersobject from aPropertiesobject.- Parameters:
properties- the Properties- Returns:
- This
Parametersinstance.
-
toProperties
public static java.util.Properties toProperties(Parameters params)
Creates ajava.util.Propertiesobject from an Avalon Parameters object.- Parameters:
params- aParametersinstance- Returns:
- a
Propertiesinstance
-
-