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

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

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

import com.squareup.javapoet.TypeName;
import net.zerobuilder.compiler.generate.DtoBeanGoal.BeanGoalContext;
import net.zerobuilder.compiler.generate.DtoContext.BuildersContext;
import net.zerobuilder.compiler.generate.DtoGoalDetails.AbstractGoalDetails;
import net.zerobuilder.compiler.generate.DtoProjectedRegularGoalContext.ProjectedRegularGoalContext;
import net.zerobuilder.compiler.generate.DtoRegularGoalContext.RegularGoalContext;
import net.zerobuilder.compiler.generate.DtoStep.AbstractStep;

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

import static java.util.Collections.unmodifiableList;
import static net.zerobuilder.compiler.generate.DtoProjectedRegularGoalContext.projectedRegularGoalContextCases;
import static net.zerobuilder.compiler.generate.DtoRegularGoalContext.regularGoalContextCases;

final class DtoGoalContext {

  static abstract class AbstractGoalContext {

    abstract  R accept(GoalCases cases);

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

    public final String name() {
      return goalName.apply(this);
    }

    final AbstractGoalDetails details() {
      return abstractGoalDetails.apply(this);
    }

    public final BuildersContext context() {
      return context.apply(this);
    }

    final TypeName goalType() {
      return goalType.apply(this);
    }
  }

  interface GoalCases {
    R regularGoal(RegularGoalContext goal);
    R beanGoal(BeanGoalContext goal);
  }

  static  Function asFunction(final GoalCases cases) {
    return goal -> goal.accept(cases);
  }

  static  Function goalCases(
      Function regularFunction,
      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 final Function regularContext =
      regularGoalContextCases(
          DtoRegularGoal.regularGoalContextCases(
              constructor -> constructor.context,
              method -> method.context,
              staticMethod -> staticMethod.context),
          projectedRegularGoalContextCases(
              method -> method.context,
              constructor -> constructor.context));

  static final Function context =
      goalCases(
          regularContext,
          bean -> bean.context);

  private static final Function PROJECTED_REGULAR_GOAL_CONTEXT_R_FUNCTION = projectedRegularGoalContextCases(
      method -> method.details,
      constructor -> constructor.details);

  private static final Function REGULAR_GOAL_CONTEXT_R_FUNCTION =
      regularGoalContextCases(
          DtoRegularGoal.goalDetails,
          PROJECTED_REGULAR_GOAL_CONTEXT_R_FUNCTION);

  private static final Function abstractGoalDetails =
      goalCases(
          REGULAR_GOAL_CONTEXT_R_FUNCTION,
          bean -> bean.details);

  private static final Function goalType =
      goalCases(
          regularGoalContextCases(
              DtoRegularGoal.regularGoalContextCases(
                  constructor -> constructor.details.goalType,
                  method -> method.details.goalType,
                  staticMethod -> staticMethod.details.goalType),
              projectedRegularGoalContextCases(
                  method -> method.details.goalType,
                  constructor -> constructor.details.goalType)),
          bean -> bean.details.goalType);

  private static final Function goalName =
      goalCases(
          regularGoalContextCases(
              DtoRegularGoal.regularGoalContextCases(
                  constructor -> constructor.details.name,
                  method -> method.details.name,
                  staticMethod -> staticMethod.details.name),
              projectedRegularGoalContextCases(
                  method -> method.details.name,
                  constructor -> constructor.details.name)),
          bean -> bean.details.name);

  static final Function> abstractSteps =
      goalCases(
          regularGoalContextCases(
              DtoRegularGoal.regularGoalContextCases(
                  constructor -> constructor.steps,
                  method -> method.steps,
                  staticMethod -> staticMethod.steps),
              projectedRegularGoalContextCases(
                  method -> method.steps,
                  constructor -> constructor.steps)),
          bean -> unmodifiableList(bean.steps));

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy