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

br.com.objectos.way.pojo.plugin.BuilderClassConstructor Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show 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.way.pojo.plugin;

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

import javax.lang.model.element.Modifier;

import br.com.objectos.way.code.AccessInfo;
import br.com.objectos.way.code.ConstructorInfo;
import br.com.objectos.way.code.FieldInfo;
import br.com.objectos.way.code.ParameterInfo;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;

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

  private final ConstructorInfo constructorInfo;
  private final int size;
  private final List customFieldList;

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

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

  public MethodSpec constructor(int index) {
    return constructorInfo.constructorWriter()
        .accessInfo(AccessInfo.PUBLIC)
        .addParameterList()
        .addParameterList(customFieldList.stream()
            .map(BuilderCustomField::builderConstructorParameter)
            .collect(Collectors.toList()))
        .addCode(constructorBody(index))
        .addCode(customFieldList.stream()
            .map(BuilderCustomField::builderConstructorBody)
            .collect(Collectors.toList()))
        .addCode(constructorMarker(index))
        .write();
  }

  public Stream fieldStream(int index) {
    Stream parameter = constructorInfo
        .parameterInfoStream()
        .map(input -> input.fieldWriter()
            .modifiers(Modifier.PRIVATE)
            .prefixNameWith(constructorPrefix(index))
            .write());
    return Stream.concat(parameter, marker(index));
  }

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

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

  private CodeBlock buildReturnStatement(Naming naming, int index) {
    String parameterString = constructorInfo.parameterInfoList().stream()
        .map(parameterInfo -> constructorPrefix(index, parameterInfo.name()))
        .collect(Collectors.joining(", "));

    String customString = customFieldList.stream()
        .map(BuilderCustomField::name)
        .collect(Collectors.joining(", "));

    return CodeBlock.builder()
        .addStatement("return new $T($L$Lthis)",
            naming.pojoUnboundedTypeName(),
            customFieldList.isEmpty() ? customString : customString + ", ",
            parameterString.isEmpty() ? parameterString : parameterString + ", ")
        .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 Arrays.asList();
    }

    if (index + 1 == size) {
      return Arrays.asList();
    }

    CodeBlock marker = CodeBlock.builder()
        .addStatement(constructorPrefix(index) + " = true")
        .build();
    return Arrays.asList(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 Stream marker(int index) {
    if (size == 1) {
      return Stream.empty();
    }

    if (index + 1 == size) {
      return Stream.empty();
    }

    FieldSpec marker = FieldSpec.builder(boolean.class, constructorPrefix(index), Modifier.PRIVATE).build();
    return Stream.of(marker);
  }

  private List parameterInfoToCodeBlock(int index, ParameterInfo parameterInfo) {
    FieldInfo fieldInfo = parameterInfo.toFieldInfo();
    List code = new ArrayList<>();

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

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

    return code;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy