dev@jsr311.java.net

Re: JSR311: hashCode and equals mismatch for MediaType

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Mon, 09 Mar 2009 09:49:51 -0400

On Mar 6, 2009, at 7:54 PM, Bill Burke wrote:

> equals does not take MediaType parameters into account, while
> hashCode uses the parameters to create a map.
>
> 1) I think equals should compare parameters. IMO text/
> plain;charset=ISO != text/plain;charset=UTF-8
> 2) hashCode should use the same elements to create the hash as
> equals whether or not you agree with #1
>
Perhaps you are looking at an older version of the code, here's the
relevant line from equals:

return (this.type.equalsIgnoreCase(other.type)
         && this.subtype.equalsIgnoreCase(other.subtype)
         && this.parameters.equals(other.parameters));

As you can see, parameters are included in the check for equality. The
Javadoc also makes this clear, see:

https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/MediaType.html
#equals(java.lang.Object)

>
> If you agree with #1, the "q" parameter should be extracted from the
> parameter map of MediaType when it is created. When a "q" parameter
> exists it should be stored as a specific attribute within MediaType.
> i.e. float MediaType.getAcceptQualifier(). Since q value is never
> extracted, this makes it very difficult to use MediaTypes created
> from an Accept header and use as keys to a Map.
>
Are you suggesting to remove the q parameter from consideration in the
equals and hashCode methods. I.e. a q-value wouldn't be part of the
map returned from getParameters ?

Marc.