Class SplitTotalRounder
java.lang.Object
com.broadleafcommerce.orderoperation.service.split.SplitTotalRounder
Can be used for handling rounding of amounts which need to add up to different totals.
This tracks targeted totals in two different directions to allow rounding component amounts while still summing to those totals.
For example, when splitting items between multiple fulfillments, this rounding method ensures that:
- The split versions of a particular item still sum to the original item price after rounding
- All the items in a particular split fulfillment sum to that split fulfillment's designated item subtotal
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents an amount value that has been split into multiple new amounts. -
Constructor Summary
ConstructorsConstructorDescriptionSplitTotalRounder(List<List<javax.money.MonetaryAmount>> amountsOfSplits, List<javax.money.MonetaryAmount> splitTargetTotals, javax.money.MonetaryAmount unitAmount, com.broadleafcommerce.order.client.domain.OrderFulfillment originalFulfillment) Create a newSplitTotalRounderto round amounts which have been split. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddistributeAmountUnits(int splitIndex) Distributes units to one split part, indicated by the givensplitIndexinto one of the parts of each element ofamounts.protected voiddistributeUnitsForAmounts(Function<SplitTotalRounder.SplitAmount, javax.money.MonetaryAmount> getAmount, Function<SplitTotalRounder.SplitAmount, javax.money.MonetaryAmount> getRemainder, Consumer<SplitTotalRounder.SplitAmount> incrementAmount, javax.money.MonetaryAmount targetTotal) Distributes units to amounts on one part of the split until the giventargetTotalis reached.protected List<SplitTotalRounder.SplitAmount>The amounts which have been split.List<javax.money.MonetaryAmount>getAmountsForSplit(int splitIndex) Retrieve the amounts for one split part.protected com.broadleafcommerce.order.client.domain.OrderFulfillmentThe original fulfillment which was split.protected IntegergetScale()protected List<javax.money.MonetaryAmount>The amounts which all of the amounts for a specific split part should sum to.protected javax.money.MonetaryAmountThe smallest unit of the currency.voidRounds the contained amounts so that each split side sums to the target totals, and each split amount sums to its original value.protected voidprotected voidsetSplitTargetTotals(List<javax.money.MonetaryAmount> splitTargetTotals) The amounts which all of the amounts for a specific split part should sum to.protected voidsetUnitAmount(javax.money.MonetaryAmount unitAmount) The smallest unit of the currency.
-
Constructor Details
-
SplitTotalRounder
public SplitTotalRounder(List<List<javax.money.MonetaryAmount>> amountsOfSplits, List<javax.money.MonetaryAmount> splitTargetTotals, javax.money.MonetaryAmount unitAmount, @Nullable com.broadleafcommerce.order.client.domain.OrderFulfillment originalFulfillment) Create a newSplitTotalRounderto round amounts which have been split.- Parameters:
amountsOfSplits- a list where each element represents one part of a split, each of which contains a list of amounts containing the split portion of the original amount which was assigned to that partsplitTargetTotals- the target total for each split partunitAmount- the smallest unit of the currencyoriginalFulfillment- the original fulfillment which was split, if splitting a fulfillment
-
-
Method Details
-
roundAmounts
public void roundAmounts()Rounds the contained amounts so that each split side sums to the target totals, and each split amount sums to its original value.All amounts are first rounded to floor, and then units are distributed for each side of the split.
-
distributeAmountUnits
protected void distributeAmountUnits(int splitIndex) Distributes units to one split part, indicated by the givensplitIndexinto one of the parts of each element ofamounts.- Parameters:
splitIndex- the index into theSplitTotalRounder.SplitAmount.splitAmounts, indicating which part of the split to distribute units to
-
distributeUnitsForAmounts
protected void distributeUnitsForAmounts(Function<SplitTotalRounder.SplitAmount, javax.money.MonetaryAmount> getAmount, Function<SplitTotalRounder.SplitAmount, javax.money.MonetaryAmount> getRemainder, Consumer<SplitTotalRounder.SplitAmount> incrementAmount, javax.money.MonetaryAmount targetTotal) Distributes units to amounts on one part of the split until the giventargetTotalis reached. An amount will only be incremented if it has not already reached the correct total across that amount.This implementation only handles unit-rounding, and will never distribute more than one unit to each amount. This should always be sufficient to reach the target total. If some erroneous case occurs where the target total cannot be reached, extra units will not be distributed, and the amounts will not add up to the target total.
- Parameters:
getAmount- getter for the amount on this side of the splitgetRemainder- getter for the remainder on this side of the splitincrementAmount- method to increment the amount on this side of the splittargetTotal- the total which the amounts on this side of the split should sum to
-
getAmountsForSplit
Retrieve the amounts for one split part.- Parameters:
splitIndex- the index of the part which was split to retrieve- Returns:
- a list of all the amounts for a specific split part
-
getOriginalFulfillment
@Nullable protected com.broadleafcommerce.order.client.domain.OrderFulfillment getOriginalFulfillment()The original fulfillment which was split. -
getAmounts
The amounts which have been split. -
getSplitTargetTotals
The amounts which all of the amounts for a specific split part should sum to. -
getUnitAmount
protected javax.money.MonetaryAmount getUnitAmount()The smallest unit of the currency. -
getScale
-
setSplitTargetTotals
The amounts which all of the amounts for a specific split part should sum to. -
setUnitAmount
protected void setUnitAmount(javax.money.MonetaryAmount unitAmount) The smallest unit of the currency. -
setScale
-