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

net.zerobuilder.compiler.generate.DtoRegularGoalContext 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.DtoBuildersContext.BuildersContext;
import net.zerobuilder.compiler.generate.DtoGoal.ConstructorGoalDetails;
import net.zerobuilder.compiler.generate.DtoGoal.MethodGoalDetails;
import net.zerobuilder.compiler.generate.DtoGoal.RegularGoalDetails;
import net.zerobuilder.compiler.generate.DtoGoalContext.AbstractGoalContext;
import net.zerobuilder.compiler.generate.DtoGoalContext.GoalCases;
import net.zerobuilder.compiler.generate.DtoGoalContext.IGoal;
import net.zerobuilder.compiler.generate.DtoStep.RegularStep;

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

final class DtoRegularGoalContext {

  interface RegularGoalContext extends AbstractGoalContext {

     R acceptRegular(RegularGoalContextCases cases);
  }

  interface RegularGoalContextCases {
    R constructorGoal(ConstructorGoalContext goal);
    R methodGoal(MethodGoalContext goal);
  }

  static  Function asFunction(final RegularGoalContextCases cases) {
    return goal -> goal.acceptRegular(cases);
  }

  static Predicate asPredicate(final RegularGoalContextCases cases) {
    return goal -> goal.acceptRegular(cases);
  }

  static final class ConstructorGoal implements IGoal {
    final ConstructorGoalDetails details;

    final List steps;
    final List thrownTypes;

    private ConstructorGoal(ConstructorGoalDetails details,
                            List steps,
                            List thrownTypes) {
      this.steps = steps;
      this.thrownTypes = thrownTypes;
      this.details = details;
    }

    static ConstructorGoal create(ConstructorGoalDetails details,
                                  List steps,
                                  List thrownTypes) {
      return new ConstructorGoal(details, steps, thrownTypes);
    }

    @Override
    public AbstractGoalContext withContext(BuildersContext context) {
      return new ConstructorGoalContext(this, context);
    }
  }

  static final class ConstructorGoalContext
      implements RegularGoalContext {

    final ConstructorGoal goal;
    final BuildersContext builders;

    ConstructorGoalContext(ConstructorGoal goal,
                           BuildersContext builders) {
      this.goal = goal;
      this.builders = builders;
    }

    @Override
    public  R acceptRegular(RegularGoalContextCases cases) {
      return cases.constructorGoal(this);
    }

    @Override
    public  R accept(GoalCases cases) {
      return cases.regularGoal(this);
    }
  }

  static final class MethodGoal implements IGoal {
    final MethodGoalDetails details;

    final List steps;
    final List thrownTypes;

    private MethodGoal(MethodGoalDetails details,
                       List steps,
                       List thrownTypes) {
      this.steps = steps;
      this.thrownTypes = thrownTypes;
      this.details = details;
    }

    static MethodGoal create(MethodGoalDetails details,
                             List steps,
                             List thrownTypes) {
      return new MethodGoal(details, steps, thrownTypes);
    }

    @Override
    public AbstractGoalContext withContext(BuildersContext context) {
      return new MethodGoalContext(this, context);
    }
  }

  static final class MethodGoalContext
      implements RegularGoalContext {
    final BuildersContext builders;
    final MethodGoal goal;

    MethodGoalContext(MethodGoal goal,
                      BuildersContext builders) {
      this.goal = goal;
      this.builders = builders;
    }

    @Override
    public  R acceptRegular(RegularGoalContextCases cases) {
      return cases.methodGoal(this);
    }

    @Override
    public  R accept(GoalCases cases) {
      return cases.regularGoal(this);
    }
  }

  static final Function regularGoal
      = asFunction(new RegularGoalContextCases() {
    @Override
    public RegularGoalDetails constructorGoal(ConstructorGoalContext goal) {
      return goal.goal.details;
    }
    @Override
    public RegularGoalDetails methodGoal(MethodGoalContext goal) {
      return goal.goal.details;
    }
  });

  static final Predicate isInstance
      = asPredicate(new RegularGoalContextCases() {
    @Override
    public Boolean constructorGoal(ConstructorGoalContext goal) {
      return false;
    }
    @Override
    public Boolean methodGoal(MethodGoalContext goal) {
      return goal.goal.details.methodType.isInstance();
    }
  });

  static final Function buildersContext
      = asFunction(new RegularGoalContextCases() {
    @Override
    public BuildersContext constructorGoal(ConstructorGoalContext goal) {
      return goal.builders;
    }
    @Override
    public BuildersContext methodGoal(MethodGoalContext goal) {
      return goal.builders;
    }
  });

  static final Function> thrownTypes
      = asFunction(new RegularGoalContextCases>() {
    @Override
    public List constructorGoal(ConstructorGoalContext goal) {
      return goal.goal.thrownTypes;
    }
    @Override
    public List methodGoal(MethodGoalContext goal) {
      return goal.goal.thrownTypes;
    }
  });

  static final Function> regularSteps
      = asFunction(new RegularGoalContextCases>() {
    @Override
    public List constructorGoal(ConstructorGoalContext goal) {
      return goal.goal.steps;
    }
    @Override
    public List methodGoal(MethodGoalContext goal) {
      return goal.goal.steps;
    }
  });

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy