You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Leo Simons <le...@apache.org> on 2004/01/11 23:50:35 UTC

[framework] weirdness in Enum and ValuedEnum

Hi gang!

I set out to improve test coverage in avalon-framework. I've already 
found 3 bugs :D. I need some input on Enum and ValuedEnum. I've never 
used them (enumerations are a code smell :D), but they should be fixed 
nonetheless.

The Enum equals() method is marked final, which is weird to say the 
least. Weird, because ValuedEnum can't override it and hence what value 
is stored in the ValuedEnum doesn't matter for equality tests. See the 
testcases for more info. In particular, the following code *passes*:

                                                             // fragments
     private final static class Color extends ValuedEnum
     {
         public static final Color RED = new Color( "Red", 0 );
         public static final Color RED_NEGATIVE = new Color( "Red", -1 );

         /* ... */

     }

     private final static class OtherColor extends ValuedEnum
     {
         public static final OtherColor RED = new OtherColor( "Red", 0 );
         public static final OtherColor RED_NEGATIVE = new OtherColor(
           "Red", -1 );

         /* ... */
     }

         /* ... */

         assertTrue( Color.RED.equals( Color.RED_NEGATIVE ) );
         assertTrue( Color.RED_NEGATIVE.equals( Color.RED ) );
         assertTrue( OtherColor.RED.equals( OtherColor.RED_NEGATIVE ) );
         assertTrue( OtherColor.RED_NEGATIVE.equals( OtherColor.RED ) );

         assertTrue( Color.RED.hashCode() ==
           Color.RED_NEGATIVE.hashCode() );
         assertTrue( Color.RED_NEGATIVE.hashCode()
           == Color.RED.hashCode() );
         assertTrue( OtherColor.RED.hashCode()
           == OtherColor.RED_NEGATIVE.hashCode() );
         assertTrue( OtherColor.RED_NEGATIVE.hashCode()
           == OtherColor.RED.hashCode() );

is this really desired behaviour, or should I go ahead and override 
equals() and hashCode() to take the value into account? Does anyone have 
code anywhere that depends on this behaviour?

-- 
cheers,

- Leo Simons

-----------------------------------------------------------------------
Weblog              -- http://leosimons.com/
IoC Component Glue  -- http://jicarilla.org/
Articles & Opinions -- http://articles.leosimons.com/
-----------------------------------------------------------------------
"We started off trying to set up a small anarchist community, but
  people wouldn't obey the rules."
                                                         -- Alan Bennett



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


Re: [framework] weirdness in Enum and ValuedEnum

Posted by peter royal <pr...@apache.org>.
On Jan 11, 2004, at 5:50 PM, Leo Simons wrote:
> I set out to improve test coverage in avalon-framework. I've already 
> found 3 bugs :D. I need some input on Enum and ValuedEnum. I've never 
> used them (enumerations are a code smell :D), but they should be fixed 
> nonetheless.

I'm using them in my application's object model to model, well, 
enumerations :)

(such as fixed transaction types for modifying an inventory system, for 
example... there are uses!)

> is this really desired behaviour, or should I go ahead and override 
> equals() and hashCode() to take the value into account? Does anyone 
> have code anywhere that depends on this behaviour?

+1 to fix.
-pete


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