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

io.yawp.repository.shields.ShieldRules Maven / Gradle / Ivy

There is a newer version: 2.08alpha
Show newest version
package io.yawp.repository.shields;

import io.yawp.repository.query.condition.BaseCondition;

import java.util.ArrayList;
import java.util.List;

public class ShieldRules {

    private List> rules = new ArrayList<>();

    private boolean merged;

    private RuleConditions conditions;

    private Class facade;

    public void add(Rule rule) {
        rules.add(rule);
    }

    public boolean hasCondition() {
        merge();
        return conditions != null;
    }

    public boolean evaluateConditions() {
        merge();
        if (conditions == null) {
            return true;
        }

        return conditions.evaluate();
    }

    public BaseCondition getWhere() {
        merge();
        return conditions.getWhere();
    }

    public boolean hasFacade() {
        merge();
        return facade != null;
    }

    public Class getFacade() {
        merge();
        return facade;
    }

    private void merge() {
        if (merged) {
            return;
        }

        boolean emptyCondition = false;

        for (Rule rule : rules) {
            if (!emptyCondition) {
                if (rule.hasConditions()) {
                    mergeConditions(rule);
                } else {
                    this.conditions = null;
                    emptyCondition = true;
                }
            }

            // last facade wins, even if it is null
            this.facade = rule.getFacade();
        }

        merged = true;
    }

    private void mergeConditions(Rule rule) {
        if (this.conditions == null) {
            this.conditions = rule.getConditions();
        } else {
            this.conditions.or(rule.getConditions().getWhere());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy