Browse Source

Polishing in mapping package.

Nullable annotations and Objects.equals(…) and Objects.hash(…)/Objects.hashCode(…). Records for internal cache key.

Related issue: #2813.
pull/2823/head
Oliver Drotbohm 3 years ago
parent
commit
03a6a84bf9
No known key found for this signature in database
GPG Key ID: C25FBFA0DA493A1D
  1. 11
      src/main/java/org/springframework/data/mapping/Alias.java
  2. 29
      src/main/java/org/springframework/data/mapping/Parameter.java
  3. 13
      src/main/java/org/springframework/data/mapping/PreferredConstructor.java
  4. 89
      src/main/java/org/springframework/data/mapping/PropertyPath.java

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

@ -15,9 +15,10 @@
*/ */
package org.springframework.data.mapping; package org.springframework.data.mapping;
import java.util.Objects;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/** /**
* A container object which may or may not contain a type alias value. If a value is present, {@code isPresent()} will * 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 final class Alias {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(@Nullable Object o) {
if (this == o) { if (this == o) {
return true; return true;
} }
if (!(o instanceof Alias alias)) { if (!(o instanceof Alias that)) {
return false; return false;
} }
return ObjectUtils.nullSafeEquals(value, alias.value); return Objects.equals(this.value, that.value);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return ObjectUtils.nullSafeHashCode(value); return Objects.hashCode(value);
} }
} }

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

@ -16,6 +16,7 @@
package org.springframework.data.mapping; package org.springframework.data.mapping;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations;
@ -23,7 +24,6 @@ import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -144,38 +144,25 @@ public class Parameter<T, P extends PersistentProperty<P>> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(@Nullable Object o) {
if (this == o) { if (this == o) {
return true; return true;
} }
if (!(o instanceof Parameter<?, ?> parameter)) { if (!(o instanceof Parameter<?, ?> that)) {
return false; return false;
} }
if (!ObjectUtils.nullSafeEquals(name, parameter.name)) { return Objects.equals(this.name, that.name)
return false; && Objects.equals(this.type, that.type)
} && Objects.equals(this.key, that.key)
&& Objects.equals(this.entity, that.entity);
if (!ObjectUtils.nullSafeEquals(type, parameter.type)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(key, parameter.key)) {
return false;
}
return ObjectUtils.nullSafeEquals(entity, parameter.entity);
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(name); return Objects.hash(name, type, key, entity);
result = 31 * result + ObjectUtils.nullSafeHashCode(type);
result = 31 * result + ObjectUtils.nullSafeHashCode(key);
result = 31 * result + ObjectUtils.nullSafeHashCode(entity);
return result;
} }
/** /**

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

@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.PersistenceCreator;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
@ -35,7 +36,9 @@ import org.springframework.util.ReflectionUtils;
* @author Myeonghyeon Lee * @author Myeonghyeon Lee
* @author Xeno Amess * @author Xeno Amess
*/ */
public final class PreferredConstructor<T, P extends PersistentProperty<P>> extends InstanceCreatorMetadataSupport<T, P> { @SuppressWarnings("deprecation")
public final class PreferredConstructor<T, P extends PersistentProperty<P>>
extends InstanceCreatorMetadataSupport<T, P> {
private final List<Parameter<Object, P>> parameters; private final List<Parameter<Object, P>> parameters;
@ -59,11 +62,11 @@ public final class PreferredConstructor<T, P extends PersistentProperty<P>> exte
* *
* @return * @return
*/ */
@SuppressWarnings("unchecked")
public Constructor<T> getConstructor() { public Constructor<T> getConstructor() {
return (Constructor<T>) getExecutable(); return (Constructor<T>) getExecutable();
} }
/** /**
* Returns whether the constructor does not have any arguments. * Returns whether the constructor does not have any arguments.
* *
@ -80,7 +83,11 @@ public final class PreferredConstructor<T, P extends PersistentProperty<P>> exte
* @return * @return
*/ */
public boolean isExplicitlyAnnotated() { public boolean isExplicitlyAnnotated() {
return MergedAnnotations.from(getExecutable()).isPresent(PersistenceConstructor.class);
var annotations = MergedAnnotations.from(getExecutable());
return annotations.isPresent(PersistenceConstructor.class)
|| annotations.isPresent(PersistenceCreator.class);
} }
/** /**

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

@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Stack; import java.util.Stack;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -30,7 +31,6 @@ import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -244,7 +244,7 @@ public class PropertyPath implements Streamable<PropertyPath> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(@Nullable Object o) {
if (this == o) { if (this == o) {
return true; return true;
@ -258,34 +258,16 @@ public class PropertyPath implements Streamable<PropertyPath> {
return false; return false;
} }
if (!ObjectUtils.nullSafeEquals(owningType, that.owningType)) { return Objects.equals(this.owningType, that.owningType)
return false; && Objects.equals(this.name, that.name)
} && Objects.equals(this.typeInformation, that.typeInformation)
&& Objects.equals(this.actualTypeInformation, that.actualTypeInformation)
if (!ObjectUtils.nullSafeEquals(name, that.name)) { && Objects.equals(next, that.next);
return false;
}
if (!ObjectUtils.nullSafeEquals(typeInformation, that.typeInformation)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(actualTypeInformation, that.actualTypeInformation)) {
return false;
}
return ObjectUtils.nullSafeEquals(next, that.next);
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(owningType); return Objects.hash(owningType, name, typeInformation, actualTypeInformation, isCollection, next);
result = 31 * result + ObjectUtils.nullSafeHashCode(name);
result = 31 * result + ObjectUtils.nullSafeHashCode(typeInformation);
result = 31 * result + ObjectUtils.nullSafeHashCode(actualTypeInformation);
result = 31 * result + (isCollection ? 1 : 0);
result = 31 * result + ObjectUtils.nullSafeHashCode(next);
return result;
} }
/** /**
@ -331,7 +313,7 @@ public class PropertyPath implements Streamable<PropertyPath> {
Assert.hasText(source, "Source must not be null or empty"); Assert.hasText(source, "Source must not be null or empty");
Assert.notNull(type, "TypeInformation must not be null or empty"); Assert.notNull(type, "TypeInformation must not be null or empty");
return cache.computeIfAbsent(Key.of(type, source), it -> { return cache.computeIfAbsent(new Key(type, source), it -> {
List<String> iteratorSource = new ArrayList<>(); List<String> iteratorSource = new ArrayList<>();
@ -467,56 +449,5 @@ public class PropertyPath implements Streamable<PropertyPath> {
return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath()); return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath());
} }
private static final class Key { private record Key(TypeInformation<?> type, String path) {};
private final TypeInformation<?> type;
private final String path;
private Key(TypeInformation<?> type, String path) {
this.type = type;
this.path = path;
}
public static Key of(TypeInformation<?> type, String path) {
return new Key(type, path);
}
public TypeInformation<?> getType() {
return this.type;
}
public String getPath() {
return this.path;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Key key)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(type, key.type)) {
return false;
}
return ObjectUtils.nullSafeEquals(path, key.path);
}
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(type);
result = 31 * result + ObjectUtils.nullSafeHashCode(path);
return result;
}
@Override
public String toString() {
return "PropertyPath.Key(type=" + this.getType() + ", path=" + this.getPath() + ")";
}
}
} }

Loading…
Cancel
Save