From 3832a8645d858de657a46859ca2ae841c8afb285 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 17 Jun 2014 18:52:42 +0200 Subject: [PATCH] Properly implemented hashCode() and equals(). --- .../org/eclipse/jetty/http/HttpField.java | 49 ++++++-------- .../org/eclipse/jetty/http/HttpFields.java | 41 +++++++++--- .../java/org/eclipse/jetty/http/MetaData.java | 66 ++++++++++++++++++- 3 files changed, 119 insertions(+), 37 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java index d24fff8770c..f3b21d83892 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java @@ -20,9 +20,6 @@ package org.eclipse.jetty.http; import java.util.ArrayList; -import org.eclipse.jetty.util.QuotedStringTokenizer; - - /* ------------------------------------------------------------ */ /** A HTTP Field */ @@ -222,7 +219,27 @@ public class HttpField return false; } - @Override + 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) { if (o==this) @@ -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(); - } - - - } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java index c0a674bb630..f7d47d569d0 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java @@ -51,14 +51,12 @@ import org.eclipse.jetty.util.log.Logger; */ public class HttpFields implements Iterable { - private static final Logger LOG = Log.getLogger(HttpFields.class); - public final static String __separators = ", \t"; + public static final String __separators = ", \t"; - final List _fields; + private static final Logger LOG = Log.getLogger(HttpFields.class); + + private final List _fields; - /** - * Constructor. - */ public HttpFields() { _fields=new ArrayList<>(); @@ -563,8 +561,35 @@ public class HttpFields implements Iterable } @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 { diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/MetaData.java b/jetty-http/src/main/java/org/eclipse/jetty/http/MetaData.java index 8740af384ce..39a975bfa6d 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/MetaData.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/MetaData.java @@ -56,7 +56,28 @@ public class MetaData implements Iterable { 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 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 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() {