Skip to content

Commit 992e4cb

Browse files
committed
Polishing in mapping package.
Nullable annotations and Objects.equals(…) and Objects.hash(…)/Objects.hashCode(…). Records for internal cache key. Related issue: #2813.
1 parent 3f695a5 commit 992e4cb

File tree

4 files changed

+34
-108
lines changed

4 files changed

+34
-108
lines changed

src/main/java/org/springframework/data/mapping/Alias.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
package org.springframework.data.mapping;
1717

18+
import java.util.Objects;
19+
1820
import org.springframework.lang.Nullable;
1921
import org.springframework.util.Assert;
20-
import org.springframework.util.ObjectUtils;
2122

2223
/**
2324
* A container object which may or may not contain a type alias value. If a value is present, {@code isPresent()} will
@@ -144,21 +145,21 @@ public Object getValue() {
144145
}
145146

146147
@Override
147-
public boolean equals(Object o) {
148+
public boolean equals(@Nullable Object o) {
148149

149150
if (this == o) {
150151
return true;
151152
}
152153

153-
if (!(o instanceof Alias alias)) {
154+
if (!(o instanceof Alias that)) {
154155
return false;
155156
}
156157

157-
return ObjectUtils.nullSafeEquals(value, alias.value);
158+
return Objects.equals(this.value, that.value);
158159
}
159160

160161
@Override
161162
public int hashCode() {
162-
return ObjectUtils.nullSafeHashCode(value);
163+
return Objects.hashCode(value);
163164
}
164165
}

src/main/java/org/springframework/data/mapping/Parameter.java

+8-21
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
package org.springframework.data.mapping;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.Objects;
1920

2021
import org.springframework.beans.factory.annotation.Value;
2122
import org.springframework.core.annotation.MergedAnnotations;
2223
import org.springframework.data.util.Lazy;
2324
import org.springframework.data.util.TypeInformation;
2425
import org.springframework.lang.Nullable;
2526
import org.springframework.util.Assert;
26-
import org.springframework.util.ObjectUtils;
2727
import org.springframework.util.StringUtils;
2828

2929
/**
@@ -144,38 +144,25 @@ public boolean hasSpelExpression() {
144144
}
145145

146146
@Override
147-
public boolean equals(Object o) {
147+
public boolean equals(@Nullable Object o) {
148148

149149
if (this == o) {
150150
return true;
151151
}
152152

153-
if (!(o instanceof Parameter<?, ?> parameter)) {
153+
if (!(o instanceof Parameter<?, ?> that)) {
154154
return false;
155155
}
156156

157-
if (!ObjectUtils.nullSafeEquals(name, parameter.name)) {
158-
return false;
159-
}
160-
161-
if (!ObjectUtils.nullSafeEquals(type, parameter.type)) {
162-
return false;
163-
}
164-
165-
if (!ObjectUtils.nullSafeEquals(key, parameter.key)) {
166-
return false;
167-
}
168-
169-
return ObjectUtils.nullSafeEquals(entity, parameter.entity);
157+
return Objects.equals(this.name, that.name)
158+
&& Objects.equals(this.type, that.type)
159+
&& Objects.equals(this.key, that.key)
160+
&& Objects.equals(this.entity, that.entity);
170161
}
171162

172163
@Override
173164
public int hashCode() {
174-
int result = ObjectUtils.nullSafeHashCode(name);
175-
result = 31 * result + ObjectUtils.nullSafeHashCode(type);
176-
result = 31 * result + ObjectUtils.nullSafeHashCode(key);
177-
result = 31 * result + ObjectUtils.nullSafeHashCode(entity);
178-
return result;
165+
return Objects.hash(name, type, key, entity);
179166
}
180167

181168
/**

src/main/java/org/springframework/data/mapping/PreferredConstructor.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.core.annotation.MergedAnnotations;
2323
import org.springframework.data.annotation.PersistenceConstructor;
24+
import org.springframework.data.annotation.PersistenceCreator;
2425
import org.springframework.util.Assert;
2526
import org.springframework.util.ReflectionUtils;
2627

@@ -35,7 +36,9 @@
3536
* @author Myeonghyeon Lee
3637
* @author Xeno Amess
3738
*/
38-
public final class PreferredConstructor<T, P extends PersistentProperty<P>> extends InstanceCreatorMetadataSupport<T, P> {
39+
@SuppressWarnings("deprecation")
40+
public final class PreferredConstructor<T, P extends PersistentProperty<P>>
41+
extends InstanceCreatorMetadataSupport<T, P> {
3942

4043
private final List<Parameter<Object, P>> parameters;
4144

@@ -59,11 +62,11 @@ public PreferredConstructor(Constructor<T> constructor, Parameter<Object, P>...
5962
*
6063
* @return
6164
*/
65+
@SuppressWarnings("unchecked")
6266
public Constructor<T> getConstructor() {
6367
return (Constructor<T>) getExecutable();
6468
}
6569

66-
6770
/**
6871
* Returns whether the constructor does not have any arguments.
6972
*
@@ -80,7 +83,11 @@ public boolean isNoArgConstructor() {
8083
* @return
8184
*/
8285
public boolean isExplicitlyAnnotated() {
83-
return MergedAnnotations.from(getExecutable()).isPresent(PersistenceConstructor.class);
86+
87+
var annotations = MergedAnnotations.from(getExecutable());
88+
89+
return annotations.isPresent(PersistenceConstructor.class)
90+
|| annotations.isPresent(PersistenceCreator.class);
8491
}
8592

8693
/**

src/main/java/org/springframework/data/mapping/PropertyPath.java

+10-79
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.Objects;
2425
import java.util.Stack;
2526
import java.util.regex.Matcher;
2627
import java.util.regex.Pattern;
@@ -30,7 +31,6 @@
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.ConcurrentReferenceHashMap;
33-
import org.springframework.util.ObjectUtils;
3434
import org.springframework.util.StringUtils;
3535

3636
/**
@@ -244,7 +244,7 @@ public void remove() {
244244
}
245245

246246
@Override
247-
public boolean equals(Object o) {
247+
public boolean equals(@Nullable Object o) {
248248

249249
if (this == o) {
250250
return true;
@@ -258,34 +258,16 @@ public boolean equals(Object o) {
258258
return false;
259259
}
260260

261-
if (!ObjectUtils.nullSafeEquals(owningType, that.owningType)) {
262-
return false;
263-
}
264-
265-
if (!ObjectUtils.nullSafeEquals(name, that.name)) {
266-
return false;
267-
}
268-
269-
if (!ObjectUtils.nullSafeEquals(typeInformation, that.typeInformation)) {
270-
return false;
271-
}
272-
273-
if (!ObjectUtils.nullSafeEquals(actualTypeInformation, that.actualTypeInformation)) {
274-
return false;
275-
}
276-
277-
return ObjectUtils.nullSafeEquals(next, that.next);
261+
return Objects.equals(this.owningType, that.owningType)
262+
&& Objects.equals(this.name, that.name)
263+
&& Objects.equals(this.typeInformation, that.typeInformation)
264+
&& Objects.equals(this.actualTypeInformation, that.actualTypeInformation)
265+
&& Objects.equals(next, that.next);
278266
}
279267

280268
@Override
281269
public int hashCode() {
282-
int result = ObjectUtils.nullSafeHashCode(owningType);
283-
result = 31 * result + ObjectUtils.nullSafeHashCode(name);
284-
result = 31 * result + ObjectUtils.nullSafeHashCode(typeInformation);
285-
result = 31 * result + ObjectUtils.nullSafeHashCode(actualTypeInformation);
286-
result = 31 * result + (isCollection ? 1 : 0);
287-
result = 31 * result + ObjectUtils.nullSafeHashCode(next);
288-
return result;
270+
return Objects.hash(owningType, name, typeInformation, actualTypeInformation, isCollection, next);
289271
}
290272

291273
/**
@@ -331,7 +313,7 @@ public static PropertyPath from(String source, TypeInformation<?> type) {
331313
Assert.hasText(source, "Source must not be null or empty");
332314
Assert.notNull(type, "TypeInformation must not be null or empty");
333315

334-
return cache.computeIfAbsent(Key.of(type, source), it -> {
316+
return cache.computeIfAbsent(new Key(type, source), it -> {
335317

336318
List<String> iteratorSource = new ArrayList<>();
337319

@@ -467,56 +449,5 @@ public String toString() {
467449
return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath());
468450
}
469451

470-
private static final class Key {
471-
472-
private final TypeInformation<?> type;
473-
private final String path;
474-
475-
private Key(TypeInformation<?> type, String path) {
476-
this.type = type;
477-
this.path = path;
478-
}
479-
480-
public static Key of(TypeInformation<?> type, String path) {
481-
return new Key(type, path);
482-
}
483-
484-
public TypeInformation<?> getType() {
485-
return this.type;
486-
}
487-
488-
public String getPath() {
489-
return this.path;
490-
}
491-
492-
@Override
493-
public boolean equals(Object o) {
494-
495-
if (this == o) {
496-
return true;
497-
}
498-
499-
if (!(o instanceof Key key)) {
500-
return false;
501-
}
502-
503-
if (!ObjectUtils.nullSafeEquals(type, key.type)) {
504-
return false;
505-
}
506-
507-
return ObjectUtils.nullSafeEquals(path, key.path);
508-
}
509-
510-
@Override
511-
public int hashCode() {
512-
int result = ObjectUtils.nullSafeHashCode(type);
513-
result = 31 * result + ObjectUtils.nullSafeHashCode(path);
514-
return result;
515-
}
516-
517-
@Override
518-
public String toString() {
519-
return "PropertyPath.Key(type=" + this.getType() + ", path=" + this.getPath() + ")";
520-
}
521-
}
452+
private record Key(TypeInformation<?> type, String path) {};
522453
}

0 commit comments

Comments
 (0)