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

cdc.mf.checks.atts.cardinality.AbstractCardinalityUpperBoundMustBeAmong Maven / Gradle / Ivy

The newest version!
package cdc.mf.checks.atts.cardinality;

import java.util.List;

import cdc.issues.IssueSeverity;
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.FormatUtils;
import cdc.mf.checks.IssueDescription;
import cdc.mf.checks.MfAbstractRuleChecker;
import cdc.mf.model.MfCardinality;
import cdc.mf.model.MfCardinalityItem;

public abstract class AbstractCardinalityUpperBoundMustBeAmong extends MfAbstractRuleChecker {
    public static final IssueSeverity SEVERITY = IssueSeverity.MAJOR;

    protected static String describe(String article,
                                     String item,
                                     List values) {
        return RuleDescription.format("When the effective {%wrap} of {%lower}{%wrap} is valid, its {%wrap} must be "
                + FormatUtils.chooseBound(values) + ".",
                                      N_CARDINALITY,
                                      article,
                                      item,
                                      N_UPPER + WS + N_BOUND);
    }

    protected static String describe(String article1,
                                     String item1,
                                     String article2,
                                     String item2,
                                     List values) {
        return RuleDescription.format("When the effective {%wrap} of {%lower}{%wrap} {%lower}{%wrap} is valid, its {%wrap} must be "
                + FormatUtils.chooseBound(values) + ".",
                                      N_CARDINALITY,
                                      article1,
                                      item1,
                                      article2,
                                      item2,
                                      N_UPPER + WS + N_BOUND);
    }

    protected static String describe(String article,
                                     String item,
                                     int value) {
        return describe(article, item, List.of(value));
    }

    private final List values;

    protected AbstractCardinalityUpperBoundMustBeAmong(SnapshotManager manager,
                                                       Class objectClass,
                                                       Rule rule,
                                                       List values) {
        super(manager,
              objectClass,
              rule);
        this.values = List.copyOf(values);
    }

    protected AbstractCardinalityUpperBoundMustBeAmong(SnapshotManager manager,
                                                       Class objectClass,
                                                       Rule rule,
                                                       int value) {
        this(manager,
             objectClass,
             rule,
             List.of(value));
    }

    private boolean isOneOf(int value) {
        return values.contains(value);
    }

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

    @Override
    public CheckResult check(CheckContext context,
                             O object,
                             Location location) {
        final MfCardinality cardinality = object.getEffectiveCardinality();
        if (cardinality.isValid()
                && !isOneOf(cardinality.getMax())) {
            final IssueDescription.Builder description = IssueDescription.builder();

            description.header(getHeader(object))
                       .value(cardinality.getText())
                       .violation("is unexpected");

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy