Skip to content
Advertisement

MapStruct Java: property to list

Shortly, I’d like to move this code inside a mapstruct mapper:

List<Provincia> provincies = resultSetType.getResults().getResult().stream()
    .map(resultType -> ResultTypeToProvinciaMapper.INSTANCE.resultTypeToProvincia(resultType))
    .collect(Collectors.toList());

I’d like to have:

List<Provincia> provinces = ResultTypeToProvinciaMapper.INSTANCE.getList(resultSet);

Details

My source class:

public class ResultSetType {

    protected SearchRequestType request;
    protected Results results;
    protected Long resultCount;
    protected Long totalCount;
    protected Long startIndex;
    protected Long pageSize;
    protected ResultSetType.Errors errors;

    // getters & setters

}

where Results is:

public static class Results {

    protected List<ResultType> result;

    // geters & setters

}

And ResultType:

public class ResultType {

    protected String id;
    protected String description;

    // getters & setters

}

My Service class is getting a ResultSetType object from my Repository:

@Service
@RequiredArgsConstructor
public class ServeiTerritorialServiceImpl implements ServeiTerritorialService {

    private final ServeiTerritorialClientRepository serveiTerritorialClientRepository;

    /**
     * {@inheritDoc}
     */
    @Override
    public void getPaisos() {
        ResultSetType resultSetType = this.serveiTerritorialClientRepository.getOid("2.16.724.4.400");

        // here I need to map resultSetType to a List<Provincia>...
    }

}

I need to map resultSetType to List<Provincia>.

So, I need to map resultSetType.results.result to List<Provincia>.

First of all, I’ve created a mapper in order to map ResultType to Provincia:

@Mapper
public interface ResultTypeToProvinciaMapper {
    
    ResultTypeToProvinciaMapper INSTANCE = Mappers.getMapper(ResultTypeToProvinciaMapper.class);
    
    @Mapping(source = "id", target = "code")
    @Mapping(source = "description", target = "name")
    Provincia resultTypeToProvincia(ResultType resultType);

}

However, I can’t quite figure out how to travel from resultSetType.results.result to List<Provincia>.

Any ideas?

Advertisement

Answer

With MapStruct you can define mapping between different iterable. However, you can map from a nested listed into a top level list in a method (You can if it is wrapped though).

In any case for this I would suggest doing the following:

@Mapper
public interface ResultTypeToProvinciaMapper {
    
    ResultTypeToProvinciaMapper INSTANCE = Mappers.getMapper(ResultTypeToProvinciaMapper.class);
    

    default List<Provincia> resultSetToListOfProvincia(ResultSetType resultSetType) {
        if (resultSetType == null) {
            return null;
        }

        Results results = resultSetType.getResults();

        if (results == null) {
            return null;
        }
        return resultTypesToProvincias(results.getResult());
    }


    List<Provincia> resultTypesToProvincias(List<ResultType> resultTypes);

    @Mapping(source = "id", target = "code")
    @Mapping(source = "description", target = "name")
    Provincia resultTypeToProvincia(ResultType resultType);

}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement