You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/04/25 03:00:17 UTC
cvs commit: jakarta-avalon/src/java/org/apache/avalon/framework/parameters Parameters.java
donaldp 02/04/24 18:00:16
Modified: src/java/org/apache/avalon/framework/parameters
Parameters.java
Log:
Optimized performance of parameter access by not throwing oodles of exceptions only to disgard them in the case where a defaul value was specified.
Submitted by: "Vadim Gritsenko" <va...@verizon.net>
Revision Changes Path
1.23 +123 -55 jakarta-avalon/src/java/org/apache/avalon/framework/parameters/Parameters.java
Index: Parameters.java
===================================================================
RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/parameters/Parameters.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- Parameters.java 21 Mar 2002 14:51:13 -0000 1.22
+++ Parameters.java 25 Apr 2002 01:00:16 -0000 1.23
@@ -157,8 +157,10 @@
throw new ParameterException( "The parameter '" + name +
"' does not contain a value" );
}
-
- return test;
+ else
+ {
+ return test;
+ }
}
/**
@@ -169,18 +171,57 @@
*
* @param name the name of parameter
* @param defaultValue the default value, returned if parameter does not exist
+ * or parameter's name is null
* @return the value of parameter
*/
public String getParameter( final String name, final String defaultValue )
{
- try
+ if( name == null )
{
- return getParameter( name );
+ return defaultValue;
}
- catch( final ParameterException pe )
+
+ final String test = (String)m_parameters.get( name );
+
+ if( test == null )
{
return defaultValue;
}
+ else
+ {
+ return test;
+ }
+ }
+
+ /**
+ * Parses string represenation of the <code>int</code> value.
+ * <p />
+ * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
+ * numbers begin with 0b, all other values are assumed to be decimal.
+ *
+ * @param value the value to parse
+ * @return the integer value
+ * @throws NumberFormatException if the specified value can not be parsed
+ */
+ private int parseInt ( final String value )
+ throws NumberFormatException
+ {
+ if( value.startsWith( "0x" ) )
+ {
+ return Integer.parseInt( value.substring(2), 16 );
+ }
+ else if( value.startsWith( "0o" ) )
+ {
+ return Integer.parseInt( value.substring(2), 8 );
+ }
+ else if( value.startsWith( "0b" ) )
+ {
+ return Integer.parseInt( value.substring(2), 2 );
+ }
+ else
+ {
+ return Integer.parseInt( value );
+ }
}
/**
@@ -194,32 +235,16 @@
* @param name the name of parameter
* @return the integer parameter type
* @throws ParameterException if the specified parameter cannot be found
+ * or is not an Integer value
*/
public int getParameterAsInteger( final String name )
throws ParameterException
{
try
{
- final String value = getParameter( name );
-
- if( value.startsWith( "0x" ) )
- {
- return Integer.parseInt( value.substring(2), 16 );
- }
- else if( value.startsWith( "0o" ) )
- {
- return Integer.parseInt( value.substring(2), 8 );
- }
- else if( value.startsWith( "0b" ) )
- {
- return Integer.parseInt( value.substring(2), 2 );
- }
- else
- {
- return Integer.parseInt( value );
- }
+ return parseInt( getParameter( name ) );
}
- catch( final Exception e )
+ catch( final NumberFormatException e )
{
throw new ParameterException( "Could not return an integer value", e );
}
@@ -242,15 +267,52 @@
{
try
{
- return getParameterAsInteger( name );
+ final String value = getParameter( name, null );
+ if ( value == null )
+ {
+ return defaultValue;
+ }
+
+ return parseInt( value );
}
- catch( final ParameterException pe )
+ catch( final NumberFormatException e )
{
return defaultValue;
}
}
/**
+ * Parses string represenation of the <code>long</code> value.
+ * <p />
+ * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
+ * numbers begin with 0b, all other values are assumed to be decimal.
+ *
+ * @param value the value to parse
+ * @return the long value
+ * @throws NumberFormatException if the specified value can not be parsed
+ */
+ private long parseLong ( final String value )
+ throws NumberFormatException
+ {
+ if( value.startsWith( "0x" ) )
+ {
+ return Long.parseLong( value.substring(2), 16 );
+ }
+ else if( value.startsWith( "0o" ) )
+ {
+ return Long.parseLong( value.substring(2), 8 );
+ }
+ else if( value.startsWith( "0b" ) )
+ {
+ return Long.parseLong( value.substring(2), 2 );
+ }
+ else
+ {
+ return Long.parseLong( value );
+ }
+ }
+
+ /**
* Retrieve the <code>long</code> value of the specified parameter.
* <p />
* If the specified parameter cannot be found, an exception is thrown.
@@ -261,32 +323,16 @@
* @param name the name of parameter
* @return the long parameter type
* @throws ParameterException if the specified parameter cannot be found
+ * or is not a Long value.
*/
public long getParameterAsLong( final String name )
throws ParameterException
{
try
{
- final String value = getParameter( name );
-
- if( value.startsWith( "0x" ) )
- {
- return Long.parseLong( value.substring(2), 16 );
- }
- else if( value.startsWith( "0o" ) )
- {
- return Long.parseLong( value.substring(2), 8 );
- }
- else if( value.startsWith( "0b" ) )
- {
- return Long.parseLong( value.substring(2), 2 );
- }
- else
- {
- return Long.parseLong( value );
- }
+ return parseLong( getParameter( name ) );
}
- catch( final Exception e )
+ catch( final NumberFormatException e )
{
throw new ParameterException( "Could not return a long value", e );
}
@@ -309,9 +355,15 @@
{
try
{
- return getParameterAsLong( name );
+ final String value = getParameter( name, null );
+ if( value == null )
+ {
+ return defaultValue;
+ }
+
+ return parseLong( value );
}
- catch( final ParameterException pe )
+ catch( final NumberFormatException e )
{
return defaultValue;
}
@@ -325,6 +377,7 @@
* @param name the parameter name
* @return the value
* @throws ParameterException if the specified parameter cannot be found
+ * or is not a Float value
*/
public float getParameterAsFloat( final String name )
throws ParameterException
@@ -333,7 +386,7 @@
{
return Float.parseFloat( getParameter( name ) );
}
- catch( final Exception e )
+ catch( final NumberFormatException e )
{
throw new ParameterException( "Could not return a float value", e );
}
@@ -353,9 +406,15 @@
{
try
{
- return getParameterAsFloat( name );
+ final String value = getParameter( name, null );
+ if( value == null )
+ {
+ return defaultValue;
+ }
+
+ return Float.parseFloat( value );
}
- catch( final ParameterException pe )
+ catch( final NumberFormatException pe )
{
return defaultValue;
}
@@ -369,7 +428,7 @@
* @param name the parameter name
* @return the value
* @exception ParameterException if an error occurs
- * @exception ParemterException
+ * @exception ParameterException
*/
public boolean getParameterAsBoolean( final String name )
throws ParameterException
@@ -402,11 +461,20 @@
*/
public boolean getParameterAsBoolean( final String name, final boolean defaultValue )
{
- try
+ final String value = getParameter( name, null );
+ if ( value == null ) {
+ return defaultValue;
+ }
+
+ if( value.equalsIgnoreCase( "true" ) )
+ {
+ return true;
+ }
+ else if( value.equalsIgnoreCase( "false" ) )
{
- return getParameterAsBoolean( name );
+ return false;
}
- catch( final ParameterException e )
+ else
{
return defaultValue;
}
@@ -427,7 +495,7 @@
while( names.hasNext() )
{
- final String name = (String) names.next();
+ final String name = (String)names.next();
String value = null;
try
{
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>