cdc.issues.checks.AbstractPartsChecker Maven / Gradle / Ivy
package cdc.issues.checks;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import cdc.issues.locations.LocatedObject;
import cdc.issues.locations.Location;
import cdc.util.debug.Printables;
import cdc.util.lang.Checks;
/**
* Specialization of {@link AbstractChecker} used to check parts of an object.
*
* This checker does nothing by itself, it delegates checks to its part checkers.
*
* @author Damien Carbonne
*
* @param The checked object type.
* @param The checked part type.
* @param The checker type.
*/
public abstract class AbstractPartsChecker>
extends AbstractChecker {
/** The part class. */
private final Class partClass;
/** The part checkers. */
private final List> partCheckers = new ArrayList<>();
/**
* Creates an empty part checker.
*
* @param manager The manager.
* @param objectClass The object class.
* @param partClass The part class.
*/
protected AbstractPartsChecker(SnapshotManager manager,
Class objectClass,
Class partClass) {
super(manager,
objectClass);
this.partClass = Checks.isNotNull(partClass, "partClass");
}
/**
* Creates part checker with some part checkers.
*
* @param manager The manager.
* @param objectClass The object class.
* @param partClass The part class.
* @param partCheckers The part checkers.
*/
@SafeVarargs
protected AbstractPartsChecker(SnapshotManager manager,
Class objectClass,
Class partClass,
AbstractChecker super P>... partCheckers) {
this(manager,
objectClass,
partClass);
for (final AbstractChecker super P> partChecker : partCheckers) {
add(partChecker);
}
}
public final List> getPartCheckers() {
return partCheckers;
}
/**
* @return An instance of this checker, with the right (specialized) type.
*/
protected final C self() {
@SuppressWarnings("unchecked")
final C tmp = (C) this;
return tmp;
}
/**
* Add a new part checker.
*
* @param partChecker A part checker.
* @return This checker.
*/
public C add(AbstractChecker super P> partChecker) {
partCheckers.add(partChecker);
return self();
}
/**
* Method that must return the parts of an object.
*
* @param object The object.
* @return The object parts.
*/
protected abstract List> getParts(O object);
@Override
public final boolean isEnabled() {
return true;
}
@Override
public final CheckResult check(CheckContext context,
O object,
Location location) {
for (final LocatedObject extends P> part : getParts(object)) {
for (final AbstractChecker super P> partChecker : partCheckers) {
checkAndStat(partChecker,
context.push(part),
part);
}
}
return CheckResult.SUCCESS;
}
@Override
public void print(PrintStream out,
int level) {
Printables.indent(out, level);
out.println("PARTS<" + getObjectClass().getSimpleName() + ", " + partClass.getSimpleName() + ">");
for (final AbstractChecker super P> child : partCheckers) {
child.print(out, level + 1);
}
}
@Override
public String toString() {
return "PARTS<" + getObjectClass().getSimpleName() + ", " + partClass.getSimpleName() + ">";
}
}