net.zerobuilder.compiler.generate.DtoGoalContext Maven / Gradle / Ivy
package net.zerobuilder.compiler.generate;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import net.zerobuilder.compiler.generate.DtoBeanGoalContext.BeanGoalContext;
import net.zerobuilder.compiler.generate.DtoBuildersContext.BuildersContext;
import net.zerobuilder.compiler.generate.DtoGoal.AbstractGoalDetails;
import net.zerobuilder.compiler.generate.DtoGoal.RegularGoalDetails;
import net.zerobuilder.compiler.generate.DtoRegularGoalContext.RegularGoalContext;
import net.zerobuilder.compiler.generate.DtoStep.AbstractStep;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static net.zerobuilder.compiler.generate.DtoRegularGoalContext.regularSteps;
import static net.zerobuilder.compiler.generate.Utilities.upcase;
final class DtoGoalContext {
interface IGoal {
AbstractGoalContext withContext(BuildersContext context);
}
interface AbstractGoalContext {
R accept(GoalCases cases);
}
interface GoalCases {
R regularGoal(RegularGoalContext goal);
R beanGoal(BeanGoalContext goal);
}
static Function asFunction(final GoalCases cases) {
return goal -> goal.accept(cases);
}
static Function
goalCases(final Function regularFunction,
final Function beanFunction) {
return asFunction(new GoalCases() {
@Override
public R regularGoal(RegularGoalContext goal) {
return regularFunction.apply(goal);
}
@Override
public R beanGoal(BeanGoalContext goal) {
return beanFunction.apply(goal);
}
});
}
static List stepInterfaceTypes(AbstractGoalContext goal) {
List specs = new ArrayList<>();
for (AbstractStep abstractStep : abstractSteps.apply(goal)) {
specs.add(abstractStep.thisType);
}
return specs;
}
static final Function buildersContext
= asFunction(new GoalCases() {
@Override
public BuildersContext regularGoal(RegularGoalContext goal) {
return DtoRegularGoalContext.buildersContext.apply(goal);
}
@Override
public BuildersContext beanGoal(BeanGoalContext goal) {
return goal.builders;
}
});
static ClassName builderImplType(AbstractGoalContext goal) {
return buildersContext.apply(goal).generatedType.nestedClass(
upcase(goalName.apply(goal) + "BuilderImpl"));
}
static final Function goalType =
asFunction(new GoalCases() {
@Override
public TypeName regularGoal(RegularGoalContext goal) {
RegularGoalDetails regularGoalDetails = DtoRegularGoalContext.regularGoal.apply(goal);
return regularGoalDetails.goalType;
}
@Override
public TypeName beanGoal(BeanGoalContext goal) {
return goal.goal.details.goalType;
}
});
static final Function goalName = asFunction(new GoalCases() {
@Override
public String regularGoal(RegularGoalContext goal) {
RegularGoalDetails regularGoalDetails = DtoRegularGoalContext.regularGoal.apply(goal);
return regularGoalDetails.name;
}
@Override
public String beanGoal(BeanGoalContext goal) {
return goal.goal.details.name;
}
});
static final Function abstractGoalDetails
= asFunction(new GoalCases() {
@Override
public AbstractGoalDetails regularGoal(RegularGoalContext goal) {
return DtoRegularGoalContext.regularGoal.apply(goal);
}
@Override
public AbstractGoalDetails beanGoal(BeanGoalContext goal) {
return goal.goal.details;
}
});
static final Function> abstractSteps
= asFunction(new GoalCases>() {
@Override
public List regularGoal(RegularGoalContext goal) {
return unmodifiableList(regularSteps.apply(goal));
}
@Override
public List beanGoal(BeanGoalContext goal) {
return unmodifiableList(goal.goal.steps);
}
});
static final Function> thrownTypes
= asFunction(new GoalCases>() {
@Override
public List regularGoal(RegularGoalContext goal) {
return DtoRegularGoalContext.thrownTypes.apply(goal);
}
@Override
public List beanGoal(BeanGoalContext goal) {
return emptyList();
}
});
static ClassName contractName(String goalName, ClassName generatedType) {
return generatedType.nestedClass(upcase(goalName + "Builder"));
}
private DtoGoalContext() {
throw new UnsupportedOperationException("no instances");
}
}