Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Add missing Override annotations, consistently use literal tags for boolean values.
  • Loading branch information
mp911de committed Nov 21, 2023
1 parent 9b05305 commit 6af7659
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.ReflectionUtils;
Expand Down Expand Up @@ -197,10 +196,12 @@ public boolean isTransient() {
return isTransient.get();
}

@Override
public boolean isIdProperty() {
return isId.get();
}

@Override
public boolean isVersionProperty() {
return isVersion.get();
}
Expand All @@ -226,6 +227,7 @@ public boolean isWritable() {
* @param annotationType must not be {@literal null}.
* @return {@literal null} if annotation type not found on property.
*/
@Override
@Nullable
public <A extends Annotation> A findAnnotation(Class<A> annotationType) {

Expand Down Expand Up @@ -262,11 +264,12 @@ public <A extends Annotation> A findPropertyOrOwnerAnnotation(Class<A> annotatio
}

/**
* Returns whether the property carries the an annotation of the given type.
* Returns whether the property carries the annotation of the given type.
*
* @param annotationType the annotation type to look up.
* @return
* @return {@literal true} if the annotation is present, {@literal false} otherwise.
*/
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
return doFindAnnotation(annotationType).isPresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato

@Nullable
@Override
@SuppressWarnings("unchecked")
public PreferredConstructor<T, P> getPersistenceConstructor() {
return creator instanceof PreferredConstructor ? (PreferredConstructor<T, P>) creator : null;
}
Expand All @@ -151,36 +150,89 @@ public boolean isCreatorArgument(PersistentProperty<?> property) {
return creator != null && creator.isCreatorParameter(property);
}

/**
* Calculates the {@link Alias} to be used for the given type.
*
* @param type must not be {@literal null}.
* @return the alias for {@code type}
*/
private static Alias getAliasFromAnnotation(Class<?> type) {

TypeAlias typeAlias = AnnotatedElementUtils.findMergedAnnotation(type, TypeAlias.class);

if (typeAlias != null && StringUtils.hasText(typeAlias.value())) {
return Alias.of(typeAlias.value());
}

return Alias.empty();
}

@Override
public boolean isIdProperty(PersistentProperty<?> property) {
return idProperty != null && idProperty.equals(property);
}

@Override
public boolean isVersionProperty(PersistentProperty<?> property) {
return versionProperty != null && versionProperty.equals(property);
}

@Override
public String getName() {
return getType().getName();
}

@Override
@Nullable
public P getIdProperty() {
return idProperty;
}

@Override
@Nullable
public P getVersionProperty() {
return versionProperty;
}

@Override
public boolean hasIdProperty() {
return idProperty != null;
}

@Override
public boolean hasVersionProperty() {
return versionProperty != null;
}

@Override
public void setEvaluationContextProvider(EvaluationContextProvider provider) {
this.evaluationContextProvider = provider;
}

/**
* Returns the given property if it is a better candidate for the id property than the current id property.
*
* @param property the new id property candidate, will never be {@literal null}.
* @return the given id property or {@literal null} if the given property is not an id property.
*/
@Nullable
protected P returnPropertyIfBetterIdPropertyCandidateOrNull(P property) {

if (!property.isIdProperty()) {
return null;
}

P idProperty = this.idProperty;

if (idProperty != null) {
throw new MappingException(String.format("Attempt to add id property %s but already have property %s registered "
+ "as id; Check your mapping configuration ", property.getField(), idProperty.getField()));
}

return property;
}

@Override
public void addPersistentProperty(P property) {

Assert.notNull(property, "Property must not be null");
Expand Down Expand Up @@ -230,41 +282,6 @@ public void addPersistentProperty(P property) {
}
}

@Override
public void setEvaluationContextProvider(EvaluationContextProvider provider) {
this.evaluationContextProvider = provider;
}

/**
* Returns the given property if it is a better candidate for the id property than the current id property.
*
* @param property the new id property candidate, will never be {@literal null}.
* @return the given id property or {@literal null} if the given property is not an id property.
*/
@Nullable
protected P returnPropertyIfBetterIdPropertyCandidateOrNull(P property) {

if (!property.isIdProperty()) {
return null;
}

P idProperty = this.idProperty;

if (idProperty != null) {
throw new MappingException(String.format("Attempt to add id property %s but already have property %s registered "
+ "as id; Check your mapping configuration ", property.getField(), idProperty.getField()));
}

return property;
}

public void addAssociation(Association<P> association) {

Assert.notNull(association, "Association must not be null");

associations.add(association);
}

@Override
@Nullable
public P getPersistentProperty(String name) {
Expand Down Expand Up @@ -306,27 +323,29 @@ private List<P> doFindPersistentProperty(Class<? extends Annotation> annotationT
.filter(it -> it.isAnnotationPresent(annotationType)).collect(Collectors.toList());
}

@Override
public void addAssociation(Association<P> association) {

Assert.notNull(association, "Association must not be null");

associations.add(association);
}

@Override
public Class<T> getType() {
return information.getType();
}

@Override
public Alias getTypeAlias() {
return typeAlias.get();
}

@Override
public TypeInformation<T> getTypeInformation() {
return information;
}

public void doWithProperties(PropertyHandler<P> handler) {

Assert.notNull(handler, "PropertyHandler must not be null");

for (P property : persistentPropertiesCache) {
handler.doWithPersistentProperty(property);
}
}

@Override
public void doWithProperties(SimplePropertyHandler handler) {

Expand All @@ -337,20 +356,22 @@ public void doWithProperties(SimplePropertyHandler handler) {
}
}

public void doWithAssociations(AssociationHandler<P> handler) {
@Override
public void doWithProperties(PropertyHandler<P> handler) {

Assert.notNull(handler, "Handler must not be null");
Assert.notNull(handler, "PropertyHandler must not be null");

for (Association<P> association : associations) {
handler.doWithAssociation(association);
for (P property : persistentPropertiesCache) {
handler.doWithPersistentProperty(property);
}
}

public void doWithAssociations(SimpleAssociationHandler handler) {
@Override
public void doWithAssociations(AssociationHandler<P> handler) {

Assert.notNull(handler, "Handler must not be null");

for (Association<? extends PersistentProperty<?>> association : associations) {
for (Association<P> association : associations) {
handler.doWithAssociation(association);
}
}
Expand All @@ -373,11 +394,13 @@ private <A extends Annotation> Optional<A> doFindAnnotation(Class<A> annotationT
it -> Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(getType(), it)));
}

public void verify() {
@Override
public void doWithAssociations(SimpleAssociationHandler handler) {

if (comparator != null) {
properties.sort(comparator);
persistentPropertiesCache.sort(comparator);
Assert.notNull(handler, "Handler must not be null");

for (Association<? extends PersistentProperty<?>> association : associations) {
handler.doWithAssociation(association);
}
}

Expand Down Expand Up @@ -471,16 +494,13 @@ protected EvaluationContext getEvaluationContext(Object rootObject, ExpressionDe
return evaluationContextProvider.getEvaluationContext(rootObject, dependencies);
}

/**
* Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default.
* Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the
* user wants to be in control over whether an entity is new or not.
*
* @return
* @since 2.1
*/
protected IsNewStrategy getFallbackIsNewStrategy() {
return PersistentEntityIsNewStrategy.of(this);
@Override
public void verify() {

if (comparator != null) {
properties.sort(comparator);
persistentPropertiesCache.sort(comparator);
}
}

/**
Expand All @@ -496,20 +516,15 @@ private void verifyBeanType(Object bean) {
}

/**
* Calculates the {@link Alias} to be used for the given type.
* Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default.
* Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the
* user wants to be in control over whether an entity is new or not.
*
* @param type must not be {@literal null}.
* @return
* @return the fallback {@link IsNewStrategy}.
* @since 2.1
*/
private static Alias getAliasFromAnnotation(Class<?> type) {

TypeAlias typeAlias = AnnotatedElementUtils.findMergedAnnotation(type, TypeAlias.class);

if (typeAlias != null && StringUtils.hasText(typeAlias.value())) {
return Alias.of(typeAlias.value());
}

return Alias.empty();
protected IsNewStrategy getFallbackIsNewStrategy() {
return PersistentEntityIsNewStrategy.of(this);
}

/**
Expand Down Expand Up @@ -546,6 +561,7 @@ private static final class AssociationComparator<P extends PersistentProperty<P>
this.delegate = delegate;
}

@Override
public int compare(@Nullable Association<P> left, @Nullable Association<P> right) {

if (left == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public Class<?> getParameterType() {
}

/**
* @return {@code true} if the value hierarchy applies boxing.
* @return {@literal true} if the value hierarchy applies boxing.
*/
public boolean appliesBoxing() {
return applyBoxing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
private final @Nullable Object parent;

public PersistentEntityParameterValueProvider(PersistentEntity<?, P> entity, PropertyValueProvider<P> provider,
Object parent) {
@Nullable Object parent) {
this.entity = entity;
this.provider = provider;
this.parent = parent;
Expand All @@ -53,6 +53,7 @@ private static Object getTransientDefault(Class<?> parameterType) {
return parameterType.isPrimitive() ? ReflectionUtils.getPrimitiveDefault(parameterType) : null;
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <T> T getParameterValue(Parameter<T, P> parameter) {
Expand Down

0 comments on commit 6af7659

Please sign in to comment.