@turdubekov
Студент

Как правильно оформить код?

Есть объект, в котором завернуть другой объект, у которого есть лист объектов.
мне надо достать лист объектов и обогащать данными.
Вопрос: как правильно проверить лист на пустоту?
так как до листа надо обойти большую цепочку объектов.

код:
spoiler
public List<Receipt> buildTitle(List<ComServiceResponse> comServiceResponses) {

        List<Receipt> receipts = new ArrayList<>();

        for (ComServiceResponse comService : comServiceResponses) {
            
            // if list not empty
            if (comService != null && comService.getComment() != null && !comService.getComment().getComs().isEmpty()) {

                String date = calendarService.formatDate(comService.getPeriod());

                boolean paymentStatus = isPayed(comService.getComment().getComs());
                double totalAmount = calculateComServiceTotalAmount(comService.getComment().getComs());

                Receipt receipt = new Receipt();

                receipt.setPeriod(date);
                receipt.setPayed(paymentStatus);
                receipt.setTotalAmount(totalAmount);
                receipt.setAccount(comService.getAccount());
                receipt.setServiceIcon(properties.getMyVillageIcon());
                receipt.setName(comService.getComment().getComs().get(0).getOwner());
                receipt.setAddress(comService.getComment().getComs().get(0).getAddress());

                receipts.add(receipt);
            }
        }
        return receipts;
    }
  • Вопрос задан
  • 146 просмотров
Решения вопроса 1
xez
@xez Куратор тега Java
TL Junior Roo
Примерно так:
public List<Receipt> buildTitle(List<ComServiceResponse> comServiceResponses) {
return comServiceResponses.stream()
 .map(ComServiceResponse::getComment)
 .map(Comment::getComs)
 .filter(Objects::nonNull)
 .flatMap(List::stream) // Collection::stream ?
 .map(comService -> {   // todo:: extract to mapping method
                var date = calendarService.formatDate(comService.getPeriod()); // todo :: check for null here and below

                var paymentStatus = isPayed(comService.getComment().getComs()); 
                var totalAmount = calculateComServiceTotalAmount(comService.getComment().getComs());

                var receipt = new Receipt();  // todo:: beter use fluent accessors or builder

                receipt.setPeriod(date);
                receipt.setPayed(paymentStatus);
                receipt.setTotalAmount(totalAmount);
                receipt.setAccount(comService.getAccount());
                receipt.setServiceIcon(properties.getMyVillageIcon());
                receipt.setName(comService.getComment().getComs().get(0).getOwner());
                receipt.setAddress(comService.getComment().getComs().get(0).getAddress());
                
                return receipt;
 })
 .toList();
}


Современный язык Java. Лямбда-выражения, потоки и ...
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы