Search in sources :

Example 1 with ConvertiblePair

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));
}
Also used : ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)

Example 2 with ConvertiblePair

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);
}
Also used : ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)

Example 3 with ConvertiblePair

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;
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)

Example 4 with ConvertiblePair

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());
}
Also used : ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)

Example 5 with ConvertiblePair

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());
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Function(java.util.function.Function) Value(lombok.Value) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AccessLevel(lombok.AccessLevel) Optionals(org.springframework.data.util.Optionals) Map(java.util.Map) GenericConverter(org.springframework.core.convert.converter.GenericConverter) GenericTypeResolver(org.springframework.core.GenericTypeResolver) LinkedHashSet(java.util.LinkedHashSet) Converter(org.springframework.core.convert.converter.Converter) SimpleTypeHolder(org.springframework.data.mapping.model.SimpleTypeHolder) NonNull(lombok.NonNull) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConverterFactory(org.springframework.core.convert.converter.ConverterFactory) Set(java.util.Set) ConverterRegistry(org.springframework.core.convert.converter.ConverterRegistry) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Streamable(org.springframework.data.util.Streamable) Optional(java.util.Optional) ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair) Collections(java.util.Collections) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) Assert(org.springframework.util.Assert) ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)

Aggregations

ConvertiblePair (org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)6 GenericConverter (org.springframework.core.convert.converter.GenericConverter)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 AccessLevel (lombok.AccessLevel)1 Getter (lombok.Getter)1 NonNull (lombok.NonNull)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Value (lombok.Value)1 Slf4j (lombok.extern.slf4j.Slf4j)1