leosimons 2004/01/11 14:16:47
Modified: framework/api/src/java/org/apache/avalon/framework Enum.java
Log:
Bring equals() and hashCode() a little more in line with standard practice. This
will avoid ClassCastExceptions when comparing enums coming from different classloaders
(for which I won't add a testcase as that's a bit difficult to write).
Revision Changes Path
1.26 +26 -16
avalon/framework/api/src/java/org/apache/avalon/framework/Enum.java
Index: Enum.java
===================================================================
RCS file:
/home/cvs/avalon/framework/api/src/java/org/apache/avalon/framework/Enum.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- Enum.java 11 Jan 2004 21:58:28 -0000 1.25
+++ Enum.java 11 Jan 2004 22:16:47 -0000 1.26
@@ -151,26 +151,36 @@
/**
* 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.
+ * if they are of the same class and have the same names.
* The method is also declared final - I (LSutic) did this to
* allow the JIT to inline it easily.
*
- * @param other the other object
+ * @param o the other object
* @return the equality status
*/
- public final boolean equals( final Object other )
+ public final boolean equals( Object o )
{
- if( null == other )
- {
+ if( this == o )
+ return true;
+ if( !(o instanceof Enum) )
return false;
- }
- else
- {
- return other == this
- || ( other.getClass().getName().equals( this.getClass().getName() )
- && m_name.equals( ( (Enum)other ).m_name ) );
- }
+
+ final Enum enum = (Enum)o;
+
+ if( !getClass().equals( enum.getClass() ) )
+ return false;
+ if( m_name != null ? !m_name.equals( enum.m_name ) : enum.m_name != null )
+ return false;
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ int result;
+ result = (m_name != null ? m_name.hashCode() : 0);
+ result = 29 * result + getClass().hashCode();
+ return result;
}
/**
@@ -178,10 +188,10 @@
*
* @return a hash code value for this object
*/
- public int hashCode()
+ /*public int hashCode()
{
return m_name.hashCode() ^ this.getClass().getName().hashCode();
- }
+ }*/
/**
* Retrieve the name of this Enum item, set in the constructor.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]