cdc.issues.checks.IssuesDetector Maven / Gradle / Ivy
package cdc.issues.checks;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import cdc.issues.Issue;
import cdc.issues.IssuesHandler;
import cdc.issues.locations.Location;
import cdc.issues.rules.ConfiguredRule;
import cdc.issues.rules.Rule;
import cdc.util.lang.CollectionUtils;
/**
* Interface implemented by classes that can detect issues.
*
* @author Damien Carbonne
*
* @param The data type onto which issue can be detected.
*/
public interface IssuesDetector {
/**
* Interface implemented by classes that can describe
* and create an IssuesDetector.
*
* @author Damien Carbonne
*
* @param The data type onto which issue can be detected.
*/
public interface Factory {
/**
* @return The class of data that can be analyzed.
*/
public Class getDataClass();
/**
* @return The set of all supported Rules.
*/
public Set getSupportedRules();
/**
* Creates and configures an IssuesDetector to analyze some rules.
*
* @param project The project for which detection is run.
* @param snapshot The snapshot for which detection is run.
* @param configuredRules The configured rules for which analysis must be performed.
* Rules must be a subset of supported rules and associated
* params must comply with expected params.
* @return A newly created IssuesDetector, configured with
* {@code configuredRules} and {@code rules}.
*/
public IssuesDetector create(String project,
String snapshot,
Set configuredRules);
/**
* Creates and configures an IssuesDetector to analyze some rules.
*
* @param project The project for which detection is run.
* @param snapshot The snapshot for which detection is run.
* @param configuredRules The configured rules for which analysis must be performed.
* Rules must be a subset of supported rules and associated
* params must comply with expected params.
* @return A newly created IssuesDetector, configured with
* {@code configuredRules} and {@code rules}.
*/
public default IssuesDetector create(String project,
String snapshot,
ConfiguredRule... configuredRules) {
final Set set = new HashSet<>();
Collections.addAll(set, configuredRules);
return create(project, snapshot, set);
}
/**
* Creates and configures an IssuesDetector to analyze some rules.
*
* @param project The project for which detection is run.
* @param snapshot The snapshot for which detection is run.
* @param configuredRule The configured rule for which analysis must be performed.
* The rule must be one of supported rules and params must
* comply with {@link Rule#getParams()}.
* @return A newly created IssuesDetector, configured with
* {@code configuredRules}.
*/
public default IssuesDetector create(String project,
String snapshot,
ConfiguredRule configuredRule) {
final Set configuredRules = new HashSet<>();
configuredRules.add(configuredRule);
return create(project, snapshot, configuredRules);
}
}
public abstract static class AbstractFactory implements Factory {
private final Class dataClass;
private final Set rules;
protected AbstractFactory(Class dataClass,
Rule... rules) {
this.dataClass = dataClass;
this.rules = Collections.unmodifiableSet(CollectionUtils.toSet(rules));
}
@Override
public final Class getDataClass() {
return dataClass;
}
@Override
public final Set getSupportedRules() {
return rules;
}
}
/**
* @return The factory that was used to create this IssuesDetector.
*/
public Factory getFactory();
/**
* @return The class of data that can be analyzed.
*/
public default Class getDataClass() {
return getFactory().getDataClass();
}
/**
* @return The project name of analyzed data.
*/
public String getProject();
/**
* @return The snapshot name for which analysis is done.
*/
public String getSnapshot();
/**
* @return The set of enabled rules.
*/
public Set getEnabledRules();
/**
* @return The set of enabled ConfiguredRules.
*/
public Set getEnabledConfiguredRules();
/**
* Analyzes one data.
*
* @param data The data to analyze.
* @param locations The data locations. Must NOT be empty.
* @param issuesHandler The issues handler that must be used to store issues.
*/
public void analyze(T data,
List locations,
IssuesHandler issuesHandler);
/**
* Analyzes one data.
*
* @param data The data to analyze.
* @param location The data location.
* @param issuesHandler The issues handler that must be used to store issues.
*/
public default void analyze(T data,
Location location,
IssuesHandler issuesHandler) {
final List locations = new ArrayList<>();
locations.add(location);
analyze(data, locations, issuesHandler);
}
public static String toString(IssuesDetector> detector) {
final StringBuilder builder = new StringBuilder();
builder.append("IssuesDetector<")
.append(detector.getDataClass().getSimpleName())
.append(">(");
boolean first = true;
for (final ConfiguredRule crule : detector.getEnabledConfiguredRules()) {
if (first) {
first = false;
} else {
builder.append(",");
}
builder.append(crule);
}
builder.append(")");
return builder.toString();
}
}