use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class NumberToDataSizeConverterTests method convert.
@SuppressWarnings({ "rawtypes", "unchecked" })
private DataSize convert(ConversionService conversionService, Integer source, DataUnit defaultUnit) {
TypeDescriptor targetType = mock(TypeDescriptor.class);
if (defaultUnit != null) {
DataSizeUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", defaultUnit), DataSizeUnit.class, null);
given(targetType.getAnnotation(DataSizeUnit.class)).willReturn(unitAnnotation);
}
given(targetType.getType()).willReturn((Class) DataSize.class);
return (DataSize) conversionService.convert(source, TypeDescriptor.fromObject(source), targetType);
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class MockPeriodTypeDescriptor method get.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static TypeDescriptor get(ChronoUnit unit, PeriodStyle style) {
TypeDescriptor descriptor = mock(TypeDescriptor.class);
if (unit != null) {
PeriodUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", unit), PeriodUnit.class, null);
given(descriptor.getAnnotation(PeriodUnit.class)).willReturn(unitAnnotation);
}
if (style != null) {
PeriodFormat formatAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", style), PeriodFormat.class, null);
given(descriptor.getAnnotation(PeriodFormat.class)).willReturn(formatAnnotation);
}
given(descriptor.getType()).willReturn((Class) Period.class);
given(descriptor.getObjectType()).willReturn((Class) Period.class);
return descriptor;
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class CollectionToCollectionConverter method convert.
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
Collection<?> sourceCollection = (Collection<?>) source;
// Shortcut if possible...
boolean copyRequired = !targetType.getType().isInstance(source);
if (!copyRequired && sourceCollection.isEmpty()) {
return source;
}
TypeDescriptor elementDesc = targetType.getElementDescriptor();
if (elementDesc == null && !copyRequired) {
return source;
}
// At this point, we need a collection copy in any case, even if just for finding out about element copies...
Collection<Object> target = CollectionUtils.createCollection(targetType.getType(), (elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());
if (elementDesc == null) {
target.addAll(sourceCollection);
} else {
for (Object sourceElement : sourceCollection) {
Object targetElement = conversionService.convert(sourceElement, sourceType.elementDescriptor(sourceElement), elementDesc);
target.add(targetElement);
if (sourceElement != targetElement) {
copyRequired = true;
}
}
}
return (copyRequired ? target : source);
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class StringToArrayConverter method convert.
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
String string = (String) source;
String[] fields = StringUtils.commaDelimitedListToStringArray(string);
TypeDescriptor targetElementType = targetType.getElementDescriptor();
Assert.state(targetElementType != null, "No target element type");
Object target = Array.newInstance(targetElementType.getType(), fields.length);
for (int i = 0; i < fields.length; i++) {
String sourceElement = fields[i];
Object targetElement = conversionService.convert(sourceElement.trim(), sourceType, targetElementType);
Array.set(target, i, targetElement);
}
return target;
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class StreamConverter method convertFromStream.
@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.getElementDescriptor());
return conversionService.convert(content, listType, targetType);
}
Aggregations