33 lines
888 B
Java
33 lines
888 B
Java
package com.baeldung.mapper;
|
|
|
|
import org.mapstruct.AfterMapping;
|
|
import org.mapstruct.BeforeMapping;
|
|
import org.mapstruct.Mapper;
|
|
import org.mapstruct.MappingTarget;
|
|
|
|
import com.baeldung.dto.CarDTO;
|
|
import com.baeldung.dto.FuelType;
|
|
import com.baeldung.entity.BioDieselCar;
|
|
import com.baeldung.entity.Car;
|
|
import com.baeldung.entity.ElectricCar;
|
|
|
|
@Mapper
|
|
public abstract class CarsMapper {
|
|
|
|
@BeforeMapping
|
|
protected void enrichDTOWithFuelType(Car car, @MappingTarget CarDTO carDto) {
|
|
if (car instanceof ElectricCar)
|
|
carDto.setFuelType(FuelType.ELECTRIC);
|
|
if (car instanceof BioDieselCar)
|
|
carDto.setFuelType(FuelType.BIO_DIESEL);
|
|
}
|
|
|
|
@AfterMapping
|
|
protected void convertNameToUpperCase(@MappingTarget CarDTO carDto) {
|
|
carDto.setName(carDto.getName().toUpperCase());
|
|
}
|
|
|
|
public abstract CarDTO toCarDto(Car car);
|
|
|
|
}
|