All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.zerobuilder.compiler.generate.DtoBeanStep Maven / Gradle / Ivy

There is a newer version: 1.603
Show newest version
package net.zerobuilder.compiler.generate;

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.DtoContext.BuildersContext;
import net.zerobuilder.compiler.generate.DtoGoalDetails.AbstractGoalDetails;
import net.zerobuilder.compiler.generate.DtoStep.AbstractStep;
import net.zerobuilder.compiler.generate.DtoStep.CollectionInfo;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static net.zerobuilder.compiler.generate.ZeroUtil.parameterSpec;
import static net.zerobuilder.compiler.generate.ZeroUtil.upcase;

public final class DtoBeanStep {

  interface BeanStepCases {
    R accessorPair(AccessorPairStep step);
    R loneGetter(LoneGetterStep step);
  }

  static  Function asFunction(final BeanStepCases cases) {
    return abstractStep -> abstractStep.acceptBean(cases);
  }

  public static  Function beanStepCases(Function accessorPair,
                                                                Function loneGetter) {
    return asFunction(new BeanStepCases() {
      @Override
      public R accessorPair(AccessorPairStep step) {
        return accessorPair.apply(step);
      }
      @Override
      public R loneGetter(LoneGetterStep step) {
        return loneGetter.apply(step);
      }
    });
  }

  public static abstract class AbstractBeanStep extends AbstractStep {
    AbstractBeanStep(String thisType,
                     Optional nextType,
                     AbstractGoalDetails goalDetails,
                     BuildersContext context) {
      super(thisType, nextType, goalDetails, context);
    }
    @Override
    final  R accept(DtoStep.StepCases cases) {
      return cases.beanStep(this);
    }
    abstract  R acceptBean(BeanStepCases cases);

    public final List getterThrownTypes() {
      return getterThrownTypes.apply(this);
    }

    public final List setterThrownTypes() {
      return setterThrownTypes.apply(this);
    }

    public final AbstractBeanParameter beanParameter() {
      return validBeanParameter.apply(this);
    }

  }

  public static final class AccessorPairStep extends AbstractBeanStep {
    public final AccessorPair accessorPair;

    private AccessorPairStep(String thisType,
                             Optional nextType,
                             AbstractGoalDetails goalDetails,
                             BuildersContext context,
                             AccessorPair accessorPair) {
      super(thisType, nextType, goalDetails, context);
      this.accessorPair = accessorPair;
    }

    static AccessorPairStep create(String thisType,
                                   Optional nextType,
                                   AbstractGoalDetails goalDetails,
                                   BuildersContext context,
                                   AccessorPair accessorPair) {
      return new AccessorPairStep(thisType, nextType, goalDetails, context, accessorPair);
    }

    public Optional emptyOption() {
      String name = accessorPair.name();
      return CollectionInfo.create(accessorPair.type, name);
    }


    public ParameterSpec parameter() {
      return parameterSpec(accessorPair.type, accessorPair.name());
    }

    @Override
     R acceptBean(BeanStepCases cases) {
      return cases.accessorPair(this);
    }
  }

  public static final class LoneGetterStep extends AbstractBeanStep {
    public final LoneGetter loneGetter;
    public final String emptyMethod;

    private LoneGetterStep(String thisType,
                           Optional nextType,
                           AbstractGoalDetails goalDetails,
                           BuildersContext context,
                           LoneGetter loneGetter,
                           String emptyMethod) {
      super(thisType, nextType, goalDetails, context);
      this.loneGetter = loneGetter;
      this.emptyMethod = emptyMethod;
    }

    static LoneGetterStep create(String thisType,
                                 Optional nextType,
                                 AbstractGoalDetails goalDetails,
                                 BuildersContext context,
                                 LoneGetter loneGetter) {
      String emptyMethod = "empty" + upcase(loneGetter.name());
      return new LoneGetterStep(thisType, nextType, goalDetails, context, loneGetter, emptyMethod);
    }

    @Override
     R acceptBean(BeanStepCases cases) {
      return cases.loneGetter(this);
    }
  }

  private static final Function validBeanParameter
      = beanStepCases(
      step -> step.accessorPair,
      step -> step.loneGetter);

  private static final Function> getterThrownTypes
      = beanStepCases(step -> step.accessorPair.getterThrownTypes,
      step -> step.loneGetter.getterThrownTypes);

  private static final Function> setterThrownTypes
      = beanStepCases(step -> step.accessorPair.setterThrownTypes,
      step -> Collections.emptyList());

  private DtoBeanStep() {
    throw new UnsupportedOperationException("no instances");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy