#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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user