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

net.zerobuilder.modules.builder.Step Maven / Gradle / Ivy

The newest version!
package net.zerobuilder.modules.builder;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import net.zerobuilder.compiler.generate.DtoRegularGoalDescription.SimpleRegularGoalDescription;
import net.zerobuilder.compiler.generate.DtoRegularParameter.SimpleParameter;
import net.zerobuilder.compiler.generate.NullPolicy;

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

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.ZeroUtil.emptyCodeBlock;
import static net.zerobuilder.compiler.generate.ZeroUtil.nullCheck;
import static net.zerobuilder.compiler.generate.ZeroUtil.parameterSpec;
import static net.zerobuilder.compiler.generate.ZeroUtil.upcase;
import static net.zerobuilder.modules.builder.Builder.nextType;

final class Step {

  static IntFunction stepInterface(SimpleRegularGoalDescription description) {
    return i -> interfaceBuilder(upcase(description.parameters.get(i).name))
        .addMethod(stepMethod(i, description))
        .addModifiers(PUBLIC)
        .build();
  }

  private static MethodSpec stepMethod(int i, SimpleRegularGoalDescription description) {
    SimpleParameter parameter = description.parameters.get(i);
    String name = parameter.name;
    TypeName type = parameter.type;
    List thrownTypes = i == description.parameters.size() - 1 ?
        description.thrownTypes :
        Collections.emptyList();
    return methodBuilder(name)
        .returns(nextType(i, description))
        .addParameter(parameterSpec(type, name))
        .addExceptions(thrownTypes)
        .addModifiers(PUBLIC, ABSTRACT)
        .build();
  }

  static final Function nullCheck
      = parameter -> {
    if (parameter.nullPolicy == NullPolicy.ALLOW || parameter.type.isPrimitive()) {
      return emptyCodeBlock;
    }
    String name = parameter.name;
    return nullCheck(name, name);
  };

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy