Skip to content

Add RepositoryMetadataAccess interface #3145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Optional;
import java.util.stream.Stream;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
Expand All @@ -41,6 +42,7 @@
* Fragments are immutable.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.0
* @see RepositoryComposition
*/
Expand All @@ -53,7 +55,7 @@ public interface RepositoryFragment<T> {
* @return
*/
static <T> RepositoryFragment<T> implemented(T implementation) {
return new ImplementedRepositoryFragment<T>(Optional.empty(), implementation);
return new ImplementedRepositoryFragment<>((Class<T>) null, implementation);
}

/**
Expand All @@ -64,7 +66,7 @@ static <T> RepositoryFragment<T> implemented(T implementation) {
* @return
*/
static <T> RepositoryFragment<T> implemented(Class<T> interfaceClass, T implementation) {
return new ImplementedRepositoryFragment<>(Optional.of(interfaceClass), implementation);
return new ImplementedRepositoryFragment<>(interfaceClass, implementation);
}

/**
Expand Down Expand Up @@ -134,7 +136,7 @@ public Class<?> getSignatureContributor() {

@Override
public RepositoryFragment<T> withImplementation(T implementation) {
return new ImplementedRepositoryFragment<>(Optional.of(interfaceOrImplementation), implementation);
return new ImplementedRepositoryFragment<>(interfaceOrImplementation, implementation);
}

@Override
Expand Down Expand Up @@ -164,47 +166,57 @@ public int hashCode() {

class ImplementedRepositoryFragment<T> implements RepositoryFragment<T> {

private final Optional<Class<T>> interfaceClass;
private final @Nullable Class<T> interfaceClass;
private final T implementation;
private final Optional<T> optionalImplementation;

/**
* Creates a new {@link ImplementedRepositoryFragment} for the given interface class and implementation.
*
* @param interfaceClass
* @param implementation
* @deprecated since 3.4 - use {@link ImplementedRepositoryFragment(Class, Object)} instead.
*/
@Deprecated(since = "3.4")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a forRemoval=true so that we clean up the API with Spring Data 4.0

public ImplementedRepositoryFragment(Optional<Class<T>> interfaceClass, T implementation) {
this(interfaceClass.orElse(null), implementation);
}

/**
* Creates a new {@link ImplementedRepositoryFragment} for the given interface class and implementation.
*
* @param interfaceClass must not be {@literal null}.
* @param implementation must not be {@literal null}.
*/
public ImplementedRepositoryFragment(Optional<Class<T>> interfaceClass, T implementation) {
public ImplementedRepositoryFragment(@Nullable Class<T> interfaceClass, T implementation) {

Assert.notNull(interfaceClass, "Interface class must not be null");
Assert.notNull(implementation, "Implementation object must not be null");

interfaceClass.ifPresent(it -> {
if(interfaceClass != null) {

Assert.isTrue(ClassUtils.isAssignableValue(it, implementation),
() -> String.format("Fragment implementation %s does not implement %s",
ClassUtils.getQualifiedName(implementation.getClass()), ClassUtils.getQualifiedName(it)));
});
Assert.isTrue(ClassUtils.isAssignableValue(interfaceClass, implementation),
() -> String.format("Fragment implementation %s does not implement %s",
ClassUtils.getQualifiedName(implementation.getClass()), ClassUtils.getQualifiedName(interfaceClass)));
};

this.interfaceClass = interfaceClass;
this.implementation = implementation;
this.optionalImplementation = Optional.of(implementation);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public Class<?> getSignatureContributor() {
return interfaceClass.orElseGet(() -> {

if(implementation instanceof Class type) {
return type;
}
return (Class<T>) implementation.getClass();
});
if(interfaceClass != null) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Formatting

return interfaceClass;
}

if(implementation instanceof Class<?> type) {
return type;
}
return implementation.getClass();
}

@Override
public Optional<T> getImplementation() {
return optionalImplementation;
return Optional.of(implementation);
}

@Override
Expand All @@ -216,7 +228,7 @@ public RepositoryFragment<T> withImplementation(T implementation) {
public String toString() {

return String.format("ImplementedRepositoryFragment %s%s",
interfaceClass.map(ClassUtils::getShortName).map(it -> it + ":").orElse(""),
interfaceClass != null ? (ClassUtils.getShortName(interfaceClass) + ";") : "",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A slight change in : vs. ;

ClassUtils.getShortName(implementation.getClass()));
}

Expand All @@ -235,18 +247,13 @@ public boolean equals(Object o) {
return false;
}

if (!ObjectUtils.nullSafeEquals(implementation, that.implementation)) {
return false;
}

return ObjectUtils.nullSafeEquals(optionalImplementation, that.optionalImplementation);
return ObjectUtils.nullSafeEquals(implementation, that.implementation);
}

@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(interfaceClass);
result = 31 * result + ObjectUtils.nullSafeHashCode(implementation);
result = 31 * result + ObjectUtils.nullSafeHashCode(optionalImplementation);
return result;
}
}
Expand Down