@Test public void isPriceBreakdownConsistent_whenAnyItemIsNull_returnsTrue() { assertTrue(isPriceBreakdownConsistent(null, BigDecimal.TEN, 1234L)); assertTrue(isPriceBreakdownConsistent(55L, null, 8888L)); assertTrue(isPriceBreakdownConsistent(33L, BigDecimal.ONE, null)); }
static boolean isPriceBreakdownConsistent( Long unitPrice, BigDecimal quantity, Long totalPrice) { if (unitPrice == null || quantity == null || totalPrice == null) { return true; } BigDecimal expectedPrice = quantity.multiply(BigDecimal.valueOf(unitPrice)); BigDecimal actualPrice = BigDecimal.valueOf(totalPrice); if (expectedPrice.compareTo(BigDecimal.ZERO) == 0) { return totalPrice == 0; } double ratio = actualPrice.divide( expectedPrice, CONSISTENCY_SCALE, BigDecimal.ROUND_HALF_EVEN).doubleValue(); return Math.abs(ratio - 1.0) < CONSISTENCY_THRESHOLD; }
LineItemBuilder { static boolean isPriceBreakdownConsistent( Long unitPrice, BigDecimal quantity, Long totalPrice) { if (unitPrice == null || quantity == null || totalPrice == null) { return true; } BigDecimal expectedPrice = quantity.multiply(BigDecimal.valueOf(unitPrice)); BigDecimal actualPrice = BigDecimal.valueOf(totalPrice); if (expectedPrice.compareTo(BigDecimal.ZERO) == 0) { return totalPrice == 0; } double ratio = actualPrice.divide( expectedPrice, CONSISTENCY_SCALE, BigDecimal.ROUND_HALF_EVEN).doubleValue(); return Math.abs(ratio - 1.0) < CONSISTENCY_THRESHOLD; } }
LineItemBuilder { static boolean isPriceBreakdownConsistent( Long unitPrice, BigDecimal quantity, Long totalPrice) { if (unitPrice == null || quantity == null || totalPrice == null) { return true; } BigDecimal expectedPrice = quantity.multiply(BigDecimal.valueOf(unitPrice)); BigDecimal actualPrice = BigDecimal.valueOf(totalPrice); if (expectedPrice.compareTo(BigDecimal.ZERO) == 0) { return totalPrice == 0; } double ratio = actualPrice.divide( expectedPrice, CONSISTENCY_SCALE, BigDecimal.ROUND_HALF_EVEN).doubleValue(); return Math.abs(ratio - 1.0) < CONSISTENCY_THRESHOLD; } LineItemBuilder();   LineItemBuilder(String currencyCode); }
LineItemBuilder { static boolean isPriceBreakdownConsistent( Long unitPrice, BigDecimal quantity, Long totalPrice) { if (unitPrice == null || quantity == null || totalPrice == null) { return true; } BigDecimal expectedPrice = quantity.multiply(BigDecimal.valueOf(unitPrice)); BigDecimal actualPrice = BigDecimal.valueOf(totalPrice); if (expectedPrice.compareTo(BigDecimal.ZERO) == 0) { return totalPrice == 0; } double ratio = actualPrice.divide( expectedPrice, CONSISTENCY_SCALE, BigDecimal.ROUND_HALF_EVEN).doubleValue(); return Math.abs(ratio - 1.0) < CONSISTENCY_THRESHOLD; } LineItemBuilder();   LineItemBuilder(String currencyCode); LineItemBuilder setCurrencyCode(String currencyCode); LineItemBuilder setUnitPrice(long unitPrice); LineItemBuilder setTotalPrice(long totalPrice); LineItemBuilder setQuantity(BigDecimal quantity); LineItemBuilder setQuantity(double quantity); LineItemBuilder setDescription(String description); LineItemBuilder setRole(int role); LineItem build(); }
LineItemBuilder { static boolean isPriceBreakdownConsistent( Long unitPrice, BigDecimal quantity, Long totalPrice) { if (unitPrice == null || quantity == null || totalPrice == null) { return true; } BigDecimal expectedPrice = quantity.multiply(BigDecimal.valueOf(unitPrice)); BigDecimal actualPrice = BigDecimal.valueOf(totalPrice); if (expectedPrice.compareTo(BigDecimal.ZERO) == 0) { return totalPrice == 0; } double ratio = actualPrice.divide( expectedPrice, CONSISTENCY_SCALE, BigDecimal.ROUND_HALF_EVEN).doubleValue(); return Math.abs(ratio - 1.0) < CONSISTENCY_THRESHOLD; } LineItemBuilder();   LineItemBuilder(String currencyCode); LineItemBuilder setCurrencyCode(String currencyCode); LineItemBuilder setUnitPrice(long unitPrice); LineItemBuilder setTotalPrice(long totalPrice); LineItemBuilder setQuantity(BigDecimal quantity); LineItemBuilder setQuantity(double quantity); LineItemBuilder setDescription(String description); LineItemBuilder setRole(int role); LineItem build(); static final Set<Integer> VALID_ROLES; }