Skip to content

Instantly share code, notes, and snippets.

@taljacob2
Created October 8, 2021 16:08
Show Gist options
  • Select an option

  • Save taljacob2/86c096af1b12b7caaa79109f99319ca1 to your computer and use it in GitHub Desktop.

Select an option

Save taljacob2/86c096af1b12b7caaa79109f99319ca1 to your computer and use it in GitHub Desktop.

Revisions

  1. taljacob2 created this gist Oct 8, 2021.
    34 changes: 34 additions & 0 deletions Mapper.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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());
    }

    }