RFQRule.java

package com.tradecloud.domain.model.requestforquote;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.base.utils.MathUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAttribute;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Entity
@Table(name = "rfqrule")
public class RFQRule extends PersistenceBase {
    @XmlAttribute
    @Column
    private BigDecimal ruleAllocation1 = BigDecimal.ZERO;

    @XmlAttribute
    @Column
    private BigDecimal ruleAllocation2 = BigDecimal.ZERO;

    @XmlAttribute
    @Column
    private BigDecimal ruleAllocation3 = BigDecimal.ZERO;

    @XmlAttribute
    @Column
    private BigDecimal ruleAllocation4 = BigDecimal.ZERO;

    @XmlAttribute
    @Column
    private BigDecimal ruleAllocation5 = BigDecimal.ZERO;

    public BigDecimal getRuleAllocation1() {
        return ruleAllocation1;
    }

    public void setRuleAllocation1(BigDecimal ruleAllocation1) {
        this.ruleAllocation1 = ruleAllocation1;
    }

    public BigDecimal getRuleAllocation2() {
        return ruleAllocation2;
    }

    public void setRuleAllocation2(BigDecimal ruleAllocation2) {
        this.ruleAllocation2 = ruleAllocation2;
    }

    public BigDecimal getRuleAllocation3() {
        return ruleAllocation3;
    }

    public void setRuleAllocation3(BigDecimal ruleAllocation3) {
        this.ruleAllocation3 = ruleAllocation3;
    }

    public BigDecimal getRuleAllocation4() {
        return ruleAllocation4;
    }

    public void setRuleAllocation4(BigDecimal ruleAllocation4) {
        this.ruleAllocation4 = ruleAllocation4;
    }

    public BigDecimal getRuleAllocation5() {
        return ruleAllocation5;
    }

    public void setRuleAllocation5(BigDecimal ruleAllocation5) {
        this.ruleAllocation5 = ruleAllocation5;
    }

    public BigDecimal getTotalAllocation() {
        return getAllocationStream()
                .map(field -> field == null ? BigDecimal.ZERO : field)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public List<BigDecimal> getAllocations() {
        return getAllocationStream()
                .filter(field -> field != null && MathUtils.isNonZero(field)).collect(Collectors.toList());

    }

    private Stream<BigDecimal> getAllocationStream() {
        return Stream.of(ruleAllocation1, ruleAllocation2, ruleAllocation3, ruleAllocation4, ruleAllocation5);
    }
}