org.modelmapper.Converter Java Examples

The following examples show how to use org.modelmapper.Converter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConverterConfigurerSupportTest.java    From modelmapper-spring-boot-starter with Apache License 2.0 7 votes vote down vote up
@Bean
public ConverterConfigurerSupport<User, UserDto> userConverter() {
    return new ConverterConfigurerSupport<User, UserDto>() {
        @Override
        protected Converter<User, UserDto> converter() {
            return new AbstractConverter<User, UserDto>() {

                @Override
                protected UserDto convert(User source) {
                    String[] parts = source.getName().split(" ");
                    return new UserDto(parts[0], parts[1]);
                }
            };
        }
    };
}
 
Example #2
Source File: ProductMapper.java    From research-graphql with MIT License 6 votes vote down vote up
public ProductMapper() {
    // Converters for complex field conversions
    // Converter<LocalDate, LocalDateTime> convertLocalDate2LocalDateTime = context -> context.getSource() == null ? null :
    //                context.getSource().atStartOfDay();
    // Converter<LocalDateTime, LocalDate> convertLocalDateTime2LocalDate = context -> context.getSource() == null ? null :
    //                context.getSource().toLocalDate();
    Converter<List<Category>, List<String>> convertCategory2Name = context -> context.getSource() == null ? null :
            context.getSource().stream().map(Category::getName).collect(Collectors.toList());
    Converter<List<String>, List<Category>> convertCategoryName2Category = context -> context.getSource() == null ? null :
            context.getSource().stream().map((name) -> Category.builder().name(name).build()).collect(Collectors.toList());
    // create PropertyMap<source,target> here and add to mapper
    // map().set<Target>(source.get<Source>());
    // map(source.get<Source>(), destination.get<Target>());
    mapper.addMappings(new PropertyMap<Product, ProductDto>() {
        @Override
        protected void configure() {
            using(convertCategory2Name).map(source.getCategories(), destination.getCategory());
        }
    });
    mapper.addMappings(new PropertyMap<ProductDto, Product>() {
        @Override
        protected void configure() {
            using(convertCategoryName2Category).map(source.getCategory(), destination.getCategories());
        }
    });
}
 
Example #3
Source File: UserMapper.java    From research-graphql with MIT License 5 votes vote down vote up
public UserMapper() {
    // Converters for complex field conversions
    Converter<LocalDate, LocalDateTime> convertLocalDate2LocalDateTime = context -> context.getSource() == null ? null :
            context.getSource().atStartOfDay();
    Converter<LocalDateTime, LocalDate> convertLocalDateTime2LocalDate = context -> context.getSource() == null ? null :
            context.getSource().toLocalDate();
    // add PropertyMap<source,target> for unnatural field mappings
    // map().set<Target>(source.get<Source>());
    // map(source.get<Source>(), destination.get<Target>());
    mapper.addMappings(new PropertyMap<User, UserDto>() {
        @Override
        protected void configure() {
            map().setGivenName(source.getFirstname());
            map().setFamilyName(source.getLastname());
            using(convertLocalDate2LocalDateTime).map(source.getDob(), destination.getBday());
        }
    });
    mapper.addMappings(new PropertyMap<UserDto, User>() {
        @Override
        protected void configure() {
            map().setFirstname(source.getGivenName());
            map().setLastname(source.getFamilyName());
            using(convertLocalDateTime2LocalDate).map(source.getBday(), destination.getDob());
        }
    });
    // add mapper.getTypeMap(<source>, <target>) pre/post conversion logic
}
 
Example #4
Source File: WebConfig.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public ModelMapper modelMapper(List<Converter> converters) {
    ModelMapper mapper = new ModelMapper();

    converters.stream().forEach(mapper::addConverter);

    return mapper;
}
 
Example #5
Source File: ModelMapperAutoConfiguration.java    From building-microservices with Apache License 2.0 5 votes vote down vote up
public ModelMapperAutoConfiguration(ModelMapperProperties properties,
		ObjectProvider<List<PropertyMap<?, ?>>> mappings,
		ObjectProvider<List<Converter<?, ?>>> converters) {
	this.properties = properties;
	this.mappings = mappings.getIfAvailable();
	this.converters = converters.getIfAvailable();
}
 
Example #6
Source File: ModelMapperAutoConfiguration.java    From building-microservices with Apache License 2.0 5 votes vote down vote up
private void addConverters(ModelMapper modelMapper) {
	if (this.properties.isAddConverters() && this.converters != null) {
		for (Converter<?, ?> converter : this.converters) {
			modelMapper.addConverter(converter);
		}
	}
}
 
Example #7
Source File: ConverterConfigurerSupport.java    From modelmapper-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Allows to specify a custom converter between two types.
 *
 * @return the converter
 */
protected abstract Converter<S,D> converter();