SupplementaryQuantity.java

package com.tradecloud.domain.duties;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * A embeddable value class that holds 3 tightly coupled fields for convenience.
 */
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "SupplementaryQuantity")
@Embeddable
public class SupplementaryQuantity implements Serializable, Cloneable {

    /**
     * A duty that will be applied on additional parts of the product.
     */
    @XmlAttribute
    @Column(precision = 19, scale = 5)
    private BigDecimal value;

    /**
     * The type of unit associated to {@link #value}.
     */
    @Enumerated(value = EnumType.STRING)
    @XmlAttribute
    private DutyUnit unit;

    public SupplementaryQuantity() {
    }

    public SupplementaryQuantity(BigDecimal value, DutyUnit unit) {
        this.value = value;
        this.unit = unit;
    }

    public SupplementaryQuantity(SupplementaryQuantity supplementaryQty) {
        value = supplementaryQty.value;
        unit = supplementaryQty.unit;
    }

    public BigDecimal getValue() {
        return value;
    }

    public void setValue(BigDecimal value) {
        this.value = value;
    }

    public DutyUnit getUnit() {
        return unit;
    }

    public void setUnit(DutyUnit unit) {
        this.unit = unit;
    }

    @Override
    public SupplementaryQuantity clone() {
        try {
            SupplementaryQuantity clone = (SupplementaryQuantity) super.clone();
            clone.setUnit(unit);
            clone.setValue(value);

            return clone;
        } catch (CloneNotSupportedException ex) {
            return null;
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("value", value).append("unit", unit).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof SupplementaryQuantity)) {
            return false;
        }
        SupplementaryQuantity supplementaryQty = (SupplementaryQuantity) obj;
        return new EqualsBuilder().append(value, supplementaryQty.value).append(unit, supplementaryQty.unit).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(value).append(unit).toHashCode();
    }
}