Add equals/hashcode to ResponseEntity

Issue: SPR-9714
This commit is contained in:
Rossen Stoyanchev
2012-08-28 17:24:31 -04:00
parent 473de081b8
commit a49851d5eb
3 changed files with 66 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.http;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
/**
* Represents an HTTP request or response entity, consisting of headers and body.
@@ -122,6 +123,24 @@ public class HttpEntity<T> {
return (this.body != null);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof HttpEntity)) {
return false;
}
HttpEntity<?> otherEntity = (HttpEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.headers, otherEntity.headers) &&
ObjectUtils.nullSafeEquals(this.body, otherEntity.body));
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.headers) * 29 + ObjectUtils.nullSafeHashCode(this.body);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("<");
@@ -137,4 +156,5 @@ public class HttpEntity<T> {
builder.append('>');
return builder.toString();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.http;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
/**
* Extension of {@link HttpEntity} that adds a {@link HttpStatus} status code.
@@ -94,6 +95,23 @@ public class ResponseEntity<T> extends HttpEntity<T> {
return statusCode;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ResponseEntity)) {
return false;
}
ResponseEntity<?> otherEntity = (ResponseEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.statusCode, otherEntity.statusCode) && super.equals(other));
}
@Override
public int hashCode() {
return super.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.statusCode);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("<");