Skip to content

Commit a49851d

Browse files
committed
Add equals/hashcode to ResponseEntity
Issue: SPR-9714
1 parent 473de08 commit a49851d

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.http;
1818

1919
import org.springframework.util.MultiValueMap;
20+
import org.springframework.util.ObjectUtils;
2021

2122
/**
2223
* Represents an HTTP request or response entity, consisting of headers and body.
@@ -122,6 +123,24 @@ public boolean hasBody() {
122123
return (this.body != null);
123124
}
124125

126+
@Override
127+
public boolean equals(Object other) {
128+
if (this == other) {
129+
return true;
130+
}
131+
if (!(other instanceof HttpEntity)) {
132+
return false;
133+
}
134+
HttpEntity<?> otherEntity = (HttpEntity<?>) other;
135+
return (ObjectUtils.nullSafeEquals(this.headers, otherEntity.headers) &&
136+
ObjectUtils.nullSafeEquals(this.body, otherEntity.body));
137+
}
138+
139+
@Override
140+
public int hashCode() {
141+
return ObjectUtils.nullSafeHashCode(this.headers) * 29 + ObjectUtils.nullSafeHashCode(this.body);
142+
}
143+
125144
@Override
126145
public String toString() {
127146
StringBuilder builder = new StringBuilder("<");
@@ -137,4 +156,5 @@ public String toString() {
137156
builder.append('>');
138157
return builder.toString();
139158
}
159+
140160
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.http;
1818

1919
import org.springframework.util.MultiValueMap;
20+
import org.springframework.util.ObjectUtils;
2021

2122
/**
2223
* Extension of {@link HttpEntity} that adds a {@link HttpStatus} status code.
@@ -94,6 +95,23 @@ public HttpStatus getStatusCode() {
9495
return statusCode;
9596
}
9697

98+
@Override
99+
public boolean equals(Object other) {
100+
if (this == other) {
101+
return true;
102+
}
103+
if (!(other instanceof ResponseEntity)) {
104+
return false;
105+
}
106+
ResponseEntity<?> otherEntity = (ResponseEntity<?>) other;
107+
return (ObjectUtils.nullSafeEquals(this.statusCode, otherEntity.statusCode) && super.equals(other));
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
return super.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.statusCode);
113+
}
114+
97115
@Override
98116
public String toString() {
99117
StringBuilder builder = new StringBuilder("<");

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ public void noHeaders() {
3434
assertSame(body, entity.getBody());
3535
assertTrue(entity.getHeaders().isEmpty());
3636
}
37-
37+
3838
@Test
3939
public void httpHeaders() {
4040
HttpHeaders headers = new HttpHeaders();
@@ -56,7 +56,30 @@ public void multiValueMap() {
5656
assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType());
5757
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
5858
}
59-
59+
60+
@Test
61+
public void testEquals() {
62+
MultiValueMap<String, String> map1 = new LinkedMultiValueMap<String, String>();
63+
map1.set("Content-Type", "text/plain");
64+
65+
MultiValueMap<String, String> map2 = new LinkedMultiValueMap<String, String>();
66+
map2.set("Content-Type", "application/json");
67+
68+
assertTrue(new HttpEntity<Object>().equals(new HttpEntity<Object>()));
69+
assertFalse(new HttpEntity<Object>(map1).equals(new HttpEntity<Object>()));
70+
assertFalse(new HttpEntity<Object>().equals(new HttpEntity<Object>(map2)));
71+
72+
assertTrue(new HttpEntity<Object>(map1).equals(new HttpEntity<Object>(map1)));
73+
assertFalse(new HttpEntity<Object>(map1).equals(new HttpEntity<Object>(map2)));
74+
75+
assertTrue(new HttpEntity<String>(null, null).equals(new HttpEntity<String>(null, null)));
76+
assertFalse(new HttpEntity<String>("foo", null).equals(new HttpEntity<String>(null, null)));
77+
assertFalse(new HttpEntity<String>(null, null).equals(new HttpEntity<String>("bar", null)));
78+
79+
assertTrue(new HttpEntity<String>("foo", map1).equals(new HttpEntity<String>("foo", map1)));
80+
assertFalse(new HttpEntity<String>("foo", map1).equals(new HttpEntity<String>("bar", map1)));
81+
}
82+
6083
@Test
6184
public void responseEntity() {
6285
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)