All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.zeimao77.product.tree.ThresholdVoterComponent Maven / Gradle / Ivy

package top.zeimao77.product.tree;

import top.zeimao77.product.exception.BaseServiceRunException;
import static top.zeimao77.product.exception.ExceptionCodeDefinition.CUSTOM;
import top.zeimao77.product.util.AssertUtil;

import java.util.List;

public class ThresholdVoterComponent implements Voter {

    /**
     * 表决器阈值
     * 阈值用于控制表决行为
     * 1D:一票否决
     * 0D:一票通过
     * 0D > threshold > 1D : 如果 投票通过的票数/投票器列表数量 >= threshold 将会被表决通过;
     */
    private Double threshold;

    // 投票器列表
    List voters;

    /**
     * 构造一个表决器
     * @param voters  投票器列表
     * @param threshold 阈值
     */
    public ThresholdVoterComponent(List voters, Double threshold) {
        AssertUtil.assertTrue(threshold >= 0D && threshold <= 1D,"错误的阈值");
        this.voters = voters;
        this.threshold = threshold;
    }

    @Override
    public int vote(T obj) {
        int gc = 0,dc = 0;
        for (Voter voter : voters) {
            int vote = voter.vote(obj);
            switch (vote) {
                case ACCESS_GRANTED -> gc++;
                case ACCESS_DENIED -> dc++;
            }
            if(threshold.compareTo(0D) == 0 && gc > 0) {
                return ACCESS_GRANTED;
            } else if(threshold.compareTo(1D) == 0 && dc > 0) {
                return ACCESS_DENIED;
            } else if(threshold.compareTo(0D) == 0 && dc == voters.size()) {
                return ACCESS_DENIED;
            } else if(threshold.compareTo(1D) == 0 && gc == voters.size()) {
                return ACCESS_GRANTED;
            } else if(threshold.compareTo(0D) > 0 && threshold.compareTo(1D) < 0) {
                /**
                 * 并不会将所有投票器执行完毕进行唱票
                 * 只要通过的票数超过了阈值 或者 否决的票数超过了 1 - 阈值 则直接产出结果;
                 */
                if((Double.valueOf(gc)/voters.size()) >= threshold) {
                    return ACCESS_GRANTED;
                }
                if((Double.valueOf(dc)/voters.size()) > (1-threshold)) {
                    return ACCESS_DENIED;
                }
            }
        }
        throw new BaseServiceRunException(CUSTOM,"表决错误");
    }

    public Double getThreshold() {
        return threshold;
    }

    public List getVoters() {
        return voters;
    }

    public void setVoters(List voters) {
        this.voters = voters;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy