Verbosity Contest and the Winner is LEO S.! :o)
Lot of code to do a simple thing;
public void setAttribute( final String name, final String value )
{
checkWriteable();
if( null != value )
{
if( null == m_attributes )
{
m_attributes = new HashMap();
}
m_attributes.put( name, value );
}
else
{
if( null != m_attributes )
{
m_attributes.remove( name );
}
}
}
can be written as;
public void setAttribute( final String name, final String value )
{
checkWriteable();
if( null == m_attributes )
m_attributes = new HashMap();
m_attributes.put( name, value );
}
m_attributes is a HashMap, and a value==null will remove it from the map;
Cheers,
Niclas
On Thursday 11 December 2003 02:05, [EMAIL PROTECTED] wrote:
> leosutic 2003/12/10 10:05:50
>
> Modified:
> framework/impl/src/test/org/apache/avalon/framework/configuration/test
> DefaultConfigurationTestCase.java
>
> framework/impl/src/java/org/apache/avalon/framework/configuration
> DefaultConfiguration.java
> Log:
> Added code to remove an attribute if its value was set to null.
>
> Revision Changes Path
> 1.10 +21 -0
> avalon/framework/impl/src/test/org/apache/avalon/framework/configuration/te
>st/DefaultConfigurationTestCase.java
>
> Index: DefaultConfigurationTestCase.java
> ===================================================================
> RCS file:
> /home/cvs/avalon/framework/impl/src/test/org/apache/avalon/framework/config
>uration/test/DefaultConfigurationTestCase.java,v retrieving revision 1.9
> retrieving revision 1.10
> diff -u -r1.9 -r1.10
> --- DefaultConfigurationTestCase.java 9 Dec 2003 15:59:31 -0000 1.9
> +++ DefaultConfigurationTestCase.java 10 Dec 2003 18:05:49 -0000 1.10
> @@ -53,6 +53,7 @@
> import junit.framework.TestCase;
>
> import org.apache.avalon.framework.configuration.Configuration;
> +import org.apache.avalon.framework.configuration.ConfigurationException;
> import org.apache.avalon.framework.configuration.ConfigurationUtil;
> import org.apache.avalon.framework.configuration.DefaultConfiguration;
>
> @@ -203,6 +204,26 @@
> assertEquals( "string", config.getAttribute("string") );
> assertEquals( true, config.getAttributeAsBoolean("boolean") );
> }
> +
> + public void testSetToNull() throws Exception
> + {
> + DefaultConfiguration config = new DefaultConfiguration( "root",
> "0:0", "http://root", "root" ); + config.setAttribute( "integer",
> "12" );
> + assertEquals( "12", config.getAttribute("integer") );
> +
> + config.setAttribute( "integer", null );
> + try
> + {
> + config.getAttribute("integer");
> + fail( "attribute 'integer' was present despite it being set
> to null" ); + }
> + catch( ConfigurationException e )
> + {
> + // OK, this is what we expect - the attribute wasn't found.
> + }
> + }
> +
> +
> }
>
>
>
>
>
> 1.38 +15 -5
> avalon/framework/impl/src/java/org/apache/avalon/framework/configuration/De
>faultConfiguration.java
>
> Index: DefaultConfiguration.java
> ===================================================================
> RCS file:
> /home/cvs/avalon/framework/impl/src/java/org/apache/avalon/framework/config
>uration/DefaultConfiguration.java,v retrieving revision 1.37
> retrieving revision 1.38
> diff -u -r1.37 -r1.38
> --- DefaultConfiguration.java 9 Dec 2003 15:59:31 -0000 1.37
> +++ DefaultConfiguration.java 10 Dec 2003 18:05:49 -0000 1.38
> @@ -445,11 +445,21 @@
> {
> checkWriteable();
>
> - if( null == m_attributes )
> + if( null != value )
> {
> - m_attributes = new HashMap();
> + if( null == m_attributes )
> + {
> + m_attributes = new HashMap();
> + }
> + m_attributes.put( name, value );
> + }
> + else
> + {
> + if( null != m_attributes )
> + {
> + m_attributes.remove( name );
> + }
> }
> - m_attributes.put( name, value );
> }
>
> /**
> @@ -495,7 +505,7 @@
> {
> setAttribute( name, String.valueOf( value ) );
> }
> -
> +
> /**
> * Add an attribute to this configuration element, returning its old
> * value or <b>null</b>.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]