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

br.com.objectos.code.pojo.BuilderClassConstructor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.code.pojo;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import br.com.objectos.code.AccessInfo;
import br.com.objectos.code.Code;
import br.com.objectos.code.ConstructorInfo;
import br.com.objectos.code.ConstructorInfoConstructorWriter;
import br.com.objectos.code.FieldInfo;
import br.com.objectos.code.ParameterInfo;
import br.com.objectos.core.util.MoreCollectors;

import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
class BuilderClassConstructor {

  private final ConstructorInfo constructorInfo;
  private final int size;

  public BuilderClassConstructor(ConstructorInfo constructorInfo, int size) {
    this.constructorInfo = constructorInfo;
    this.size = size;
  }

  public CodeBlock build(Configuration configuration, int index) {
    return !last(index)
        ? buildGuardeStatement(configuration, index)
        : buildReturnStatement(configuration, index);
  }

  public MethodSpec constructor(Configuration configuration, int index) {
    List parameterList = configuration.parameterList();
    ConstructorInfoConstructorWriter writer = constructorInfo.constructorWriter()
        .accessInfo(AccessInfo.PUBLIC)
        .addParameterList(parameterList)
        .addParameterList();

    List codeBlockList = parameterList.stream()
        .map(this::parameterSpecToCodeBlock)
        .collect(MoreCollectors.toImmutableList());
    for (CodeBlock code : codeBlockList) {
      writer.addCode(code);
    }

    return writer.addCode(constructorBody(index))
        .addCode(constructorMarker(index))
        .write();
  }

  private CodeBlock assignToField(int index, ParameterInfo parameterInfo) {
    return CodeBlock.builder()
        .addStatement(constructorPrefix(index) + "$L = $L", parameterInfo.name(), parameterInfo.name())
        .build();
  }

  private CodeBlock buildGuardeStatement(Configuration configuration, int index) {
    return CodeBlock.builder()
        .beginControlFlow("if ($L)", constructorPrefix(index))
        .add(buildReturnStatement(configuration, index))
        .endControlFlow()
        .build();
  }

  private CodeBlock buildReturnStatement(Configuration configuration, int index) {
    Naming naming = configuration.pojoInfo().naming();

    List parameterList = configuration.parameterList();
    Stream customFieldStream = parameterList.stream().map(spec -> spec.name);

    List parameterInfoList = constructorInfo.parameterInfoList();
    Stream parameterStream = parameterInfoList.stream()
        .map(parameterInfo -> constructorPrefix(index, parameterInfo.name()));

    String parameterString = Stream.concat(customFieldStream, parameterStream).collect(Collectors.joining(", "));
    String finalString = parameterString.isEmpty() ? parameterString : parameterString + ", ";

    return CodeBlock.builder()
        .addStatement("return new $T($Lthis)", naming.pojoUnboundedTypeName(), finalString)
        .build();
  }

  private List constructorBody(int index) {
    return constructorInfo.parameterInfoStream()
        .map(param -> parameterInfoToCodeBlock(index, param))
        .flatMap(l -> l.stream())
        .collect(Collectors.toList());
  }

  private List constructorMarker(int index) {
    if (size == 1) {
      return ImmutableList.of();
    }

    if (index + 1 == size) {
      return ImmutableList.of();
    }

    CodeBlock marker = CodeBlock.builder()
        .addStatement(constructorPrefix(index) + " = true")
        .build();
    return ImmutableList.of(marker);
  }

  private String constructorPrefix(int index) {
    return "___constructor" + index + "___";
  }

  private String constructorPrefix(int index, String text) {
    return constructorPrefix(index) + text;
  }

  private boolean last(int index) {
    return index + 1 == size;
  }

  private List parameterInfoToCodeBlock(int index, ParameterInfo parameterInfo) {
    FieldInfo fieldInfo = parameterInfo.toFieldInfo();
    List code = newArrayList();

    Optional maybeNullCheck = fieldInfo.writeNullCheck();
    if (maybeNullCheck.isPresent()) {
      code.add(maybeNullCheck.get());
    }

    code.add(assignToField(index, parameterInfo));

    return code;
  }

  private CodeBlock parameterSpecToCodeBlock(ParameterSpec parameter) {
    CodeBlock.Builder code = CodeBlock.builder();

    Optional maybeNullCheck = Code.nullCheck(parameter.type, parameter.name);
    if (maybeNullCheck.isPresent()) {
      code.add(maybeNullCheck.get());
    }

    return code
        .addStatement("this.$L = $L", parameter.name, parameter.name)
        .build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy