use of org.springframework.core.convert.converter.GenericConverter.ConvertiblePair in project spring-data-commons by spring-projects.
the class ConverterBuilder method reading.
/**
* Creates a new {@link ReadingConverterBuilder} to produce a converter to read values of the given source (the store
* type) into the given target (the domain type).
*
* @param source must not be {@literal null}.
* @param target must not be {@literal null}.
* @param function must not be {@literal null}.
* @return
*/
static <S, T> ReadingConverterBuilder<S, T> reading(Class<S> source, Class<T> target, Function<? super S, ? extends T> function) {
Assert.notNull(source, "Source type must not be null!");
Assert.notNull(target, "Target type must not be null!");
Assert.notNull(function, "Conversion function must not be null!");
return new DefaultConverterBuilder<>(new ConvertiblePair(source, target), Optional.empty(), Optional.of(function));
}
use of org.springframework.core.convert.converter.GenericConverter.ConvertiblePair in project spring-data-commons by spring-projects.
the class ConverterBuilderUnitTests method assertConverter.
private static void assertConverter(GenericConverter converter, Object source, Object target) {
assertThat(converter.getConvertibleTypes()).containsExactly(new ConvertiblePair(source.getClass(), target.getClass()));
assertThat(converter.convert(source, TypeDescriptor.forObject(source), TypeDescriptor.forObject(target))).isEqualTo(target);
}
use of org.springframework.core.convert.converter.GenericConverter.ConvertiblePair in project spring-boot by spring-projects.
the class ApplicationConversionService method isConvertViaObjectSourceType.
/**
* Return {@code true} if objects of {@code sourceType} can be converted to the
* {@code targetType} and the converter has {@code Object.class} as a supported source
* type.
* @param sourceType the source type to test
* @param targetType the target type to test
* @return if conversion happens via an {@code ObjectTo...} converter
* @since 2.4.3
*/
public boolean isConvertViaObjectSourceType(TypeDescriptor sourceType, TypeDescriptor targetType) {
GenericConverter converter = getConverter(sourceType, targetType);
Set<ConvertiblePair> pairs = (converter != null) ? converter.getConvertibleTypes() : null;
if (pairs != null) {
for (ConvertiblePair pair : pairs) {
if (Object.class.equals(pair.getSourceType())) {
return true;
}
}
}
return false;
}
use of org.springframework.core.convert.converter.GenericConverter.ConvertiblePair in project spring-data-commons by spring-projects.
the class ConverterBuilder method writing.
/**
* Creates a new {@link WritingConverterBuilder} to produce a converter to write values of the given source (the
* domain type) into the given target (the store type).
*
* @param source must not be {@literal null}.
* @param target must not be {@literal null}.
* @param function must not be {@literal null}.
* @return
*/
static <S, T> WritingConverterBuilder<S, T> writing(Class<S> source, Class<T> target, Function<? super S, ? extends T> function) {
Assert.notNull(source, "Source type must not be null!");
Assert.notNull(target, "Target type must not be null!");
Assert.notNull(function, "Conversion function must not be null!");
return new DefaultConverterBuilder<>(new ConvertiblePair(target, source), Optional.of(function), Optional.empty());
}
use of org.springframework.core.convert.converter.GenericConverter.ConvertiblePair in project spring-data-commons by spring-projects.
the class CustomConversions method getCustomTarget.
/**
* Inspects the given {@link ConvertiblePair}s for ones that have a source compatible type as source. Additionally
* checks assignability of the target type if one is given.
*
* @param sourceType must not be {@literal null}.
* @param targetType can be {@literal null}.
* @param pairs must not be {@literal null}.
* @return
*/
private Optional<Class<?>> getCustomTarget(Class<?> sourceType, Optional<Class<?>> targetType, Collection<ConvertiblePair> pairs) {
Assert.notNull(sourceType, "Source Class must not be null!");
Assert.notNull(pairs, "Collection of ConvertiblePair must not be null!");
return //
Optionals.firstNonEmpty(//
() -> targetType.filter(it -> pairs.contains(new ConvertiblePair(sourceType, it))), () -> //
pairs.stream().filter(//
it -> hasAssignableSourceType(it, sourceType)).<Class<?>>map(//
ConvertiblePair::getTargetType).filter(//
it -> requestTargetTypeIsAssignable(targetType, it)).findFirst());
}
Aggregations