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

cdc.mf.checks.atts.cardinality.AbstractCardinalityMustBeAmong 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 AbstractCardinalityMustBeAmong extends MfAbstractRuleChecker {
    public static final IssueSeverity SEVERITY = IssueSeverity.MAJOR;

    protected static String describe(String article1,
                                     String item1,
                                     String article2,
                                     String item2,
                                     MfCardinality cardinality) {
        return describe(article1, item1, article2, item2, List.of(cardinality));
    }

    protected static String describe(String article1,
                                     String item1,
                                     String article2,
                                     String item2,
                                     List cardinalities) {
        return RuleDescription.format("When the effective {%wrap} of {%lower}{%wrap} of {%lower}{%wrap} is valid, it must be "
                + FormatUtils.chooseCardinality(cardinalities) + ".",
                                      N_CARDINALITY,
                                      article1,
                                      item1,
                                      article2,
                                      item2);
    }

    private final List cardinalities;

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

    protected AbstractCardinalityMustBeAmong(SnapshotManager manager,
                                             Class objectClass,
                                             Rule rule,
                                             MfCardinality cardinality) {
        this(manager,
             objectClass,
             rule,
             List.of(cardinality));
    }

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

    private boolean isOneOf(MfCardinality cardinality) {
        for (final MfCardinality x : cardinalities) {
            if (x.equals(cardinality)) {
                return true;
            }
        }
        return false;
    }

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

            description.header(getHeader(object))
                       .value(cardinality.toExpandedString())
                       .violation("is unexpected")
                       .justifications("it must be " + FormatUtils.chooseCardinality(cardinalities));

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy