#1335 - Support for Supplier<Stream<?> expansion in EmbeddedWrappers.

This allows types like Spring Data's Streamable to be piped into the ….wrap(…) methods and immediately resolved into a collection.
This commit is contained in:
Oliver Drotbohm
2020-07-28 22:28:40 +02:00
parent 4720e563f0
commit 6453d25263
2 changed files with 44 additions and 3 deletions

View File

@@ -19,10 +19,12 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.ResolvableType;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.LinkRelation;
import org.springframework.lang.NonNull;
@@ -36,6 +38,8 @@ import org.springframework.util.Assert;
*/
public class EmbeddedWrappers {
private static ResolvableType SUPPLIER_OF_STREAM = ResolvableType.forClassWithGenerics(Supplier.class, Stream.class);
private final boolean preferCollections;
/**
@@ -83,9 +87,12 @@ public class EmbeddedWrappers {
return (EmbeddedWrapper) source;
}
return source instanceof Collection || source instanceof Stream || preferCollections
? new EmbeddedCollection(asCollection(source), rel)
: new EmbeddedElement(source, rel);
return source instanceof Collection //
|| source instanceof Stream //
|| preferCollections //
|| SUPPLIER_OF_STREAM.isAssignableFrom(source.getClass()) //
? new EmbeddedCollection(asCollection(source), rel) //
: new EmbeddedElement(source, rel);
}
@SuppressWarnings("unchecked")
@@ -107,6 +114,10 @@ public class EmbeddedWrappers {
return Arrays.asList((Object[]) source);
}
if (SUPPLIER_OF_STREAM.isInstance(source)) {
return asCollection(((Supplier<Stream<?>>) source).get());
}
return Collections.singleton(source);
}