Source:
private List<ShipmentSchedule> loadShipmentSchedulesToExport(@NonNull final QueryLimit limit) { final ShipmentScheduleQuery shipmentScheduleQuery = ShipmentScheduleQuery.builder() .canBeExportedFrom(SystemTime.asInstant()) .noFutureExportOfOrder(true) .exportStatus(APIExportStatus.Pending) .includeWithQtyToDeliverZero(true) .fromCompleteOrderOrNullOrder(true) .orderByOrderId(true) .build(); final List<ShipmentSchedule> shipmentSchedulesOrderedByOrderId = shipmentScheduleRepository.getBy(shipmentScheduleQuery); List<ShipmentSchedule> schedulesToBeExported = new ArrayList<>(); int counter = 0; for (ShipmentSchedule schedule : shipmentSchedulesOrderedByOrderId) { if (schedule.getOrderAndLineId() == null && counter < limit.toInt() - 1) { schedulesToBeExported.add(schedule); counter++; } } if (counter == limit.toInt()) { return schedulesToBeExported; } final List<ShipmentSchedule> nonNullOrderShipmentSchedules = shipmentSchedulesOrderedByOrderId.stream() .filter(sched -> sched.getOrderAndLineId() != null) .collect(ImmutableList.toImmutableList()); final ImmutableListMultimap<OrderId, ShipmentSchedule> schedulesForOrderIds = Multimaps.index(nonNullOrderShipmentSchedules, sched -> sched.getOrderAndLineId().getOrderId()); int ordersLeftToExport = limit.toInt() - counter; final UnmodifiableIterator<OrderId> orderIdIterator = schedulesForOrderIds.keySet().iterator(); while (orderIdIterator.hasNext() && ordersLeftToExport > 0) { final OrderId currentOrderId = orderIdIterator.next(); schedulesToBeExported.addAll(schedulesForOrderIds.get(currentOrderId)); ordersLeftToExport--; } return schedulesToBeExported; }

Target:
private List<ShipmentSchedule> loadShipmentSchedulesToExport(@NonNull final QueryLimit limit) { final ShipmentScheduleQuery shipmentScheduleQuery = ShipmentScheduleQuery.builder() .canBeExportedFrom(SystemTime.asInstant()) .onlyIfAllFromOrderExportable(true) .exportStatus(APIExportStatus.Pending) .includeWithQtyToDeliverZero(true) .fromCompleteOrderOrNullOrder(true) .orderByOrderId(true) .build(); final List<ShipmentSchedule> shipmentSchedulesOrderedByOrderId = shipmentScheduleRepository.getBy(shipmentScheduleQuery); List<ShipmentSchedule> schedulesToBeExported = new ArrayList<>(); int counter = 0; for (ShipmentSchedule schedule : shipmentSchedulesOrderedByOrderId) { if (schedule.getOrderAndLineId() == null && counter < limit.toInt() - 1) { schedulesToBeExported.add(schedule); counter++; } else { break; } } if (counter == limit.toInt()) { return schedulesToBeExported; } final List<ShipmentSchedule> nonNullOrderShipmentSchedules = shipmentSchedulesOrderedByOrderId.stream() .filter(sched -> sched.getOrderAndLineId() != null) .collect(ImmutableList.toImmutableList()); final ImmutableListMultimap<OrderId, ShipmentSchedule> schedulesForOrderIds = Multimaps.index(nonNullOrderShipmentSchedules, sched -> sched.getOrderAndLineId().getOrderId()); int ordersLeftToExport = limit.toInt() - counter; final UnmodifiableIterator<OrderId> orderIdIterator = schedulesForOrderIds.keySet().iterator(); while (orderIdIterator.hasNext() && ordersLeftToExport > 0) { final OrderId currentOrderId = orderIdIterator.next(); schedulesToBeExported.addAll(schedulesForOrderIds.get(currentOrderId)); ordersLeftToExport--; } return schedulesToBeExported; }