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

cdc.issues.checks.AbstractRuleChecker Maven / Gradle / Ivy

package cdc.issues.checks;

import java.io.PrintStream;

import cdc.issues.Issue;
import cdc.issues.locations.LocatedObject;
import cdc.issues.rules.Rule;
import cdc.util.debug.Printables;

/**
 * Abstract leaf ({@link Rule}) checker.
 *
 * @author Damien Carbonne
 *
 * @param  The checked object type.
 */
public abstract class AbstractRuleChecker extends AbstractChecker {
    private final Rule rule;

    protected AbstractRuleChecker(SnapshotManager manager,
                                  Class objectClass,
                                  Rule rule) {
        super(manager,
              objectClass);
        this.rule = rule;
    }

    public Rule getRule() {
        return rule;
    }

    /**
     * @param object The object.
     * @return A string designating the checked object or object part.
     */
    protected abstract String getHeader(O object);

    protected final String getHeader(LocatedObject data) {
        return getHeader(data.getObject());
    }

    @Override
    public final boolean isEnabled() {
        return getManager().getProfile().orElseThrow().isEnabled(rule);
    }

    @Override
    public String toString() {
        return "RULE (" + rule.getName() + ") "
                + (getManager().getProfile().orElseThrow().isEnabled(rule)
                        ? "ENABLED"
                        : "DISABLED");
    }

    protected Issue.Builder issue() {
        return Issue.builder()
                    .project(getManager().getProjectName())
                    .rule(rule);
    }

    @Override
    public void print(PrintStream out,
                      int level) {
        Printables.indent(out, level);
        out.println("RULE<" + getObjectClass().getSimpleName() + "> "
                + rule.getName() + " " + (isEnabled() ? "ENABLED" : "DISABLED"));
    }

    protected final void add(Issue issue) {
        getManager().getIssuesCollector().issue(issue);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy