S
S
Sland Show2018-12-24 17:30:41
Java
Sland Show, 2018-12-24 17:30:41

How to replace bulky code with lambdas or make it more beautiful?

I have a mapper that converts real entities from a base to a DTO. The problem is that the tables are nested.

To be precise, I have 5 tables that are interconnected like this:

5c20ed1444744348762085.png
And that's exactly what I eventually need to map entity A to DTO. To do this, I wrote a very cumbersome mapper:

public AssuranceAccessLineDto mapperToAssuranceAccessLineDto(AccessLine accessLine) {
        return modelMapper.map(accessLine, AssuranceAccessLineDto.class)
                .setId(accessLine.getId())
                .setTechnology(accessLine.getTechnology().name())
                .setStatus(accessLine.getStatus().name())
                .setLineId(accessLine.getLineId())
                .setPortId(accessLine.getPort().getId())
                .setAssuranceNeProfileDto(
                        accessLine.getDefaultNeProfile() == null ?
                                null : mapperToAssuranceDefaultNeProfileDto(accessLine.getDefaultNeProfile())
                )
                .setAssuranceSubscriberNeProfileDto(
                        (accessLine.getDefaultNeProfile() == null || accessLine.getDefaultNeProfile().getSubscriberNeProfile() == null) ?
                                null : mapperToAssuranceSubscriberNeProfileDto(
                                accessLine.getDefaultNeProfile().getSubscriberNeProfile()
                        )
                )
                .setAssuranceDefaultNetworkLineProfileDto(
                        accessLine.getDefaultNetworkLineProfile() == null ?
                                null : mapperToAssuranceDefaultNetworkLineProfileDto(
                                accessLine.getDefaultNetworkLineProfile()
                        )

                )
                .setAssuranceSubscriberNetworkLineProfileDto(
                        (accessLine.getDefaultNetworkLineProfile() == null || accessLine.getDefaultNetworkLineProfile().getSubscriberNetworkLineProfile() == null) ?
                                null : mapperToAssuranceSubscriberNetworkLineProfileDto(
                                accessLine.getDefaultNetworkLineProfile().getSubscriberNetworkLineProfile()
                        )

                );
    }


My question is this. Can I make these mapper setters prettier and more readable via lambdas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-12-24
@SlandShow

No, lambdas are not for that.
Advice - this (and similar) garbage

accessLine.getDefaultNeProfile() == null || accessLine.getDefaultNeProfile().getSubscriberNeProfile() == null) ?
                                null : mapperToAssuranceSubscriberNeProfileDto(
                                accessLine.getDefaultNeProfile().getSubscriberNeProfile()

put in a separate method with a normal name. It will immediately make life easier. Well, between two accesses, the internal value may change, and you will catch NPE. Save to a variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question