Source:
private void build() { if (isBuilt) { return; } if (target == null || target.getAmount() == null) { if (repaired != null) { for (final RepairAdjInvoiceItem i : repaired) { Preconditions.checkState(false, "Missing cancelledItem for cancelItem=%s", i); } } return; } if (target.getAmount().compareTo(BigDecimal.ZERO) == 0) { return; } final BigDecimal repairedAmount = sumAmounts(repaired).negate(); if (repaired != null) { for (final RepairAdjInvoiceItem i : repaired) { Preconditions.checkState(i.getStartDate().compareTo(target.getStartDate()) >= 0 && i.getEndDate().compareTo(target.getEndDate()) <= 0, "Invalid cancelledItem=%s for cancelItem=%s", i.getId(), target.getId()); } } final BigDecimal totalAdjusted = sumAmounts(adjusted).negate(); final BigDecimal remainingFromAdjusted = target.getAmount().subtract(totalAdjusted); final int compAdjOnly = remainingFromAdjusted.compareTo(BigDecimal.ZERO); if (compAdjOnly == -1) { } else { final BigDecimal remainingFromRepair = target.getAmount().subtract(repairedAmount); final int compRepair = remainingFromRepair.compareTo(BigDecimal.ZERO); if (compRepair == 0) { Preconditions.checkState(adjusted == null || adjusted.isEmpty(), "Too many repairs for invoiceItemId='%s'", target.getId()); result.add(target.getId()); result.addAll(getIds(repaired)); } else { final BigDecimal adjustedAmount = sumAmounts(adjusted).negate(); final int compWithAdjustments = remainingFromRepair.subtract(adjustedAmount).compareTo(BigDecimal.ZERO); if (compWithAdjustments == -1) { Preconditions.checkState(false, "Too many repairs for invoiceItemId='%s'", target.getId()); } } } this.isBuilt = true; }

Target:
private void build() { if (isBuilt) { return; } if (target == null || target.getAmount() == null) { if (repaired != null) { for (final RepairAdjInvoiceItem i : repaired) { Preconditions.checkState(false, "Missing cancelledItem for cancelItem=%s", i); } } return; } if (target.getAmount().compareTo(BigDecimal.ZERO) == 0) { return; } final BigDecimal repairedAmount = sumAmounts(repaired).negate(); if (repaired != null) { for (final RepairAdjInvoiceItem i : repaired) { Preconditions.checkState(i.getStartDate().compareTo(target.getStartDate()) >= 0 && i.getEndDate().compareTo(target.getEndDate()) <= 0, "Invalid cancelledItem=%s for cancelItem=%s", i.getId(), target.getId()); } } final BigDecimal totalAdjusted = sumAmounts(adjusted).negate(); final BigDecimal remainingFromAdjusted = target.getAmount().subtract(totalAdjusted); final int compAdjOnly = remainingFromAdjusted.compareTo(BigDecimal.ZERO); if (compAdjOnly == -1) { } else { final BigDecimal remainingFromRepair = target.getAmount().subtract(repairedAmount); final int compRepair = remainingFromRepair.compareTo(BigDecimal.ZERO); if (compRepair == 0) { Preconditions.checkState(adjusted == null || adjusted.isEmpty(), "Too many repairs for invoiceItemId='%s', fully repaired and adjusted='%s'", target.getId(), totalAdjusted); result.add(target.getId()); result.addAll(getIds(repaired)); } else { final int compWithAdjustments = remainingFromRepair.subtract(totalAdjusted).compareTo(BigDecimal.ZERO); if (compWithAdjustments == -1) { Preconditions.checkState(false, "Too many repairs for invoiceItemId='%s', partially repaired='%s', partially adjusted='%s', total='%s'", target.getId(), repairedAmount, totalAdjusted, target.getAmount()); } } } this.isBuilt = true; }