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

br.com.objectos.way.code.ConstructorInfoConstructorWriter 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.way.code;

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

import java.util.Collection;
import java.util.List;

import br.com.objectos.way.core.util.WayIterables;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;

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

  private final ConstructorInfo constructorInfo;
  private AccessInfo accessInfo;

  private final List parameterSpecLIst = newArrayList();
  private final List codeBlockList = newArrayList();

  private ConstructorInfoConstructorWriter(ConstructorInfo constructorInfo, AccessInfo accessInfo) {
    this.constructorInfo = constructorInfo;
    this.accessInfo = accessInfo;
  }

  static ConstructorInfoConstructorWriter get(ConstructorInfo constructorInfo) {
    AccessInfo accessInfo = constructorInfo.accessInfo();
    return new ConstructorInfoConstructorWriter(constructorInfo, accessInfo);
  }

  public ConstructorInfoConstructorWriter accessInfo(AccessInfo accessInfo) {
    this.accessInfo = accessInfo;
    return this;
  }

  public ConstructorInfoConstructorWriter addCode(CodeBlock code) {
    codeBlockList.add(code);
    return this;
  }

  public ConstructorInfoConstructorWriter addCode(Collection codes) {
    codeBlockList.addAll(codes);
    return this;
  }

  public ConstructorInfoConstructorWriter addParameter(TypeName typeName, String name) {
    ParameterSpec parameterSpec = ParameterSpec.builder(typeName, name).build();
    parameterSpecLIst.add(parameterSpec);
    return this;
  }

  public ConstructorInfoConstructorWriter addParameterList() {
    List thatParameterSpecList = WayIterables.from(constructorInfo.parameterInfoList())
        .transform(ParameterInfoToParameterSpec.get())
        .toImmutableList();
    parameterSpecLIst.addAll(thatParameterSpecList);
    return this;
  }

  public MethodSpec write() {
    MethodSpec.Builder b = MethodSpec.constructorBuilder();

    b.addModifiers(accessInfo.modifiers());

    for (ParameterSpec parameterSpec : parameterSpecLIst) {
      b.addParameter(parameterSpec);
    }

    for (CodeBlock codeBlock : codeBlockList) {
      b.addCode(codeBlock);
    }

    return b.build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy