cdc.issues.checks.LazyChecker Maven / Gradle / Ivy
package cdc.issues.checks;
import java.io.PrintStream;
import cdc.issues.locations.Location;
import cdc.util.debug.Printables;
/**
* Implementation of {@link AbstractChecker} that lazily delegate the work to a named checker.
*
* This must be used to define recursive checkers;
*
* @param The checked object type.
*/
public final class LazyChecker extends AbstractChecker {
/** The name of the referenced checker. */
private final String name;
/** The referenced checker. */
private AbstractChecker delegate = null;
public LazyChecker(SnapshotManager manager,
Class objectClass,
String name) {
super(manager,
objectClass);
this.name = name;
}
private AbstractChecker resolve() {
if (delegate == null) {
this.delegate = getManager().resolve(getObjectClass(), name).orElseThrow();
}
return delegate;
}
@Override
public void print(PrintStream out,
int level) {
Printables.indent(out, level);
out.println("LAZY<" + getObjectClass().getSimpleName() + "> --> " + name);
}
@Override
public boolean isEnabled() {
return resolve().isEnabled();
}
@Override
public final CheckResult check(CheckContext context,
O object,
Location location) {
return resolve().check(context, object, location);
}
}