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

net.neoremind.fountain.Predicates Maven / Gradle / Ivy

package net.neoremind.fountain;

import java.util.Arrays;
import java.util.List;

/**
 * 条件验证的逻辑工厂
 *
 * @author zhangxu
 */
class Predicates {

    /**
     * 返回多个条件验证,用或来做逻辑
     *
     * @param components 多个条件验证
     * @param         待验证对象类型
     *
     * @return 或逻辑的条件验证
     */
    public static  OrPredicate or(ClosurePredicate... components) {
        return new OrPredicate(Arrays.asList(components));
    }

    /**
     * 返回多个条件验证,用与来做逻辑
     *
     * @param components 多个条件验证
     * @param         待验证对象类型
     *
     * @return 与逻辑的条件验证
     */
    public static  AndPredicate and(ClosurePredicate... components) {
        return new AndPredicate(Arrays.asList(components));
    }

    /**
     * 或逻辑的条件验证类
     *
     * @param  待验证对象类型
     */
    private static class OrPredicate implements ClosurePredicate {
        private final List> components;

        private OrPredicate(List> components) {
            this.components = components;
        }

        @Override
        public boolean apply() {
            for (int i = 0; i < components.size(); i++) {
                if (components.get(i).apply()) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 与逻辑的条件验证类
     *
     * @param  待验证对象类型
     */
    private static class AndPredicate implements ClosurePredicate {
        private final List> components;

        private AndPredicate(List> components) {
            this.components = components;
        }

        @Override
        public boolean apply() {
            for (int i = 0; i < components.size(); i++) {
                if (!components.get(i).apply()) {
                    return false;
                }
            }
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy