net.zerobuilder.compiler.generate.DtoProjectedRegularGoalContext Maven / Gradle / Ivy
package net.zerobuilder.compiler.generate;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.TypeName;
import net.zerobuilder.compiler.generate.DtoContext.BuildersContext;
import net.zerobuilder.compiler.generate.DtoGoalDetails.AbstractRegularDetails;
import net.zerobuilder.compiler.generate.DtoGoalDetails.ConstructorGoalDetails;
import net.zerobuilder.compiler.generate.DtoGoalDetails.StaticMethodGoalDetails;
import net.zerobuilder.compiler.generate.DtoProjectedGoal.ProjectedGoal;
import net.zerobuilder.compiler.generate.DtoProjectedGoal.ProjectedGoalCases;
import net.zerobuilder.compiler.generate.DtoRegularGoalContext.RegularGoalContext;
import java.util.List;
import java.util.function.Function;
import static net.zerobuilder.compiler.generate.DtoRegularStep.ProjectedRegularStep;
public final class DtoProjectedRegularGoalContext {
interface ProjectedRegularGoalContextCases {
R method(ProjectedMethodGoalContext method);
R constructor(ProjectedConstructorGoalContext constructor);
}
public static abstract class ProjectedRegularGoalContext extends RegularGoalContext
implements ProjectedGoal {
ProjectedRegularGoalContext(List thrownTypes) {
super(thrownTypes);
}
abstract R acceptRegularProjected(ProjectedRegularGoalContextCases cases);
@Override
public final R acceptProjected(ProjectedGoalCases cases) {
return cases.regular(this);
}
@Override
R acceptRegular(DtoRegularGoalContext.RegularGoalContextCases cases) {
return cases.projected(this);
}
public final CodeBlock invocationParameters() {
return CodeBlock.of(String.join(", ", goalDetails.apply(this).parameterNames));
}
}
static Function asFunction(ProjectedRegularGoalContextCases cases) {
return goal -> goal.acceptRegularProjected(cases);
}
public static Function projectedRegularGoalContextCases(
Function super ProjectedMethodGoalContext, ? extends R> methodFunction,
Function super ProjectedConstructorGoalContext, ? extends R> constructorFunction) {
return asFunction(new ProjectedRegularGoalContextCases() {
@Override
public R method(ProjectedMethodGoalContext method) {
return methodFunction.apply(method);
}
@Override
public R constructor(ProjectedConstructorGoalContext constructor) {
return constructorFunction.apply(constructor);
}
});
}
public static final class ProjectedMethodGoalContext extends ProjectedRegularGoalContext {
public final List steps;
public final BuildersContext context;
public final StaticMethodGoalDetails details;
ProjectedMethodGoalContext(
BuildersContext context,
StaticMethodGoalDetails details,
List steps,
List thrownTypes) {
super(thrownTypes);
this.context = context;
this.details = details;
this.steps = steps;
}
@Override
public R acceptRegularProjected(ProjectedRegularGoalContextCases cases) {
return cases.method(this);
}
}
public static final class ProjectedConstructorGoalContext
extends ProjectedRegularGoalContext {
public final BuildersContext context;
public final ConstructorGoalDetails details;
public final List steps;
ProjectedConstructorGoalContext(BuildersContext context,
ConstructorGoalDetails details,
List steps,
List thrownTypes) {
super(thrownTypes);
this.context = context;
this.details = details;
this.steps = steps;
}
@Override
public R acceptRegularProjected(ProjectedRegularGoalContextCases cases) {
return cases.constructor(this);
}
}
public static final Function goalDetails =
projectedRegularGoalContextCases(
method -> method.details,
constructor -> constructor.details);
public static final Function> steps = DtoProjectedRegularGoalContext.projectedRegularGoalContextCases(
method -> method.steps,
constructor -> constructor.steps);
private DtoProjectedRegularGoalContext() {
throw new UnsupportedOperationException("no instances");
}
}