下面列出了org.springframework.core.convert.TypeDescriptor#collection ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Object convertToStream(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor streamType) {
TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection);
if (target == null) {
target = Collections.emptyList();
}
return target.stream();
}
@Override
public <S, T> List<T> convert(List<S> collection, Class<S> sourceType, Class<T> targetType) {
TypeDescriptor sourceTypeDesc = TypeDescriptor.collection(collection.getClass(), TypeDescriptor.valueOf(sourceType));
TypeDescriptor targetTypeDesc = TypeDescriptor.collection(collection.getClass(), TypeDescriptor.valueOf(targetType));
@SuppressWarnings("unchecked")
List<T> returnList = (List<T>) convert(collection, sourceTypeDesc, targetTypeDesc);
return returnList;
}
private List<PageUploadData> transformListToPageUploadData(List<UploadData> paginatedList) {
TypeDescriptor source = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(UploadData.class));
TypeDescriptor target = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(PageUploadData.class));
@SuppressWarnings("unchecked")
List<PageUploadData> returnList = (List<PageUploadData>) conversionService.convert(paginatedList, source, target);
return returnList;
}
private Object convertToStream(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor streamType) {
TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection);
if (target == null) {
target = Collections.emptyList();
}
return target.stream();
}
private List<String> iterableToList(final Object expressionValue) {
final TypeDescriptor genericCollection = TypeDescriptor.collection(Collection.class, TypeDescriptor.valueOf(Object.class));
final TypeDescriptor stringList = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
//noinspection unchecked
return (List<String>) conversionService.convert(expressionValue, genericCollection, stringList);
}
private List<String> arrayToList(final Object expressionValue) {
final TypeDescriptor genericArray = TypeDescriptor.array(TypeDescriptor.valueOf(Object.class));
final TypeDescriptor stringList = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
//noinspection unchecked
return (List<String>) conversionService.convert(expressionValue, genericArray, stringList);
}
@Nullable
private Object convertFromStream(@Nullable Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) {
List<Object> content = (source != null ? source.collect(Collectors.<Object>toList()) : Collections.emptyList());
TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
return this.conversionService.convert(content, listType, targetType);
}
@Nullable
private Object convertFromStream(@Nullable Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) {
List<Object> content = (source != null ? source.collect(Collectors.<Object>toList()) : Collections.emptyList());
TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
return this.conversionService.convert(content, listType, targetType);
}
private Object convertFromStream(Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) {
List<Object> content = source.collect(Collectors.<Object>toList());
TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
return this.conversionService.convert(content, listType, targetType);
}
private Object convertToStream(Object source, TypeDescriptor sourceType, TypeDescriptor streamType) {
TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection);
return target.stream();
}
private Object convertFromStream(Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) {
List<Object> content = source.collect(Collectors.<Object>toList());
TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
return this.conversionService.convert(content, listType, targetType);
}
private Object convertToStream(Object source, TypeDescriptor sourceType, TypeDescriptor streamType) {
TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection);
return target.stream();
}
/**
* Validate that a {@link Collection} of the elements held within the stream can be
* converted to the specified {@code targetType}.
* @param elementType the type of the stream elements
* @param targetType the type to convert to
*/
public boolean matchesFromStream(@Nullable TypeDescriptor elementType, TypeDescriptor targetType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(collectionOfElement, targetType);
}
/**
* Validate that the specified {@code sourceType} can be converted to a {@link Collection} of
* the type of the stream elements.
* @param elementType the type of the stream elements
* @param sourceType the type to convert from
*/
public boolean matchesToStream(@Nullable TypeDescriptor elementType, TypeDescriptor sourceType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(sourceType, collectionOfElement);
}
/**
* Validate that a {@link Collection} of the elements held within the stream can be
* converted to the specified {@code targetType}.
* @param elementType the type of the stream elements
* @param targetType the type to convert to
*/
public boolean matchesFromStream(@Nullable TypeDescriptor elementType, TypeDescriptor targetType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(collectionOfElement, targetType);
}
/**
* Validate that the specified {@code sourceType} can be converted to a {@link Collection} of
* the type of the stream elements.
* @param elementType the type of the stream elements
* @param sourceType the type to convert from
*/
public boolean matchesToStream(@Nullable TypeDescriptor elementType, TypeDescriptor sourceType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(sourceType, collectionOfElement);
}
/**
* Validate that a {@link Collection} of the elements held within the stream can be
* converted to the specified {@code targetType}.
* @param elementType the type of the stream elements
* @param targetType the type to convert to
*/
public boolean matchesFromStream(TypeDescriptor elementType, TypeDescriptor targetType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(collectionOfElement, targetType);
}
/**
* Validate that the specified {@code sourceType} can be converted to a {@link Collection} of
* the type of the stream elements.
* @param elementType the type of the stream elements
* @param sourceType the type to convert from
*/
public boolean matchesToStream(TypeDescriptor elementType, TypeDescriptor sourceType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(sourceType, collectionOfElement);
}
/**
* Validate that a {@link Collection} of the elements held within the stream can be
* converted to the specified {@code targetType}.
* @param elementType the type of the stream elements
* @param targetType the type to convert to
*/
public boolean matchesFromStream(TypeDescriptor elementType, TypeDescriptor targetType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(collectionOfElement, targetType);
}
/**
* Validate that the specified {@code sourceType} can be converted to a {@link Collection} of
* the type of the stream elements.
* @param elementType the type of the stream elements
* @param sourceType the type to convert from
*/
public boolean matchesToStream(TypeDescriptor elementType, TypeDescriptor sourceType) {
TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
return this.conversionService.canConvert(sourceType, collectionOfElement);
}