Skip to content

Commit

Permalink
updates the mapping-core
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviojava committed Aug 20, 2019
1 parent c8f1abb commit 5f39f2a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 59 deletions.
Expand Up @@ -87,7 +87,7 @@ private Object executeQuery(Method method, Object[] args, Class<?> typeClass, Co
DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(typeClass)
.withMethodSource(method)
.withList(() -> getTemplate().select(query))
.withResult(() -> getTemplate().select(query))
.withSingleResult(() -> getTemplate().singleResult(query))
.withPagination(DynamicReturn.findPagination(args))
.withListPagination(listPagination(query))
Expand Down
Expand Up @@ -45,32 +45,32 @@ public <T> Optional<T> toOptional(DynamicReturn<T> dynamic) {

@Override
public <T> List<T> toList(DynamicReturn<T> dynamic) {
return dynamic.list();
return dynamic.result();
}

@Override
public <T> Set<T> toSet(DynamicReturn<T> dynamic) {
return new HashSet<>(dynamic.list());
return new HashSet<>(dynamic.result());
}

@Override
public <T> LinkedList<T> toLinkedList(DynamicReturn<T> dynamic) {
return new LinkedList<>(dynamic.list());
return new LinkedList<>(dynamic.result());
}

@Override
public <T> Stream<T> toStream(DynamicReturn<T> dynamic) {
return dynamic.list().stream();
return dynamic.result().stream();
}

@Override
public <T> TreeSet<T> toTreeSet(DynamicReturn<T> dynamic) {
return new TreeSet<>(dynamic.list());
return new TreeSet<>(dynamic.result());
}

@Override
public <T> Object toDefault(DynamicReturn<T> dynamic) {
return dynamic.list();
return dynamic.result();
}

@Override
Expand Down
Expand Up @@ -43,7 +43,6 @@
*/
public final class DynamicReturn<T> implements MethodDynamicExecutable {


/**
* A wrapper function that convert a result as a list to a result as optional
*
Expand Down Expand Up @@ -97,13 +96,11 @@ public static List<Sort> findSorts(Object[] params) {
return sorts;
}


@Override
public Object execute() {
return DynamicReturnConverter.INSTANCE.convert(this);
}


private static class SupplierConverter implements Function<Supplier<Stream<?>>, Supplier<Optional<?>>> {

private final Method method;
Expand Down Expand Up @@ -136,7 +133,7 @@ public Supplier<Optional<?>> apply(Supplier<Stream<?>> l) {

private final Supplier<Optional<T>> singleResult;

private final Supplier<List<T>> list;
private final Supplier<Stream<T>> result;

private final Pagination pagination;

Expand All @@ -148,14 +145,14 @@ public Supplier<Optional<?>> apply(Supplier<Stream<?>> l) {

private DynamicReturn(Class<T> classSource, Method methodSource,
Supplier<Optional<T>> singleResult,
Supplier<List<T>> list, Pagination pagination,
Supplier<Stream<T>> result, Pagination pagination,
Function<Pagination, Optional<T>> singleResultPagination,
Function<Pagination, List<T>> listPagination,
Function<Pagination, Page<T>> page) {
this.classSource = classSource;
this.methodSource = methodSource;
this.singleResult = singleResult;
this.list = list;
this.result = result;
this.pagination = pagination;
this.singleResultPagination = singleResultPagination;
this.listPagination = listPagination;
Expand Down Expand Up @@ -194,8 +191,8 @@ Optional<T> singleResult() {
*
* @return the result as {@link List}
*/
List<T> list() {
return list.get();
Stream<T> result() {
return result.get();
}

/**
Expand Down Expand Up @@ -254,7 +251,7 @@ public static final class DefaultDynamicReturnBuilder<T> {

private Supplier<Optional<T>> singleResult;

private Supplier<List<T>> list;
private Supplier<Stream<T>> result;

private Pagination pagination;

Expand Down Expand Up @@ -295,11 +292,11 @@ public DefaultDynamicReturnBuilder withSingleResult(Supplier<Optional<T>> single
}

/**
* @param list the list
* @param result the list
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withList(Supplier<List<T>> list) {
this.list = list;
public DefaultDynamicReturnBuilder withResult(Supplier<Stream<T>> result) {
this.result = result;
return this;
}

Expand Down Expand Up @@ -349,15 +346,15 @@ public DynamicReturn build() {
requireNonNull(classSource, "the class Source is required");
requireNonNull(methodSource, "the method Source is required");
requireNonNull(singleResult, "the single result supplier is required");
requireNonNull(list, "the list result supplier is required");
requireNonNull(result, "the result supplier is required");

if (pagination != null) {
requireNonNull(singleResultPagination, "singleResultPagination is required when pagination is not null");
requireNonNull(listPagination, "listPagination is required when pagination is not null");
requireNonNull(page, "page is required when pagination is not null");
}

return new DynamicReturn(classSource, methodSource, singleResult, list,
return new DynamicReturn(classSource, methodSource, singleResult, result,
pagination, singleResultPagination, listPagination, page);
}
}
Expand Down
Expand Up @@ -109,7 +109,7 @@ public Object convert(DynamicQueryMethodReturn dynamicQueryMethod) {
DynamicReturn dynamicReturn = DynamicReturn.builder()
.withClassSource(typeClass)
.withMethodSource(method)
.withList(streamSupplier)
.withResult(streamSupplier)
.withSingleResult(singleSupplier)
.build();

Expand Down
Expand Up @@ -48,7 +48,7 @@ public void shouldReturnInstance() {
dynamic = DynamicReturn.builder()
.withSingleResult(() -> Optional.of(ada))
.withClassSource(Person.class)
.withList(Collections::emptyList)
.withResult(Collections::emptyList)
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Person person = converter.toInstance(dynamic);
Expand All @@ -61,7 +61,7 @@ public void shouldReturnNullAsInstance() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(Collections::emptyList)
.withResult(Collections::emptyList)
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Person person = converter.toInstance(dynamic);
Expand All @@ -75,7 +75,7 @@ public void shouldReturnOptional() {
dynamic = DynamicReturn.builder()
.withSingleResult(() -> Optional.of(ada))
.withClassSource(Person.class)
.withList(Collections::emptyList)
.withResult(Collections::emptyList)
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Optional<Person> person = converter.toOptional(dynamic);
Expand All @@ -90,7 +90,7 @@ public void shouldReturnList() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
List<Person> person = converter.toList(dynamic);
Expand All @@ -105,7 +105,7 @@ public void shouldReturnSet() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Set<Person> person = converter.toSet(dynamic);
Expand All @@ -121,7 +121,7 @@ public void shouldReturnLinkedList() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
LinkedList<Person> person = converter.toLinkedList(dynamic);
Expand All @@ -136,7 +136,7 @@ public void shouldReturnTreeSet() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
TreeSet<Person> person = converter.toTreeSet(dynamic);
Expand All @@ -151,7 +151,7 @@ public void shouldReturnStream() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Stream<Person> person = converter.toStream(dynamic);
Expand All @@ -166,7 +166,7 @@ public void shouldReturnDefault() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Object person = converter.toDefault(dynamic);
Expand All @@ -180,7 +180,7 @@ public void shouldReturnErrorWhenUsePage() {
dynamic = DynamicReturn.builder()
.withSingleResult(Optional::empty)
.withClassSource(Person.class)
.withList(() -> Collections.singletonList(ada))
.withResult(() -> Collections.singletonList(ada))
.withMethodSource(Person.class.getDeclaredMethods()[0])
.build();
Assertions.assertThrows(DynamicQueryException.class, () -> converter.toPage(dynamic));
Expand Down
Expand Up @@ -67,7 +67,7 @@ public void shouldReturnEmptyOptional() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down Expand Up @@ -96,7 +96,7 @@ public void shouldReturnOptional() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down Expand Up @@ -124,7 +124,7 @@ public void shouldReturnAnInstance() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -151,7 +151,7 @@ public void shouldReturnNull() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -178,7 +178,7 @@ public void shouldReturnList() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down Expand Up @@ -206,7 +206,7 @@ public void shouldReturnIterable() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down Expand Up @@ -234,7 +234,7 @@ public void shouldReturnCollection() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -261,7 +261,7 @@ public void shouldReturnSet() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -288,7 +288,7 @@ public void shouldReturnQueue() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down Expand Up @@ -316,7 +316,7 @@ public void shouldReturnStream() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -342,7 +342,7 @@ public void shouldReturnSortedSet() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -369,7 +369,7 @@ public void shouldReturnNavigableSet() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down Expand Up @@ -397,7 +397,7 @@ public void shouldReturnDeque() throws NoSuchMethodException {

DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand All @@ -421,7 +421,7 @@ public void shouldReturnErrorWhenExecutePage() throws NoSuchMethodException {
Pagination pagination = getPagination();
DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(Person.class)
.withMethodSource(method).withList(stream)
.withMethodSource(method).withResult(stream)
.withSingleResult(singleResult)
.withPagination(pagination)
.withListPagination(listPagination)
Expand Down

Large diffs are not rendered by default.

0 comments on commit 5f39f2a

Please sign in to comment.