Search in sources :

Example 1 with GenericConverter

use of cn.taketoday.core.conversion.GenericConverter in project today-infrastructure by TAKETODAY.

the class ConversionServiceFactoryBeanTests method createDefaultConversionServiceWithSupplements.

@Test
public void createDefaultConversionServiceWithSupplements() {
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    Set<Object> converters = new HashSet<>();
    converters.add(new Converter<String, Foo>() {

        @Override
        public Foo convert(String source) {
            return new Foo();
        }
    });
    converters.add(new ConverterFactory<String, Bar>() {

        @Override
        public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
            return new Converter<>() {

                @SuppressWarnings("unchecked")
                @Override
                public T convert(String source) {
                    return (T) new Bar();
                }
            };
        }
    });
    converters.add(new GenericConverter() {

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

        @Override
        @Nullable
        public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            return new Baz();
        }
    });
    factory.setConverters(converters);
    factory.afterPropertiesSet();
    ConversionService service = factory.getObject();
    assertThat(service.canConvert(String.class, Integer.class)).isTrue();
    assertThat(service.canConvert(String.class, Foo.class)).isTrue();
    assertThat(service.canConvert(String.class, Bar.class)).isTrue();
    assertThat(service.canConvert(String.class, Baz.class)).isTrue();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericConverter(cn.taketoday.core.conversion.GenericConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionService(cn.taketoday.core.conversion.ConversionService) Converter(cn.taketoday.core.conversion.Converter) GenericConverter(cn.taketoday.core.conversion.GenericConverter) Nullable(cn.taketoday.lang.Nullable) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with GenericConverter

use of cn.taketoday.core.conversion.GenericConverter in project today-infrastructure by TAKETODAY.

the class GenericConversionServiceTests method shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter.

@Test
void shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter() {
    GenericConverter converter = new NonConditionalGenericConverter();
    assertThatIllegalStateException().isThrownBy(() -> conversionService.addConverter(converter)).withMessage("Only conditional converters may return null convertible types");
}
Also used : GenericConverter(cn.taketoday.core.conversion.GenericConverter) Test(org.junit.jupiter.api.Test)

Example 3 with GenericConverter

use of cn.taketoday.core.conversion.GenericConverter in project today-framework by TAKETODAY.

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
 */
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(cn.taketoday.core.conversion.GenericConverter) ConvertiblePair(cn.taketoday.core.conversion.GenericConverter.ConvertiblePair)

Example 4 with GenericConverter

use of cn.taketoday.core.conversion.GenericConverter in project today-framework by TAKETODAY.

the class ConversionServiceFactoryBeanTests method createDefaultConversionServiceWithSupplements.

@Test
public void createDefaultConversionServiceWithSupplements() {
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    Set<Object> converters = new HashSet<>();
    converters.add(new Converter<String, Foo>() {

        @Override
        public Foo convert(String source) {
            return new Foo();
        }
    });
    converters.add(new ConverterFactory<String, Bar>() {

        @Override
        public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
            return new Converter<>() {

                @SuppressWarnings("unchecked")
                @Override
                public T convert(String source) {
                    return (T) new Bar();
                }
            };
        }
    });
    converters.add(new GenericConverter() {

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

        @Override
        @Nullable
        public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            return new Baz();
        }
    });
    factory.setConverters(converters);
    factory.afterPropertiesSet();
    ConversionService service = factory.getObject();
    assertThat(service.canConvert(String.class, Integer.class)).isTrue();
    assertThat(service.canConvert(String.class, Foo.class)).isTrue();
    assertThat(service.canConvert(String.class, Bar.class)).isTrue();
    assertThat(service.canConvert(String.class, Baz.class)).isTrue();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericConverter(cn.taketoday.core.conversion.GenericConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionService(cn.taketoday.core.conversion.ConversionService) Converter(cn.taketoday.core.conversion.Converter) GenericConverter(cn.taketoday.core.conversion.GenericConverter) Nullable(cn.taketoday.lang.Nullable) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with GenericConverter

use of cn.taketoday.core.conversion.GenericConverter in project today-framework by TAKETODAY.

the class GenericConversionServiceTests method shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter.

@Test
void shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter() {
    GenericConverter converter = new NonConditionalGenericConverter();
    assertThatIllegalStateException().isThrownBy(() -> conversionService.addConverter(converter)).withMessage("Only conditional converters may return null convertible types");
}
Also used : GenericConverter(cn.taketoday.core.conversion.GenericConverter) Test(org.junit.jupiter.api.Test)

Aggregations

GenericConverter (cn.taketoday.core.conversion.GenericConverter)7 Test (org.junit.jupiter.api.Test)4 TypeDescriptor (cn.taketoday.core.TypeDescriptor)3 ConversionService (cn.taketoday.core.conversion.ConversionService)2 Converter (cn.taketoday.core.conversion.Converter)2 ConvertiblePair (cn.taketoday.core.conversion.GenericConverter.ConvertiblePair)2 Nullable (cn.taketoday.lang.Nullable)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2