Removed hashCode() and equals() and fixed toString().

MetaData instances are mutable, and hashCode() and equals() were
based on those mutable fields; mutable hash codes break hash data
structures, when the object is inserted and then its hash code changes.

Fixed toString() to avoid NPE if _fields is null.
This commit is contained in:
Simone Bordet 2015-02-20 10:50:39 +01:00
parent 71f6527cae
commit 2d932fe5c7
1 changed files with 5 additions and 69 deletions

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.http;
import java.util.Collections;
import java.util.Iterator;
import java.util.Objects;
public class MetaData implements Iterable<HttpField>
{
@ -50,6 +49,7 @@ public class MetaData implements Iterable<HttpField>
_httpVersion = null;
if (_fields != null)
_fields.clear();
_fields = null;
_contentLength = Long.MIN_VALUE;
}
@ -122,31 +122,6 @@ public class MetaData implements Iterable<HttpField>
return fields == null ? Collections.<HttpField>emptyIterator() : fields.iterator();
}
@Override
public int hashCode()
{
HttpVersion version = getVersion();
int hash = version == null ? 0 : version.hashCode();
HttpFields fields = getFields();
return 31 * hash + (fields == null ? 0 : fields.hashCode());
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof MetaData))
return false;
MetaData that = (MetaData)o;
if (getVersion() != that.getVersion())
return false;
return Objects.equals(getFields(), that.getFields());
}
@Override
public String toString()
{
@ -246,33 +221,12 @@ public class MetaData implements Iterable<HttpField>
_uri = uri;
}
@Override
public int hashCode()
{
int hash = super.hashCode();
hash = hash * 31 + (_method == null ? 0 : _method.hashCode());
return hash * 31 + (_uri == null ? 0 : _uri.hashCode());
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof MetaData.Request))
return false;
MetaData.Request that = (MetaData.Request)o;
if (!Objects.equals(getMethod(), that.getMethod()) ||
!Objects.equals(getURI(), that.getURI()))
return false;
return super.equals(o);
}
@Override
public String toString()
{
HttpFields fields = getFields();
return String.format("%s{u=%s,%s,h=%d}",
getMethod(), getURI(), getVersion(), getFields().size());
getMethod(), getURI(), getVersion(), fields == null ? -1 : fields.size());
}
}
@ -342,29 +296,11 @@ public class MetaData implements Iterable<HttpField>
_reason = reason;
}
@Override
public int hashCode()
{
return 31 * super.hashCode() + getStatus();
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof MetaData.Response))
return false;
MetaData.Response that = (MetaData.Response)o;
if (getStatus() != that.getStatus())
return false;
return super.equals(o);
}
@Override
public String toString()
{
return String.format("%s{s=%d,h=%d}", getVersion(), getStatus(), getFields().size());
HttpFields fields = getFields();
return String.format("%s{s=%d,h=%d}", getVersion(), getStatus(), fields == null ? -1 : fields.size());
}
}
}