cdc.issues.checks.CompositeChecker Maven / Gradle / Ivy
package cdc.issues.checks;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import cdc.issues.locations.Location;
import cdc.util.debug.Printables;
/**
* Specialization of {@link AbstractChecker} that aggregates checkers dedicated to the same object type.
*
* This checker does nothing by itself, it delegates checks to its components.
*
* @author Damien Carbonne
*
* @param The checked object type.
*/
public class CompositeChecker extends AbstractChecker {
private final List> components = new ArrayList<>();
/**
* Creates an empty composite checker.
*
* @param manager The manager.
* @param objectClass The object class.
*/
public CompositeChecker(SnapshotManager manager,
Class objectClass) {
super(manager,
objectClass);
}
/**
* Creates a composite checker with some components.
*
* @param manager The manager.
* @param objectClass The object class.
* @param components The components (checkers).
*/
@SafeVarargs
public CompositeChecker(SnapshotManager manager,
Class objectClass,
AbstractChecker super O>... components) {
this(manager,
objectClass);
for (final AbstractChecker super O> component : components) {
add(component);
}
}
/**
* Add a component checker to this composite checker.
*
* @param component The component checker.
* @return This composite checker.
*/
protected CompositeChecker add(AbstractChecker super O> component) {
components.add(component);
return this;
}
/**
* @return The components.
*/
public final List> getComponents() {
return components;
}
@Override
public final boolean isEnabled() {
return true;
}
@Override
public final CheckResult check(CheckContext context,
O object,
Location location) {
for (final AbstractChecker super O> component : components) {
checkAndStat(component,
context,
object,
location);
}
return CheckResult.SUCCESS;
}
@Override
public void print(PrintStream out,
int level) {
Printables.indent(out, level);
out.println("COMPOSITE<" + getObjectClass().getSimpleName() + ">");
for (final AbstractChecker super O> component : components) {
component.print(out, level + 1);
}
}
@Override
public String toString() {
return "COMPOSITE<" + getObjectClass().getSimpleName() + ">";
}
}