leosutic 2002/06/05 00:18:01 Modified: src/java/org/apache/avalon/framework Enum.java Added: src/test/org/apache/avalon/framework/test EnumTestCase.java Log: Added an equals() method to Enum. Revision Changes Path 1.1 jakarta-avalon/src/test/org/apache/avalon/framework/test/EnumTestCase.java Index: EnumTestCase.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.framework.test; import junit.framework.TestCase; import org.apache.avalon.framework.Enum; /** * TestCase for Enum. * * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a> */ public class EnumTestCase extends TestCase { private final static class Color extends Enum { public static final Color RED = new Color( "Red" ); public static final Color GREEN = new Color( "Green" ); public static final Color BLUE = new Color( "Blue" ); private Color( final String color ) { super( color ); } } private final static class OtherColor extends Enum { public static final OtherColor RED = new OtherColor( "Red" ); public static final OtherColor GREEN = new OtherColor( "Green" ); public static final OtherColor BLUE = new OtherColor( "Blue" ); private OtherColor( final String color ) { super( color ); } } public EnumTestCase( final String name ) { super( name ); } public void testEquality () { assertTrue( Color.RED.equals( Color.RED ) ); assertTrue( Color.GREEN.equals( Color.GREEN ) ); assertTrue( Color.BLUE.equals( Color.BLUE ) ); assertTrue( !OtherColor.RED.equals( Color.RED ) ); assertTrue( !OtherColor.GREEN.equals( Color.GREEN ) ); assertTrue( !OtherColor.BLUE.equals( Color.BLUE ) ); assertTrue( !Color.RED.equals( OtherColor.RED ) ); assertTrue( !Color.GREEN.equals( OtherColor.GREEN ) ); assertTrue( !Color.BLUE.equals( OtherColor.BLUE ) ); assertTrue( !Color.RED.equals( Color.GREEN ) ); assertTrue( !Color.GREEN.equals( Color.BLUE ) ); assertTrue( !Color.BLUE.equals( Color.RED ) ); } } 1.10 +21 -5 jakarta-avalon/src/java/org/apache/avalon/framework/Enum.java Index: Enum.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/Enum.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- Enum.java 24 May 2002 03:20:54 -0000 1.9 +++ Enum.java 5 Jun 2002 07:18:01 -0000 1.10 @@ -37,7 +37,7 @@ * public static final Color RED = new Color( "Red", map ); * public static final Color GREEN = new Color( "Green", map ); * public static final Color BLUE = new Color( "Blue", map ); - + * * private Color( final String color, final Map map ) * { * super( color, map ); @@ -60,6 +60,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a> * @version 1.0 */ public abstract class Enum @@ -68,7 +69,7 @@ * The string representation of the Enum. */ private final String m_name; - + /** * Constructor to add a new named item. * <p> @@ -82,7 +83,7 @@ { this( name, null ); } - + /** * Constructor to add a new named item. * <p> @@ -102,7 +103,22 @@ map.put( name, this ); } } - + + /** + * Tests for equality. Two Enum:s are considered equal + * if they have the same class names and the same names. + * Identity is tested for first, so this method runs fast. + * The method is also declared final - I (LSutic) did this to + * allow the JIT to inline it easily. + */ + public final boolean equals( final Object other ) + { + return other == this || + ( other.getClass().getName().equals( this.getClass().getName() ) && + m_name.equals( ((Enum) other).m_name ) ); + } + + /** * Retrieve the name of this Enum item, set in the constructor. * @return the name <code>String</code> of this Enum item @@ -111,7 +127,7 @@ { return m_name; } - + /** * Human readable description of this Enum item. For use when debugging. * @return String in the form <code>type[name]</code>, eg.:
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
