Also please follow coding guidelines when sending patches.  (Braces)

john mcnally

Age Mooy wrote:
> 
> Hi,
> 
> I was trying to store query results in the global cache service using a key based on 
>the hashcode of the
> Criteria object and I found out that hashcodes for equal Criteria are not equal. 
>This breaks the rules because
> the Java spec says something like this:
> 
> "If two objects are equal according to the equals(Object) method, then calling the 
>hashCode method on each of
> the two objects must produce the same integer result"
> 
> This problem occurs because the Criteria.Criterion inner class defines an equals() 
>method without defining a
> matching hashCode() method.
> 
> I played around a bit and came up with this implementation:
> 
> /**
>  * Returns a hash code value for the object.
>  */
> public int hashCode()
> {
>     int h = value.hashCode() + comparison.hashCode();
> 
>     if (table != null)
>       h += table.hashCode();
> 
>     if (column != null)
>       h += column.hashCode();
> 
>     if (and != null)
>       h += and.hashCode();
> 
>     if (or != null)
>       h += or.hashCode();
> 
>     return h;
> }
> 
> I'm no hashCode expert but it works consistently with my tests.
> 
> The patch for the above code (in jakarta-turbine-2) is attached below,
> Age
> 
> Index: src/java/org/apache/turbine/util/db/Criteria.java
> ===================================================================
> RCS file: 
>/home/cvspublic/jakarta-turbine-2/src/java/org/apache/turbine/util/db/Criteria.java,v
> retrieving revision 1.1.1.1
> diff -u -r1.1.1.1 Criteria.java
> --- src/java/org/apache/turbine/util/db/Criteria.java   2001/08/16 05:09:46     
>1.1.1.1
> +++ src/java/org/apache/turbine/util/db/Criteria.java   2001/09/27 14:11:14
> @@ -3650,6 +3650,28 @@
>              return isEquiv;
>          }
> 
> +        /**
> +         * Returns a hash code value for the object.
> +         */
> +        public int hashCode()
> +        {
> +            int h = value.hashCode() + comparison.hashCode();
> +
> +            if (table != null)
> +              h += table.hashCode();
> +
> +            if (column != null)
> +              h += column.hashCode();
> +
> +            if (and != null)
> +              h += and.hashCode();
> +
> +            if (or != null)
> +              h += or.hashCode();
> +
> +            return h;
> +        }
> +
>          public String[] getAllTables()
>          {
>              StringStackBuffer tables = new StringStackBuffer();
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to