Skip to content

Instantly share code, notes, and snippets.

@taljacob2
Created October 8, 2021 16:08
Show Gist options
  • Save taljacob2/86c096af1b12b7caaa79109f99319ca1 to your computer and use it in GitHub Desktop.
Save taljacob2/86c096af1b12b7caaa79109f99319ca1 to your computer and use it in GitHub Desktop.
This Component is used for mapping Objects. Mainly, for mapping DTOs to Entities / Entities to DTOs.
package com.tal.spring.bean.mapper;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
/**
* This {@link Component} is used for mapping {@code Object}s. Mainly, for
* mapping DTOs to Entities / Entities to DTOs.
* <p>
* Requires:
*
* <pre>
* <i>Gradle:</i>
* dependencies {
* <b> compileOnly 'org.modelmapper:modelmapper:2.3.5' </b>
* }
* </pre>
* </p>
*
* @author Tal Jacob
* @version 1.0
* @see org.modelmapper.ModelMapper
*/
@Component public class Mapper extends ModelMapper {
public <S, T> List<T> mapList(List<S> source, Class<T> targetClass) {
return source.stream().map(element -> this.map(element, targetClass))
.collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment