Home / Class/ CollectionToDelimitedStringConverter Class — spring-boot Architecture

CollectionToDelimitedStringConverter Class — spring-boot Architecture

Architecture documentation for the CollectionToDelimitedStringConverter class in CollectionToDelimitedStringConverter.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/main/java/org/springframework/boot/convert/CollectionToDelimitedStringConverter.java lines 36–88

final class CollectionToDelimitedStringConverter implements ConditionalGenericConverter {

	private final ConversionService conversionService;

	CollectionToDelimitedStringConverter(ConversionService conversionService) {
		this.conversionService = conversionService;
	}

	@Override
	public Set<ConvertiblePair> getConvertibleTypes() {
		return Collections.singleton(new ConvertiblePair(Collection.class, String.class));
	}

	@Override
	public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
		TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
		if (targetType == null || sourceElementType == null) {
			return true;
		}
		return this.conversionService.canConvert(sourceElementType, targetType)
				|| sourceElementType.getType().isAssignableFrom(targetType.getType());
	}

	@Override
	@Contract("!null, _, _ -> !null")
	public @Nullable Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
		if (source == null) {
			return null;
		}
		Collection<?> sourceCollection = (Collection<?>) source;
		return convert(sourceCollection, sourceType, targetType);
	}

	private Object convert(Collection<?> source, TypeDescriptor sourceType, TypeDescriptor targetType) {
		if (source.isEmpty()) {
			return "";
		}
		return source.stream()
			.map((element) -> convertElement(element, sourceType, targetType))
			.collect(Collectors.joining(getDelimiter(sourceType)));
	}

	private CharSequence getDelimiter(TypeDescriptor sourceType) {
		Delimiter annotation = sourceType.getAnnotation(Delimiter.class);
		return (annotation != null) ? annotation.value() : ",";
	}

	private String convertElement(Object element, TypeDescriptor sourceType, TypeDescriptor targetType) {
		return String
			.valueOf(this.conversionService.convert(element, sourceType.elementTypeDescriptor(element), targetType));
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free