ItemUtils.java
package com.tradecloud.domain.utils;
import com.tradecloud.domain.base.utils.MathUtils;
import java.math.BigDecimal;
/**
* Utility class for item calculations.
*/
public final class ItemUtils {
// Prevent instantiation of this utility class
private ItemUtils() {
}
/**
* Calculates the unit quantity by multiplying the numPackages by unitsPerPackage.
*
* @param numPackages number of packages, cannot be null
* @param unitsPerPackage units per package, cannot be null
* @return the calculated unit quantity
* @throws IllegalArgumentException if numPackages or unitsPerPackage are null
*/
public static BigDecimal getUnitQuantity(BigDecimal numPackages, BigDecimal unitsPerPackage) {
if (numPackages == null || unitsPerPackage == null) {
throw new IllegalArgumentException("numPackage and unitsPerPackage are required");
}
return numPackages.multiply(unitsPerPackage).setScale(MathUtils.SCALE, MathUtils.ROUNDING_MODE);
}
/**
* Calculates the number of packages by dividing the unitQuantity by the unitsPerPackage.
* <p>
* If unitsPerPackage is zero this method will return zero.
*
* @param unitQuantity the unit quantity, cannot be null
* @param unitsPerPackage the units per package, cannot be null
* @return the calculated number of packages or zero if unitsPerPackage is zero
*/
public static BigDecimal getNumPackages(BigDecimal unitQuantity, BigDecimal unitsPerPackage) {
if (unitQuantity == null || unitsPerPackage == null) {
throw new IllegalArgumentException("unitQuantity and unitsPerPackage are required");
}
/**
* The method BigDecimal.equals() takes scale into consideration: and cause the boolean fails and ArithmeticException: / by zero
* Exception occurs
* return !unitsPerPackage.equals(BigDecimal.ZERO) ? unitQuantity.divide(unitsPerPackage, MathUtils.SCALE, MathUtils.ROUNDING_MODE)
: MathUtils.ZERO;
**/
return unitsPerPackage.compareTo(BigDecimal.ZERO) != 0 ? unitQuantity.divide(unitsPerPackage, MathUtils.SCALE, MathUtils.ROUNDING_MODE)
: MathUtils.ZERO;
}
public static BigDecimal getTotal(BigDecimal unitQuantity, BigDecimal unitMeasure) {
if (unitQuantity == null || unitMeasure == null) {
return MathUtils.ZERO;
}
return MathUtils.multiply(unitQuantity, unitMeasure);
}
}