DATACMNS-867 - Removed Optional from QuerydslBindingsFactory API.

This commit is contained in:
Oliver Gierke
2017-03-15 16:43:00 +01:00
parent 9b4188bfe8
commit 917c3e2889
4 changed files with 41 additions and 18 deletions

View File

@@ -20,7 +20,6 @@ import lombok.RequiredArgsConstructor;
import lombok.Value;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
@@ -37,7 +36,6 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
/**
* {@link QuerydslBindings} allows definition of path specific bindings.
@@ -478,7 +476,8 @@ public class QuerydslBindings {
public <P extends Path<T>> void first(SingleValueBinding<P, T> binding) {
Assert.notNull(binding, "Binding must not be null!");
all((MultiValueBinding<P, T>) (path, value) -> Optionals.next(value.iterator()).flatMap(t -> binding.bind(path, t)));
all((MultiValueBinding<P, T>) (path, value) -> Optionals.next(value.iterator())
.flatMap(t -> binding.bind(path, t)));
}
/**
@@ -490,7 +489,7 @@ public class QuerydslBindings {
Assert.notNull(binding, "Binding must not be null!");
QuerydslBindings.this.typeSpecs.put(type, PathAndBinding.<T, P>withoutPath().with(binding));
QuerydslBindings.this.typeSpecs.put(type, PathAndBinding.<T, P> withoutPath().with(binding));
}
}

View File

@@ -84,6 +84,30 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
return entityPathResolver;
}
/**
* Creates the {@link QuerydslBindings} to be used using for the given domain type. A {@link QuerydslBinderCustomizer}
* will be auto-detected.
*
* @param domainType must not be {@literal null}.
* @return will never be {@literal null}.
*/
public QuerydslBindings createBindingsFor(TypeInformation<?> domainType) {
return createBindingsFor(domainType, Optional.empty());
}
/**
* Creates the {@link QuerydslBindings} to be used using for the given domain type and a pre-defined
* {@link QuerydslBinderCustomizer}.
*
* @param domainType must not be {@literal null}.
* @param customizer the {@link QuerydslBinderCustomizer} to use, must not be {@literal null}.
* @return will never be {@literal null}.
*/
public QuerydslBindings createBindingsFor(TypeInformation<?> domainType,
Class<? extends QuerydslBinderCustomizer<?>> customizer) {
return createBindingsFor(domainType, Optional.of(customizer));
}
/**
* Creates the {@link QuerydslBindings} to be used using for the given domain type and a pre-defined
* {@link QuerydslBinderCustomizer}. If no customizer is given, auto-detection will be applied.
@@ -93,7 +117,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
* detection for the given domain type will be applied.
* @return
*/
public QuerydslBindings createBindingsFor(TypeInformation<?> domainType,
private QuerydslBindings createBindingsFor(TypeInformation<?> domainType,
Optional<Class<? extends QuerydslBinderCustomizer<?>>> customizer) {
Assert.notNull(customizer, "Customizer must not be null!");
@@ -141,7 +165,8 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
return customizer//
.filter(it -> !QuerydslBinderCustomizer.class.equals(it))//
.map(this::createQuerydslBinderCustomizer).orElseGet(() -> repositories.flatMap(it -> it.getRepositoryFor(domainType))//
.map(this::createQuerydslBinderCustomizer)
.orElseGet(() -> repositories.flatMap(it -> it.getRepositoryFor(domainType))//
.map(it -> it instanceof QuerydslBinderCustomizer ? (QuerydslBinderCustomizer<EntityPath<?>>) it : null)//
.orElse(NoOpCustomizer.INSTANCE));
}

View File

@@ -23,10 +23,10 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.data.querydsl.binding.QuerydslPredicateBuilder;
import org.springframework.data.util.CastUtils;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.LinkedMultiValueMap;
@@ -89,7 +89,6 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory)
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Predicate resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
@@ -103,13 +102,13 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
.ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class));
TypeInformation<?> domainType = extractTypeInfo(parameter).getActualType();
Optional<? extends Class<? extends QuerydslBinderCustomizer>> map = annotation
.<Class<? extends QuerydslBinderCustomizer>>map(QuerydslPredicate::bindings);
Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindings = annotation//
.map(QuerydslPredicate::bindings)//
.map(CastUtils::cast);
QuerydslBindings bindings = bindingsFactory.createBindingsFor(domainType,
(Optional<Class<? extends QuerydslBinderCustomizer<?>>>) map);
return predicateBuilder.getPredicate(domainType, parameters, bindings);
return predicateBuilder.getPredicate(domainType, parameters,
bindings.map(it -> bindingsFactory.createBindingsFor(domainType, it))
.orElseGet(() -> bindingsFactory.createBindingsFor(domainType)));
}
/**
@@ -125,7 +124,7 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
.ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class));
return annotation.filter(it -> !Object.class.equals(it.root()))//
.<TypeInformation<?>>map(it -> ClassTypeInformation.from(it.root()))//
.<TypeInformation<?>> map(it -> ClassTypeInformation.from(it.root()))//
.orElseGet(() -> detectDomainType(ClassTypeInformation.fromReturnTypeOf(parameter.getMethod())));
}