Skip to content

Commit 82d67c1

Browse files
Favor ObjectUtils over Objects for equals/hashCode.
Original Pull Request: #3684
1 parent 85a30ec commit 82d67c1

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/UnwrapEntityContext.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.mapping;
1717

18-
import java.util.Objects;
18+
import org.springframework.util.ObjectUtils;
1919

2020
/**
2121
* @author Christoph Strobl
@@ -36,21 +36,11 @@ public MongoPersistentProperty getProperty() {
3636

3737
/*
3838
* (non-Javadoc)
39-
*
40-
* @see java.lang.Object#hashCode()
41-
*/
42-
@Override
43-
public int hashCode() {
44-
return Objects.hash(property);
45-
}
46-
47-
/*
48-
* (non-Javadoc)
49-
*
5039
* @see java.lang.Object#equals(java.lang.Object)
5140
*/
5241
@Override
5342
public boolean equals(Object obj) {
43+
5444
if (this == obj) {
5545
return true;
5646
}
@@ -59,8 +49,16 @@ public boolean equals(Object obj) {
5949
return false;
6050
}
6151

62-
UnwrapEntityContext other = (UnwrapEntityContext) obj;
52+
UnwrapEntityContext that = (UnwrapEntityContext) obj;
53+
return ObjectUtils.nullSafeEquals(property, that.property);
54+
}
6355

64-
return Objects.equals(property, other.property);
56+
/*
57+
* (non-Javadoc)
58+
* @see java.lang.Object#hashCode()
59+
*/
60+
@Override
61+
public int hashCode() {
62+
return ObjectUtils.nullSafeHashCode(property);
6563
}
6664
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/UnwrappedMongoPersistentProperty.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import java.lang.annotation.Annotation;
1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Method;
21-
import java.util.Objects;
2221

2322
import org.springframework.data.mapping.Association;
2423
import org.springframework.data.mapping.PersistentEntity;
2524
import org.springframework.data.mapping.PersistentPropertyAccessor;
2625
import org.springframework.data.util.TypeInformation;
2726
import org.springframework.lang.Nullable;
27+
import org.springframework.util.ObjectUtils;
2828

2929
/**
3030
* Unwrapped variant of {@link MongoPersistentProperty}.
@@ -325,21 +325,11 @@ public <T> PersistentPropertyAccessor<T> getAccessorForOwner(T owner) {
325325

326326
/*
327327
* (non-Javadoc)
328-
*
329-
* @see java.lang.Object#hashCode()
330-
*/
331-
@Override
332-
public int hashCode() {
333-
return Objects.hash(delegate, context);
334-
}
335-
336-
/*
337-
* (non-Javadoc)
338-
*
339328
* @see java.lang.Object#equals(java.lang.Object)
340329
*/
341330
@Override
342331
public boolean equals(Object obj) {
332+
343333
if (this == obj) {
344334
return true;
345335
}
@@ -348,8 +338,22 @@ public boolean equals(Object obj) {
348338
return false;
349339
}
350340

351-
UnwrappedMongoPersistentProperty other = (UnwrappedMongoPersistentProperty) obj;
341+
UnwrappedMongoPersistentProperty that = (UnwrappedMongoPersistentProperty) obj;
342+
if (!ObjectUtils.nullSafeEquals(delegate, that.delegate)) {
343+
return false;
344+
}
345+
return ObjectUtils.nullSafeEquals(context, that.context);
346+
}
347+
348+
/*
349+
* (non-Javadoc)
350+
* @see java.lang.Object#hashCode()
351+
*/
352+
@Override
353+
public int hashCode() {
352354

353-
return Objects.equals(delegate, other.delegate) && Objects.equals(context, other.context);
355+
int result = ObjectUtils.nullSafeHashCode(delegate);
356+
result = 31 * result + ObjectUtils.nullSafeHashCode(context);
357+
return result;
354358
}
355359
}

0 commit comments

Comments
 (0)