PackageDTO.java

package com.tradecloud.dto.consignment;

import com.tradecloud.domain.base.utils.ObjectUtil;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class PackageDTO {
    private String reference;
    private BigDecimal quantity = BigDecimal.ONE;
    private BigDecimal length;
    private BigDecimal width;
    private BigDecimal height;
    private BigDecimal volume;
    private BigDecimal weight;
    private BigDecimal totalVolume;
    private BigDecimal totalWeight;

    public PackageDTO() {
    }

    public PackageDTO(String reference) {
        this.reference = reference;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getLength() {
        return length;
    }

    public void setLength(BigDecimal length) {
        this.length = length;
    }

    public BigDecimal getWidth() {
        return width;
    }

    public void setWidth(BigDecimal width) {
        this.width = width;
    }

    public BigDecimal getHeight() {
        return height;
    }

    public void setHeight(BigDecimal height) {
        this.height = height;
    }

    public BigDecimal getVolume() {
        return this.volume = ObjectUtil.allNotNull(length, width, height, quantity)
                ? length.multiply(width).multiply(height).divide(BigDecimal.valueOf(1000000), MathContext.DECIMAL128)
                : BigDecimal.ZERO;
    }

    public void setVolume(BigDecimal volume) {
        this.volume = volume;
    }

    public BigDecimal getWeight() {
        return weight;
    }

    public void setWeight(BigDecimal weight) {
        this.weight = weight;
    }

    public BigDecimal getTotalVolume() {
        return totalVolume = ObjectUtil.allNotNull(getVolume(), this.quantity)
                ? getVolume().multiply(this.quantity).setScale(3, RoundingMode.HALF_UP)
                : BigDecimal.ZERO;
    }

    public void setTotalVolume(BigDecimal totalVolume) {
        this.totalVolume = ObjectUtil.allNotNull(getVolume(), this.quantity)
                ? getVolume().multiply(this.quantity).setScale(3, RoundingMode.HALF_UP)
                : BigDecimal.ZERO;
    }

    public BigDecimal getTotalWeight() {
        return totalWeight = ObjectUtil.allNotNull(this.weight, this.quantity)
                ? this.weight.multiply(this.quantity).setScale(3, RoundingMode.HALF_UP)
                : BigDecimal.ZERO;
    }

    public void setTotalWeight(BigDecimal totalWeight) {
        this.totalWeight = ObjectUtil.allNotNull(this.weight, this.quantity)
                ? this.weight.multiply(this.quantity).setScale(3, RoundingMode.HALF_UP)
                : BigDecimal.ZERO;
    }
}