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

cdc.mf.checks.nodes.misc.AbstractElementMustNotOwnAny Maven / Gradle / Ivy

The newest version!
package cdc.mf.checks.nodes.misc;

import java.util.List;

import cdc.issues.checks.CheckContext;
import cdc.issues.checks.CheckResult;
import cdc.issues.checks.SnapshotManager;
import cdc.issues.locations.Location;
import cdc.issues.rules.Rule;
import cdc.issues.rules.RuleDescription;
import cdc.mf.checks.IssueDescription;
import cdc.mf.checks.MfAbstractRuleChecker;
import cdc.mf.model.MfElement;

public abstract class AbstractElementMustNotOwnAny extends MfAbstractRuleChecker {
    protected static String describe(String owner,
                                     String other) {
        return RuleDescription.format("All {%wrap} must not own any {%wrap}.",
                                      owner,
                                      other);
    }

    private final Class otherClass;

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

    @Override
    protected final String getHeader(O object) {
        return getTheItemHeader(object);
    }

    @Override
    public CheckResult check(CheckContext context,
                             O object,
                             Location location) {
        final List others = object.getChildren(otherClass);
        if (!others.isEmpty()) {
            final IssueDescription.Builder description = IssueDescription.builder();

            description.header(getHeader(object))
                       .violation("owns " + otherClass.getSimpleName())
                       .elements(others);

            add(issue().description(description)
                       .location(location)
                       .build());
            return CheckResult.FAILURE;
        } else {
            return CheckResult.SUCCESS;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy