You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Niclas Hedhman <ni...@hedhman.org> on 2003/12/11 03:28:04 UTC

Re: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java


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, leosutic@apache.org 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: cvs-unsubscribe@avalon.apache.org
> For additional commands, e-mail: cvs-help@avalon.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org


Re: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java

Posted by Giacomo Pati <gi...@apache.org>.
You should read the first paragraph of this: 
http://java.sun.com/j2se/1.3/docs/api/java/util/HashMap.html

It's pretty clear.

Giacomo

Niclas Hedhman wrote:
> On Thursday 11 December 2003 10:28, Niclas Hedhman wrote:
> 
>>Verbosity Contest and the Winner is LEO S.! :o)
> 
> 
> Idiot of the Year Contest ... and the Winner is Niclas!
> 
> Sutic is one hell of a guy to pick up the subtle difference in Collection API.
> I would have bet my hand on that 
> 	HashMap m = new HashMap();
> 	m.put( key, null );
> 	return m.containsKey( key );
> 
> would return false...
> 
> My consolidation is that I am not alone in this believe.
> 
> 
> Niclas
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
> For additional commands, e-mail: dev-help@avalon.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org


Re: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java

Posted by Niclas Hedhman <ni...@hedhman.org>.
On Thursday 11 December 2003 10:28, Niclas Hedhman wrote:
> Verbosity Contest and the Winner is LEO S.! :o)

Idiot of the Year Contest ... and the Winner is Niclas!

Sutic is one hell of a guy to pick up the subtle difference in Collection API.
I would have bet my hand on that 
	HashMap m = new HashMap();
	m.put( key, null );
	return m.containsKey( key );

would return false...

My consolidation is that I am not alone in this believe.


Niclas


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org


Re: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java

Posted by Leo Simons <le...@apache.org>.
Leo Sutic wrote:
>>From: Niclas Hedhman [mailto:niclas@hedhman.org] 
>>
>>Verbosity Contest and the Winner is LEO S.! :o)
> 
> But which Leo S? Some more verbosity would have helped here, too... :)

we shall dub one "LSD" and the other "/LS", and here-after it shall be 
as clear as Pea?rls? forever more :D

> OK, here's a condensed version - using more shaky Java stuff than most
> people consider healthy:
> 
>     if( null != value )
>     {
>         (m_attributes = ( m_attributes == null ? new HashMap() :
> m_attributes )).put( name, value );
>     }
>     else if( null != m_attributes )
>     {
>         m_attributes.remove( name );
>     }
> 
> In this case, maintenance programmers may choke on it with an 
> OhGodNoException when they suspect there's a bug in there somewhere.

not wishing to repeat myself over and over and over again, I shall 
repeat myself: unit test. :P

back to my corner!

- LSD (the one and only)



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org


RE: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java

Posted by Niclas Hedhman <ni...@hedhman.org>.
Leo Sutic said:
>> From: Niclas Hedhman [mailto:niclas@hedhman.org]
>> Verbosity Contest and the Winner is LEO S.! :o)
> But which Leo S? Some more verbosity would have helped here,
> too... :)

Hmmmm.... Need to UnitTest that. I would never call Leo for Leo S.
;o)  Maybe for the drug he is emulating....

/LS
LSD

Ok.....

Cheers.
Niclas




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org


RE: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java

Posted by Leo Sutic <le...@inspireinfrastructure.com>.

> From: Niclas Hedhman [mailto:niclas@hedhman.org] 
> 
> Verbosity Contest and the Winner is LEO S.! :o)

But which Leo S? Some more verbosity would have helped here, too... :)

(The real bastard thing about the put(key,null) for attributes behavior
is
that the DefaultConfigurationSerializer will choke on it with a
NullPointerException.)

OK, here's a condensed version - using more shaky Java stuff than most
people consider healthy:

    if( null != value )
    {
        (m_attributes = ( m_attributes == null ? new HashMap() :
m_attributes )).put( name, value );
    }
    else if( null != m_attributes )
    {
        m_attributes.remove( name );
    }

In this case, maintenance programmers may choke on it with an 
OhGodNoException when they suspect there's a bug in there somewhere.

/LS



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org