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

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

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2014-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.List;
import java.util.Objects;
import java.util.Optional;

import br.com.objectos.way.code.Code;

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

/**
 * @author [email protected] (Marcio Endo)
 */
class ConstructorStatementPojoProperty
    extends PojoProperty
    implements
    PojoPropertyConstructorStatementBuilder,
    PojoPropertyConstructorStatementBuilder.BuilderStatement {

  private final Property property;

  private final CodeBlock.Builder body = CodeBlock.builder();

  public ConstructorStatementPojoProperty(Property property) {
    this.property = property;
  }

  @Override
  public PojoProperty build() {
    return this;
  }

  @Override
  public Add add(String statement) {
    Objects.requireNonNull(statement);
    return new AddPojo(statement);
  }

  @Override
  public BuilderStatement assignment() {
    body.addStatement("$L = builder.___get___$L()", property.name(), property.name());
    return this;
  }

  @Override
  public BuilderStatement builderGetAssignment() {
    body.addStatement("$L = builder.___get___$L()", property.name(), property.name());
    return this;
  }

  @Override
  public BuilderStatement nullCheck() {
    Optional maybeNullCheck = Code.nullCheck(property.typeName(), property.name());
    if (maybeNullCheck.isPresent()) {
      body.add(maybeNullCheck.get());
    }
    return this;
  }

  @Override
  public BuilderStatement parameterAssignment() {
    body.addStatement("this.$L = $L", property.name(), property.name());
    return this;
  }

  @Override
  void acceptConstructor(PojoConstructor constructor) {
    constructor.addParameter(ParameterSpec.builder(property.typeName(), property.name()).build());
    constructor.addCode(body.build());
  }

  private class AddPojo implements Add {

    private final String statement;
    private final List parameterList = new ArrayList<>();

    public AddPojo(String statement) {
      this.statement = statement;
    }

    @Override
    public Add add(String statement) {
      innerBuilder();
      return null;
    }

    @Override
    public PojoProperty build() {
      innerBuilder();
      return ConstructorStatementPojoProperty.this.build();
    }

    @Override
    public BuilderStatement builderGetAssignment() {
      return ConstructorStatementPojoProperty.this.builderGetAssignment();
    }

    @Override
    public BuilderStatement nullCheck() {
      return ConstructorStatementPojoProperty.this.nullCheck();
    }

    @Override
    public BuilderStatement parameterAssignment() {
      return ConstructorStatementPojoProperty.this.parameterAssignment();
    }

    @Override
    public Add set(Object arg) {
      Objects.requireNonNull(arg);
      parameterList.add(arg);
      return this;
    }

    @Override
    public Add setBuilderGet() {
      parameterList.add("builder.___get___" + property.name() + "()");
      return this;
    }

    @Override
    public Add setPropertyName() {
      parameterList.add(property.name());
      return this;
    }

    private void innerBuilder() {
      Object[] args = parameterList.toArray();
      body.addStatement(statement, args);
      parameterList.clear();
    }

  }

}