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

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

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

import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import net.zerobuilder.compiler.generate.DtoParameter.AbstractParameter;
import net.zerobuilder.compiler.generate.DtoStep.AbstractStep;

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

import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static com.squareup.javapoet.TypeSpec.interfaceBuilder;
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.PUBLIC;
import static net.zerobuilder.compiler.generate.DtoParameter.parameterName;
import static net.zerobuilder.compiler.generate.DtoStep.abstractParameter;
import static net.zerobuilder.compiler.generate.DtoStep.declaredExceptions;
import static net.zerobuilder.compiler.generate.DtoStep.emptyOption;
import static net.zerobuilder.compiler.generate.Utilities.parameterSpec;
import static net.zerobuilder.compiler.generate.Utilities.presentInstances;

final class StepContextV {

  static final Function regularStepInterface
      = step -> interfaceBuilder(step.thisType)
          .addMethod(regularStepMethod(step))
          .addMethods(presentInstances(emptyCollection(step)))
          .addModifiers(PUBLIC)
          .build();

  private static MethodSpec regularStepMethod(AbstractStep step) {
    AbstractParameter parameter = abstractParameter.apply(step);
    String name = parameterName.apply(parameter);
    TypeName type = parameter.type;
    return methodBuilder(name)
        .returns(step.nextType)
        .addParameter(parameterSpec(type, name))
        .addExceptions(declaredExceptions.apply(step))
        .addModifiers(PUBLIC, ABSTRACT)
        .build();
  }

  private static Optional emptyCollection(AbstractStep step) {
    Optional maybeEmptyOption = emptyOption.apply(step);
    if (!maybeEmptyOption.isPresent()) {
      return Optional.empty();
    }
    DtoStep.CollectionInfo collectionInfo = maybeEmptyOption.get();
    return Optional.of(methodBuilder(collectionInfo.name)
        .returns(step.nextType)
        .addExceptions(declaredExceptions.apply(step))
        .addModifiers(PUBLIC, ABSTRACT)
        .build());
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy