Browse Source

Add equals/hashcode to ResponseEntity

Issue: SPR-9714
pull/127/head
Rossen Stoyanchev 12 years ago
parent
commit
a49851d5eb
  1. 22
      spring-web/src/main/java/org/springframework/http/HttpEntity.java
  2. 20
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  3. 29
      spring-web/src/test/java/org/springframework/http/HttpEntityTests.java

22
spring-web/src/main/java/org/springframework/http/HttpEntity.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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> { @@ -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> { @@ -137,4 +156,5 @@ public class HttpEntity<T> {
builder.append('>');
return builder.toString();
}
}

20
spring-web/src/main/java/org/springframework/http/ResponseEntity.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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> { @@ -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("<");

29
spring-web/src/test/java/org/springframework/http/HttpEntityTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 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.
@ -34,7 +34,7 @@ public class HttpEntityTests { @@ -34,7 +34,7 @@ public class HttpEntityTests {
assertSame(body, entity.getBody());
assertTrue(entity.getHeaders().isEmpty());
}
@Test
public void httpHeaders() {
HttpHeaders headers = new HttpHeaders();
@ -56,7 +56,30 @@ public class HttpEntityTests { @@ -56,7 +56,30 @@ public class HttpEntityTests {
assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType());
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
}
@Test
public void testEquals() {
MultiValueMap<String, String> map1 = new LinkedMultiValueMap<String, String>();
map1.set("Content-Type", "text/plain");
MultiValueMap<String, String> map2 = new LinkedMultiValueMap<String, String>();
map2.set("Content-Type", "application/json");
assertTrue(new HttpEntity<Object>().equals(new HttpEntity<Object>()));
assertFalse(new HttpEntity<Object>(map1).equals(new HttpEntity<Object>()));
assertFalse(new HttpEntity<Object>().equals(new HttpEntity<Object>(map2)));
assertTrue(new HttpEntity<Object>(map1).equals(new HttpEntity<Object>(map1)));
assertFalse(new HttpEntity<Object>(map1).equals(new HttpEntity<Object>(map2)));
assertTrue(new HttpEntity<String>(null, null).equals(new HttpEntity<String>(null, null)));
assertFalse(new HttpEntity<String>("foo", null).equals(new HttpEntity<String>(null, null)));
assertFalse(new HttpEntity<String>(null, null).equals(new HttpEntity<String>("bar", null)));
assertTrue(new HttpEntity<String>("foo", map1).equals(new HttpEntity<String>("foo", map1)));
assertFalse(new HttpEntity<String>("foo", map1).equals(new HttpEntity<String>("bar", map1)));
}
@Test
public void responseEntity() {
HttpHeaders headers = new HttpHeaders();

Loading…
Cancel
Save