Java.MapStruct.Tips

install

    implementation("org.mapstruct:mapstruct:1.3.1.Final")
    annotationProcessor("org.mapstruct:mapstruct-processor:1.3.1.Final")
    testAnnotationProcessor("org.mapstruct:mapstruct-processor:1.3.1.Final")
    implementation("org.projectlombok:lombok-mapstruct-binding:0.2.0")

mapper

public interface GameMapMapper {

    GameMapMapper INSTANCE = Mappers.getMapper(GameMapMapper.class);

//   add mapping if needed
//    @Mapping(target = "maxX", source = "maxX")
//    @Mapping(target = "maxY", source = "maxY")
    GameMapUi toMapDto(Map gameMap);

    GrassUi toGrassDto(Grass grass);

    AnimalUi toAnimalsDto(Animal animal);

}

example of call

        var gameMapMapper = GameMapMapper.INSTANCE;
        var map = gameMapMapper.toMapDto(gameContext.getState().getMap());

        var grass = gameContext.getState().getGrass().stream().map(
                g -> gameMapMapper.toGrassDto(g)
        ).collect(Collectors.toList());

        var animals = gameContext.getState().getAnimals().stream().map(
                animal -> gameMapMapper.toAnimalsDto(animal)
        ).collect(Collectors.toList());

        GameStateUi gameStateUi = new GameStateUi();
        gameStateUi.gameMap(map);
        gameStateUi.setGrass(grass);
        gameStateUi.setAnimals(animals);

with newer versions also faced with problems like this:

Internal error in the mapping processor: java.lang.RuntimeException: javax.annotation.processing.FilerException: Attempt to recreate a file for type com.example.main.life.mappers.GameMapMapperImpl  	at org.mapstruct.ap.internal.processor.
https://github.com/mapstruct/mapstruct/issues/1818
This entry was posted in Без рубрики. Bookmark the permalink.