Properly implemented hashCode() and equals().

This commit is contained in:
Simone Bordet 2014-06-17 18:52:42 +02:00
parent 82a2dfd03a
commit 3832a8645d
3 changed files with 119 additions and 37 deletions

View File

@ -20,9 +20,6 @@ package org.eclipse.jetty.http;
import java.util.ArrayList;
import org.eclipse.jetty.util.QuotedStringTokenizer;
/* ------------------------------------------------------------ */
/** A HTTP Field
*/
@ -222,6 +219,26 @@ public class HttpField
return false;
}
private int nameHashCode()
{
int hash=13;
int len = _name.length();
for (int i = 0; i < len; i++)
{
char c = Character.toUpperCase(_name.charAt(i));
hash = 31 * hash + c;
}
return hash;
}
@Override
public int hashCode()
{
if (_header==null)
return _value.hashCode() ^ nameHashCode();
return _value.hashCode() ^ _header.hashCode();
}
@Override
public boolean equals(Object o)
{
@ -240,28 +257,4 @@ public class HttpField
return false;
return true;
}
public int nameHashCode()
{
int hash=13;
int len = _name.length();
for (int i = 0; i < len; i++)
{
char c = Character.toUpperCase(_name.charAt(i));
hash = 31 * hash + c;
}
return hash;
}
@Override
public int hashCode()
{
if (_header==null)
return _value.hashCode() ^ nameHashCode();
return _value.hashCode() ^ _header.hashCode();
}
}

View File

@ -51,14 +51,12 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class HttpFields implements Iterable<HttpField>
{
public static final String __separators = ", \t";
private static final Logger LOG = Log.getLogger(HttpFields.class);
public final static String __separators = ", \t";
final List<HttpField> _fields;
private final List<HttpField> _fields;
/**
* Constructor.
*/
public HttpFields()
{
_fields=new ArrayList<>();
@ -563,8 +561,35 @@ public class HttpFields implements Iterable<HttpField>
}
@Override
public String
toString()
public int hashCode()
{
return _fields.hashCode();
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof HttpFields))
return false;
HttpFields that = (HttpFields)o;
// Order is not important, so we cannot rely on List.equals().
if (size() != that.size())
return false;
for (HttpField field : this)
{
if (!that.contains(field))
return false;
}
return true;
}
@Override
public String toString()
{
try
{

View File

@ -57,6 +57,27 @@ public class MetaData implements Iterable<HttpField>
return _fields;
}
@Override
public int hashCode()
{
return 31 * _version.hashCode() + _fields.hashCode();
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof MetaData))
return false;
MetaData that = (MetaData)o;
if (_version != that._version)
return false;
return _fields.equals(that._fields);
}
@Override
public String toString()
{
@ -141,6 +162,30 @@ public class MetaData implements Iterable<HttpField>
return _uri;
}
@Override
public int hashCode()
{
int hash = _method.hashCode();
hash = 31 * hash + _scheme.hashCode();
hash = 31 * hash + _uri.hashCode();
return 31 * hash + super.hashCode();
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof Request))
return false;
Request that = (Request)o;
if (!_method.equals(that._method) ||
!_scheme.equals(that._scheme) ||
!_uri.equals(that._uri))
return false;
return super.equals(o);
}
@Override
public String toString()
{
@ -179,6 +224,25 @@ public class MetaData implements Iterable<HttpField>
return _status;
}
@Override
public int hashCode()
{
return 31 * _status + super.hashCode();
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof Response))
return false;
Response that = (Response)o;
if (_status != that._status)
return false;
return super.equals(o);
}
@Override
public String toString()
{