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

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

package cdc.issues.checks;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import cdc.issues.Issue;
import cdc.issues.IssueUtils;
import cdc.issues.IssuesCollector;
import cdc.issues.Params;
import cdc.issues.io.SnapshotData;
import cdc.issues.locations.Location;
import cdc.issues.rules.Profile;
import cdc.util.lang.Checks;

/**
 * Interface used to build a snapshot and associated data.
 *
 * @author Damien Carbonne
 */
public class SnapshotManager implements SnapshotData {
    private final String projectName;
    private final String projectDescription;
    private final Params projectMetas;
    private final Profile profile;
    private final String snapshotName;
    private final String snapshotDescription;
    private final Params snapshotMetas;
    private final Instant snapshotTimestamp;

    /** The issues collector. */
    private final IssuesCollector issuesCollector;
    /** The stats collector. */
    private final CheckStats stats;
    /** The registered checkers. */
    private final Map> nameToChecker = new HashMap<>();

    protected SnapshotManager(Builder builder) {
        this.projectName = builder.projectName;
        this.projectDescription = builder.projectDescription;
        this.projectMetas = builder.projectMetas;
        this.profile = builder.profile;
        this.snapshotName = builder.snapshotName;
        this.snapshotDescription = builder.snapshotDescription;
        this.snapshotMetas = builder.snapshotMetas;
        this.snapshotTimestamp = builder.snapshotTimestamp;
        this.issuesCollector = builder.issuesCollector == null ? new IssuesCollector<>() : builder.issuesCollector;
        this.stats = builder.stats;
    }

    @Override
    public final String getProjectName() {
        return projectName;
    }

    @Override
    public final String getProjectDescription() {
        return projectDescription;
    }

    @Override
    public final Params getProjectMetas() {
        return projectMetas;
    }

    @Override
    public final Optional getProfile() {
        return Optional.of(profile);
    }

    @Override
    public final String getSnapshotName() {
        return snapshotName;
    }

    @Override
    public final String getSnapshotDescription() {
        return snapshotDescription;
    }

    @Override
    public final Params getSnapshotMetas() {
        return snapshotMetas;
    }

    @Override
    public final Instant getSnapshotTimestamp() {
        return snapshotTimestamp;
    }

    @Override
    public final int getNumberOfIssues() {
        return issuesCollector.getIssues().size();
    }

    @Override
    public final String getIssuesHash() {
        return IssueUtils.getHash(issuesCollector.getIssues());
    }

    /**
     * @return The {@link IssuesCollector} used to collect detected issues.
     */
    public final IssuesCollector getIssuesCollector() {
        return issuesCollector;
    }

    /**
     * @return The object used to collect statistics.
     */
    public final CheckStats getStats() {
        return stats;
    }

    public final boolean hasStats() {
        return getStats() != null;
    }

    /**
     * Registers a checker and associate to a name.
     *
     * @param  The checked object type.
     * @param name The name used to designate the registered checker.
     * @param checker The checker.
     */
    public final  void register(String name,
                                   AbstractChecker checker) {
        Checks.isNotNull(checker, "checker");
        Checks.doesNotContainKey(nameToChecker, name, "A checker is already registered as " + name);

        nameToChecker.put(name, checker);
    }

    /**
     * Returns the checker that was registered with a name.
     *
     * @param  The checked object type.
     * @param objectClass The checked object class.
     * @param name The name of the checker to retrieve.
     * @return The corresponding checker.
     */
    public final  Optional> resolve(Class objectClass,
                                                          String name) {
        final AbstractChecker tmp = nameToChecker.get(name);
        if (tmp == null || tmp.getObjectClass().isAssignableFrom(objectClass)) {
            @SuppressWarnings("unchecked")
            final AbstractChecker x = (AbstractChecker) tmp;
            return Optional.of(x);
        } else {
            throw new IllegalArgumentException(tmp.getObjectClass().getSimpleName() + " is not compliant with "
                    + objectClass.getSimpleName());
        }
    }

    public static Builder builder() {
        return new Builder<>();
    }

    /**
     * Builder of SnapshotManager.
     *
     * @param  The builder type.
     */
    public static class Builder> {
        private String projectName;
        private String projectDescription;
        private Params projectMetas = Params.NO_PARAMS;
        private Profile profile;
        private String snapshotName;
        private String snapshotDescription;
        private Params snapshotMetas = Params.NO_PARAMS;
        private Instant snapshotTimestamp = Instant.now();
        private IssuesCollector issuesCollector;
        private CheckStats stats;

        protected Builder() {
        }

        @SuppressWarnings("unchecked")
        protected B self() {
            return (B) this;
        }

        public final B projectName(String projectName) {
            this.projectName = projectName;
            return self();
        }

        public final B projectDescription(String projectDescription) {
            this.projectDescription = projectDescription;
            return self();
        }

        public final B projectMetas(Params projectMetas) {
            this.projectMetas = projectMetas;
            return self();
        }

        public final B profile(Profile profile) {
            this.profile = profile;
            return self();
        }

        public final B snapshotName(String snapshotName) {
            this.snapshotName = snapshotName;
            return self();
        }

        public final B snapshotDescription(String snapshotDescription) {
            this.snapshotDescription = snapshotDescription;
            return self();
        }

        public final B snapshotMetas(Params snapshotMetas) {
            this.snapshotMetas = snapshotMetas;
            return self();
        }

        public final B snapshotTimestamp(Instant snapshotTimestamp) {
            this.snapshotTimestamp = snapshotTimestamp;
            return self();
        }

        public final B issuesCollector(IssuesCollector issuesCollector) {
            this.issuesCollector = issuesCollector;
            return self();
        }

        public final B stats(CheckStats stats) {
            this.stats = stats;
            return self();
        }

        public final B stats(boolean stats) {
            this.stats = stats ? new CheckStats<>() : null;
            return self();
        }

        public SnapshotManager build() {
            return new SnapshotManager(this);
        }
    }
}