Class DefaultWeightCalculationService
- java.lang.Object
-
- com.broadleafcommerce.shipping.service.DefaultWeightCalculationService
-
- All Implemented Interfaces:
WeightCalculationService
public class DefaultWeightCalculationService extends Object implements WeightCalculationService
Utilizes the Indriya project (Github) to support weight calculations with different units of measurement.
-
-
Constructor Summary
Constructors Constructor Description DefaultWeightCalculationService(MathContext shippingWeightMathContext, com.fasterxml.jackson.databind.ObjectMapper mapper, MeasurementProperties measurementProperties)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addUnitLabels(Map<String,List<String>> unitAliases)
Reads unit aliases from properties and registers them with theSimpleUnitFormat
singleton instance.protected void
alternativeWeightUnitSetup()
int
compare(com.broadleafcommerce.shipping.domain.Weight weight1, com.broadleafcommerce.shipping.domain.Weight weight2)
Convert given weights toMass
and compare.protected tech.units.indriya.ComparableQuantity<?>
convertToComparableQuantity(com.broadleafcommerce.shipping.domain.Weight weight)
Convert a BroadlefWeight
into an IndriyaComparableQuantity
.protected List<com.broadleafcommerce.shipping.domain.Weight>
getAllItemWeights(com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment)
Returns a list with aWeight
for each item in the givenOrderFulfillment
.protected List<com.broadleafcommerce.shipping.domain.Weight>
getAllItemWeights(com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Returns a list with aWeight
for each item in the givenFulfillmentPricingRequest
.protected com.fasterxml.jackson.databind.ObjectMapper
getMapper()
protected MeasurementProperties
getMeasurementProperties()
protected MathContext
getShippingWeightMathContext()
com.broadleafcommerce.shipping.domain.Weight
getTotalPackageWeight(com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment)
Calculate the total combined weight of anOrderFulfillment
.com.broadleafcommerce.shipping.domain.Weight
getTotalPackageWeight(com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Calculate the total combined weight of aFulfillmentPricingRequest
.protected com.broadleafcommerce.shipping.domain.Weight
sumWeights(List<com.broadleafcommerce.shipping.domain.Weight> itemWeights)
Sum the given list ofWeight
s.
-
-
-
Constructor Detail
-
DefaultWeightCalculationService
public DefaultWeightCalculationService(MathContext shippingWeightMathContext, com.fasterxml.jackson.databind.ObjectMapper mapper, MeasurementProperties measurementProperties)
-
-
Method Detail
-
alternativeWeightUnitSetup
@PostConstruct protected void alternativeWeightUnitSetup()
-
compare
public int compare(com.broadleafcommerce.shipping.domain.Weight weight1, com.broadleafcommerce.shipping.domain.Weight weight2)
Convert given weights toMass
and compare.- Specified by:
compare
in interfaceWeightCalculationService
- Parameters:
weight1
- The reference weight.weight2
- The weight to compare against the reference weight.- Returns:
- -1, 0, or 1, according to whether weight1 is less than, equal to, or greater than weight2.
- See Also:
Comparable.compareTo(java.lang.Object)
-
getTotalPackageWeight
public com.broadleafcommerce.shipping.domain.Weight getTotalPackageWeight(com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Description copied from interface:WeightCalculationService
Calculate the total combined weight of aFulfillmentPricingRequest
.- Specified by:
getTotalPackageWeight
in interfaceWeightCalculationService
- Parameters:
fulfillmentPricingRequest
- The fulfillment being shipped.- Returns:
- A
Weight
representing the total weight of all items in the fulfillment.
-
getTotalPackageWeight
public com.broadleafcommerce.shipping.domain.Weight getTotalPackageWeight(com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment)
Description copied from interface:WeightCalculationService
Calculate the total combined weight of anOrderFulfillment
.- Specified by:
getTotalPackageWeight
in interfaceWeightCalculationService
- Parameters:
orderFulfillment
- The fulfillment being shipped.- Returns:
- A
Weight
representing the total weight of all items in the fulfillment.
-
getAllItemWeights
protected List<com.broadleafcommerce.shipping.domain.Weight> getAllItemWeights(com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Returns a list with aWeight
for each item in the givenFulfillmentPricingRequest
. Weight objects will not be created for items that do not have a weight value or weight unit.- Parameters:
fulfillmentPricingRequest
- The fulfillment being shipped.- Returns:
- A
List
with aWeight
for each item in the given request.
-
getAllItemWeights
protected List<com.broadleafcommerce.shipping.domain.Weight> getAllItemWeights(com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment)
Returns a list with aWeight
for each item in the givenOrderFulfillment
. Weight objects will not be created for items that do not have a weight value or weight unit.- Parameters:
orderFulfillment
- The fulfillment being shipped.- Returns:
- A
List
with aWeight
for each item in the given request.
-
sumWeights
protected com.broadleafcommerce.shipping.domain.Weight sumWeights(List<com.broadleafcommerce.shipping.domain.Weight> itemWeights)
Sum the given list ofWeight
s. Weights can be in different units.- Parameters:
itemWeights
- The list to sum.- Returns:
- A
Weight
representing the sum of all Weights in the given List.
-
convertToComparableQuantity
protected tech.units.indriya.ComparableQuantity<?> convertToComparableQuantity(com.broadleafcommerce.shipping.domain.Weight weight)
Convert a BroadlefWeight
into an IndriyaComparableQuantity
.- Parameters:
weight
- TheWeight
to convert.- Returns:
- The equivalent
ComparableQuantity
.
-
addUnitLabels
protected void addUnitLabels(Map<String,List<String>> unitAliases)
Reads unit aliases from properties and registers them with theSimpleUnitFormat
singleton instance.- Parameters:
unitAliases
- Map of Unit class to aliases.
-
getShippingWeightMathContext
protected MathContext getShippingWeightMathContext()
-
getMapper
protected com.fasterxml.jackson.databind.ObjectMapper getMapper()
-
getMeasurementProperties
protected MeasurementProperties getMeasurementProperties()
-
-