Skip to content

Commit c10d4b6

Browse files
Favor ObjectUtils over Objects for equals/hashCode.
Original Pull Request: #3684
1 parent 6644ac6 commit c10d4b6

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}.
@@ -309,21 +309,11 @@ public <T> PersistentPropertyAccessor<T> getAccessorForOwner(T owner) {
309309

310310
/*
311311
* (non-Javadoc)
312-
*
313-
* @see java.lang.Object#hashCode()
314-
*/
315-
@Override
316-
public int hashCode() {
317-
return Objects.hash(delegate, context);
318-
}
319-
320-
/*
321-
* (non-Javadoc)
322-
*
323312
* @see java.lang.Object#equals(java.lang.Object)
324313
*/
325314
@Override
326315
public boolean equals(Object obj) {
316+
327317
if (this == obj) {
328318
return true;
329319
}
@@ -332,8 +322,22 @@ public boolean equals(Object obj) {
332322
return false;
333323
}
334324

335-
UnwrappedMongoPersistentProperty other = (UnwrappedMongoPersistentProperty) obj;
325+
UnwrappedMongoPersistentProperty that = (UnwrappedMongoPersistentProperty) obj;
326+
if (!ObjectUtils.nullSafeEquals(delegate, that.delegate)) {
327+
return false;
328+
}
329+
return ObjectUtils.nullSafeEquals(context, that.context);
330+
}
331+
332+
/*
333+
* (non-Javadoc)
334+
* @see java.lang.Object#hashCode()
335+
*/
336+
@Override
337+
public int hashCode() {
336338

337-
return Objects.equals(delegate, other.delegate) && Objects.equals(context, other.context);
339+
int result = ObjectUtils.nullSafeHashCode(delegate);
340+
result = 31 * result + ObjectUtils.nullSafeHashCode(context);
341+
return result;
338342
}
339343
}

0 commit comments

Comments
 (0)