0

I have the following DTO:

@Data
public class MenuItemDTO {
    private UUID siteUuid;
    private UUID productUuid;
    private boolean outOfStock;

    public MenuItemDTO(MenuItem menuItem, boolean outOfStock) {
        this.siteUuid = menuItem.getSiteUuid();
        this.productUuid = menuItem.getProductUuid();
        this.outOfStock = outOfStock;
    }
}

I have not outOfStock field in MenuItem entity and I want to fill outOfStock field if the record is not empty (I just keep the out of stocked products). But I am not sure if there is a proper way using stream:

public List<MenuItemDTO> findAllBySiteBrandUuid(UUID siteUuid) {
    return menuItemRepository.findAllBySiteUuid(siteUuid)
            .stream()
            .map(m -> new MenuItemDTO(m, m.getUuid() != null))
            .collect(Collectors.toList());
}

1. As I do not keep outOfStock field in MenuItem entity, is my approach in MenuItemDTO a proper way?

2. I am wondering if there is a more elegant way by using stream properly for .map(m -> new MenuItemDTO(m, m.getUuid() != null)) part.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Jack
  • 1
  • 21
  • 118
  • 236

1 Answers1

2

There's quite some duplication. You're passing an object and a field of an object to the constructor. Maybe the more elegant way would be to avoid passing the information twice by moving the logic to the constructor:

public MenuItemDTO(MenuItem menuItem) {
    this.siteUuid = menuItem.getSiteUuid();
    this.productUuid = menuItem.getProductUuid();
    this.outOfStock = menuItem.getUuid() != null;
}

And then the stream mapping reduces to:

public List<MenuItemDTO> findAllBySiteBrandUuid(UUID siteUuid) {
    return menuItemRepository.findAllBySiteUuid(siteUuid)
            .stream()
            .map(MenuItemDTO::new)
            .collect(Collectors.toList());
}

The information in the DTO is whatever client needs, so it's not up to us to decide if putting boolean there is the right way. I'd say it's good if that's what will be used on the client side.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • Thanks a lot, very good explanations. Now I need to be clarified about the following points: – Jack Mar 01 '22 at 12:45
  • **1.** Using a boolean `outOfStock` field in DTO and filling them according to another entity value is ok I think, right? – Jack Mar 01 '22 at 12:46
  • **2.** I think there is not any method for checking null e.g. equals, for this line: `this.outOfStock = menuItem.getUuid() != null;` Any idea? – Jack Mar 01 '22 at 12:47
  • @Jack 1. yes, I didn't comment on that because it's not for me to decide. DTO should contain all the information required by the client – Andronicus Mar 01 '22 at 12:47
  • @Jack 2. `!= null` is a standard approach for null checking in java. If you're not comfortable with that you can use `Objects.nonNull`, but there's not much difference. I hope I understood your question. – Andronicus Mar 01 '22 at 12:49
  • @Jack no problem, I'm glad I could help. Have a nice day! – Andronicus Mar 01 '22 at 13:02