Return-Path: Delivered-To: apmail-avalon-cvs-archive@www.apache.org Received: (qmail 92001 invoked from network); 11 Jan 2004 22:16:48 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 11 Jan 2004 22:16:48 -0000 Received: (qmail 26087 invoked by uid 500); 11 Jan 2004 22:16:35 -0000 Delivered-To: apmail-avalon-cvs-archive@avalon.apache.org Received: (qmail 26036 invoked by uid 500); 11 Jan 2004 22:16:35 -0000 Mailing-List: contact cvs-help@avalon.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list cvs@avalon.apache.org Received: (qmail 26021 invoked from network); 11 Jan 2004 22:16:35 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 11 Jan 2004 22:16:35 -0000 Received: (qmail 91975 invoked by uid 1291); 11 Jan 2004 22:16:47 -0000 Date: 11 Jan 2004 22:16:47 -0000 Message-ID: <20040111221647.91974.qmail@minotaur.apache.org> From: leosimons@apache.org To: avalon-cvs@apache.org Subject: cvs commit: avalon/framework/api/src/java/org/apache/avalon/framework Enum.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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: cvs-unsubscribe@avalon.apache.org For additional commands, e-mail: cvs-help@avalon.apache.org