net.zerobuilder.compiler.generate.DtoBeanStep Maven / Gradle / Ivy
package net.zerobuilder.compiler.generate;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;
import net.zerobuilder.compiler.generate.DtoBeanParameter.AbstractBeanParameter;
import net.zerobuilder.compiler.generate.DtoBeanParameter.AccessorPair;
import net.zerobuilder.compiler.generate.DtoBeanParameter.LoneGetter;
import net.zerobuilder.compiler.generate.DtoStep.AbstractStep;
import net.zerobuilder.compiler.generate.DtoStep.CollectionInfo;
import java.util.Optional;
import java.util.function.Function;
import static net.zerobuilder.compiler.generate.Utilities.parameterSpec;
import static net.zerobuilder.compiler.generate.Utilities.upcase;
import static net.zerobuilder.compiler.generate.DtoBeanParameter.beanParameterName;
final class DtoBeanStep {
interface BeanStepCases {
R accessorPair(AccessorPairStep step);
R loneGetter(LoneGetterStep step);
}
static Function asFunction(final BeanStepCases cases) {
return abstractStep -> abstractStep.acceptBean(cases);
}
static abstract class AbstractBeanStep extends AbstractStep {
AbstractBeanStep(ClassName thisType, TypeName nextType) {
super(thisType, nextType);
}
@Override
final R accept(DtoStep.StepCases cases) {
return cases.beanStep(this);
}
abstract R acceptBean(BeanStepCases cases);
}
static final class AccessorPairStep extends AbstractBeanStep {
final AccessorPair accessorPair;
final String setter;
private AccessorPairStep(ClassName thisType, TypeName nextType, AccessorPair accessorPair,
String setter) {
super(thisType, nextType);
this.accessorPair = accessorPair;
this.setter = setter;
}
static AccessorPairStep create(ClassName thisType, TypeName nextType, AccessorPair accessorPair,
String setter) {
return new AccessorPairStep(thisType, nextType, accessorPair, setter);
}
Optional emptyOption() {
String name = accessorPair.accept(beanParameterName);
return CollectionInfo.create(accessorPair.type, name);
}
ParameterSpec parameter() {
return parameterSpec(accessorPair.type, accessorPair.accept(beanParameterName));
}
@Override
R acceptBean(BeanStepCases cases) {
return cases.accessorPair(this);
}
}
static final class LoneGetterStep extends AbstractBeanStep {
final LoneGetter loneGetter;
final String emptyMethod;
private LoneGetterStep(ClassName thisType, TypeName nextType, LoneGetter loneGetter, String emptyMethod) {
super(thisType, nextType);
this.loneGetter = loneGetter;
this.emptyMethod = emptyMethod;
}
static LoneGetterStep create(ClassName thisType, TypeName nextType, LoneGetter loneGetter) {
String emptyMethod = "empty" + upcase(loneGetter.accept(beanParameterName));
return new LoneGetterStep(thisType, nextType, loneGetter, emptyMethod);
}
@Override
R acceptBean(BeanStepCases cases) {
return cases.loneGetter(this);
}
}
static final BeanStepCases validBeanParameter
= new BeanStepCases() {
@Override
public AbstractBeanParameter accessorPair(AccessorPairStep step) {
return step.accessorPair;
}
@Override
public AbstractBeanParameter loneGetter(LoneGetterStep step) {
return step.loneGetter;
}
};
static final BeanStepCases> emptyOption
= new BeanStepCases>() {
@Override
public Optional accessorPair(AccessorPairStep step) {
return step.emptyOption();
}
@Override
public Optional loneGetter(LoneGetterStep step) {
return Optional.empty();
}
};
private DtoBeanStep() {
throw new UnsupportedOperationException("no instances");
}
}