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

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

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

import com.squareup.javapoet.CodeBlock;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class AbstractCodeWriter> {

  private final StringBuilder statementBuilder = new StringBuilder();
  private final List parameterList = new ArrayList<>();

  private final CodeWriterMode mode;

  AbstractCodeWriter(CodeWriterMode mode) {
    this.mode = mode;
  }

  public abstract Add add(String statement);

  public T set(Object parameter) {
    parameterList.add(parameter);
    return self();
  }

  public T setParameterName() {
    List parameterInfoList = parameterInfoList();
    for (ParameterInfo parameterInfo : parameterInfoList) {
      parameterInfo.addName(parameterList);
    }
    return self();
  }

  public T setParameterTypeAndName() {
    List parameterInfoList = parameterInfoList();
    for (ParameterInfo parameterInfo : parameterInfoList) {
      parameterInfo.addTypeNameAndName(parameterList);
    }
    return self();
  }

  public final CodeBlock write() {
    String format = statementBuilder.toString();
    Object[] args = parameterList.toArray();
    return mode.doWrite(format, args);
  }

  abstract List parameterInfoList();

  abstract T self();

  abstract class Add {

    private final String statement;

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

    public Add add(String statement) {
      appendStatement();
      return AbstractCodeWriter.this.add(statement);
    }

    public T forEachParameter(String separator) {
      List parameterInfoList = parameterInfoList();
      String[] parts = new String[parameterInfoList.size()];
      Arrays.fill(parts, statement);

      String text = Arrays.stream(parts).collect(Collectors.joining(separator));
      statementBuilder.append(text);

      return self();
    }

    public T set(Object parameter) {
      appendStatement();
      return AbstractCodeWriter.this.set(parameter);
    }

    public T setParameterName() {
      appendStatement();
      return AbstractCodeWriter.this.setParameterName();
    }

    public T setParameterTypeAndName() {
      appendStatement();
      return AbstractCodeWriter.this.setParameterTypeAndName();
    }

    public T when(boolean condition) {
      if (condition) {
        appendStatement();
      }
      return AbstractCodeWriter.this.self();
    }

    void appendStatement() {
      statementBuilder.append(statement);
    }

  }

}