Created
November 2, 2018 21:09
-
-
Save sjaakd/50c758f0a1ccea9453e35bbdbab96627 to your computer and use it in GitHub Desktop.
exploding lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class Course { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class CourseDto { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.List; | |
import org.mapstruct.Mapper; | |
@Mapper | |
public interface CourseMapper { | |
CourseDto map( Course in ); | |
List<CourseDto> map( List<Course> in ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class Program { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.List; | |
public class ProgramDto { | |
private List<CourseDto> courses; | |
public List<CourseDto> getCourses() { | |
return courses; | |
} | |
public void setCourses(List<CourseDto> courses) { | |
this.courses = courses; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.mapstruct.Mapper; | |
@Mapper | |
public interface ProgramMapper { | |
ProgramDto map( Program program ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.List; | |
public class Student { | |
private String name; | |
private List<Course> courses; | |
private List<Program> programs; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public List<Course> getCourses() { | |
return courses; | |
} | |
public void setCourses(List<Course> courses) { | |
this.courses = courses; | |
} | |
public List<Program> getPrograms() { | |
return programs; | |
} | |
public void setPrograms(List<Program> programs) { | |
this.programs = programs; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.List; | |
public class StudentDto { | |
private String studentName; | |
private List<ProgramDto> programs; | |
public String getStudentName() { | |
return studentName; | |
} | |
public void setStudentName(String studentName) { | |
this.studentName = studentName; | |
} | |
public List<ProgramDto> getPrograms() { | |
return programs; | |
} | |
public void setPrograms(List<ProgramDto> programs) { | |
this.programs = programs; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.mapstruct.AfterMapping; | |
import org.mapstruct.Context; | |
import org.mapstruct.Mapper; | |
import org.mapstruct.Mapping; | |
import org.mapstruct.MappingTarget; | |
@Mapper( uses = ProgramMapper.class ) | |
public interface StudentMapper { | |
@Mapping( target = "studentName", source = "name" ) | |
@Mapping( target = "programs", source = "programs" ) // strictly not needed, same name | |
StudentDto map( Student student, @Context CourseMapper courseMapper ); | |
@AfterMapping | |
default void map(Student student, @MappingTarget StudentDto studentDto, @Context CourseMapper courseMapper) { | |
for (ProgramDto programDto : studentDto.getPrograms() ) { | |
programDto.setCourses( courseMapper.map( student.getCourses() ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment