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 voidaddUnitLabels(Map<String,List<String>> unitAliases)Reads unit aliases from properties and registers them with theSimpleUnitFormatsingleton instance.protected voidalternativeWeightUnitSetup()intcompare(com.broadleafcommerce.shipping.domain.Weight weight1, com.broadleafcommerce.shipping.domain.Weight weight2)Convert given weights toMassand compare.protected tech.units.indriya.ComparableQuantity<?>convertToComparableQuantity(com.broadleafcommerce.shipping.domain.Weight weight)Convert a BroadlefWeightinto an IndriyaComparableQuantity.protected List<com.broadleafcommerce.shipping.domain.Weight>getAllItemWeights(com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment)Returns a list with aWeightfor each item in the givenOrderFulfillment.protected List<com.broadleafcommerce.shipping.domain.Weight>getAllItemWeights(com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)Returns a list with aWeightfor each item in the givenFulfillmentPricingRequest.protected com.fasterxml.jackson.databind.ObjectMappergetMapper()protected MeasurementPropertiesgetMeasurementProperties()protected MathContextgetShippingWeightMathContext()com.broadleafcommerce.shipping.domain.WeightgetTotalPackageWeight(com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment)Calculate the total combined weight of anOrderFulfillment.com.broadleafcommerce.shipping.domain.WeightgetTotalPackageWeight(com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)Calculate the total combined weight of aFulfillmentPricingRequest.protected com.broadleafcommerce.shipping.domain.WeightsumWeights(List<com.broadleafcommerce.shipping.domain.Weight> itemWeights)Sum the given list ofWeights.
-
-
-
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 toMassand compare.- Specified by:
comparein 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:WeightCalculationServiceCalculate the total combined weight of aFulfillmentPricingRequest.- Specified by:
getTotalPackageWeightin interfaceWeightCalculationService- Parameters:
fulfillmentPricingRequest- The fulfillment being shipped.- Returns:
- A
Weightrepresenting 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:WeightCalculationServiceCalculate the total combined weight of anOrderFulfillment.- Specified by:
getTotalPackageWeightin interfaceWeightCalculationService- Parameters:
orderFulfillment- The fulfillment being shipped.- Returns:
- A
Weightrepresenting 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 aWeightfor 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
Listwith aWeightfor 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 aWeightfor 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
Listwith aWeightfor 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 ofWeights. Weights can be in different units.- Parameters:
itemWeights- The list to sum.- Returns:
- A
Weightrepresenting the sum of all Weights in the given List.
-
convertToComparableQuantity
protected tech.units.indriya.ComparableQuantity<?> convertToComparableQuantity(com.broadleafcommerce.shipping.domain.Weight weight)
Convert a BroadlefWeightinto an IndriyaComparableQuantity.- Parameters:
weight- TheWeightto convert.- Returns:
- The equivalent
ComparableQuantity.
-
addUnitLabels
protected void addUnitLabels(Map<String,List<String>> unitAliases)
Reads unit aliases from properties and registers them with theSimpleUnitFormatsingleton 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()
-
-