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

br.com.objectos.way.sql.Step Maven / Gradle / Ivy

/*
 * 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.sql;

import javax.lang.model.element.Modifier;

import br.com.objectos.way.db.ColumnOf;
import br.com.objectos.way.db.InsertableValue;
import br.com.objectos.way.db.Selectable;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeVariableName;

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

  private final int level;

  private final TypeVariableName t;
  private final TypeVariableName tExtendsColumnOf;
  private final TypeVariableName tExtendsColumnOfT;
  private final TypeVariableName tExtendsInsertableValue;
  private final TypeVariableName tExtendsSelectable;

  private Step(int level,
               TypeVariableName t,
               TypeVariableName tExtendsColumnOf,
               TypeVariableName tExtendsColumnOfT,
               TypeVariableName tExtendsInsertableValue,
               TypeVariableName tExtendsSelectable) {
    this.level = level;
    this.t = t;
    this.tExtendsColumnOf = tExtendsColumnOf;
    this.tExtendsColumnOfT = tExtendsColumnOfT;
    this.tExtendsInsertableValue = tExtendsInsertableValue;
    this.tExtendsSelectable = tExtendsSelectable;
  }

  public static Step of(int level) {
    return new Step(
        level,
        TypeVariableName.get("T" + level),
        TypeVariableName.get("T" + level, ParameterizedTypeName.get(ClassName.get(ColumnOf.class), Naming.Wildcard)),
        TypeVariableName.get("T" + level, ParameterizedTypeName.get(ClassName.get(ColumnOf.class), Naming.T)),
        TypeVariableName.get("T" + level, InsertableValue.class),
        TypeVariableName.get("T" + level, Selectable.class));
  }

  public CodeBlock columnAssignment() {
    String columnName = columnName();
    return CodeBlock.builder()
        .addStatement("this.$L = $L", columnName, columnName)
        .build();
  }

  public FieldSpec columnField() {
    return FieldSpec.builder(t, columnName())
        .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
        .build();
  }

  public MethodSpec columnGetter() {
    return MethodSpec.methodBuilder(columnName())
        .addModifiers(Modifier.PUBLIC)
        .returns(t)
        .addStatement("return $L", columnName())
        .build();
  }

  public String columnName() {
    return "column" + level;
  }

  public CodeBlock columnRead() {
    return CodeBlock.builder()
        .addStatement("$T $L = ($T) $L.read(rs, $L)", t, valueName(), t, columnName(), level)
        .build();
  }

  public ParameterSpec columnParameter() {
    return ParameterSpec.builder(t, columnName()).build();
  }

  public ParameterSpec insertValueParameter() {
    return ParameterSpec.builder(ParameterizedTypeName.get(Naming.InsertValue, t), valName()).build();
  }

  public TypeVariableName t() {
    return t;
  }

  public TypeVariableName tExtendsColumnOf() {
    return tExtendsColumnOf;
  }

  public TypeVariableName tExtendsColumnOfT() {
    return tExtendsColumnOfT;
  }

  public TypeVariableName tExtendsInsertableValue() {
    return tExtendsInsertableValue;
  }

  public TypeVariableName tExtendsSelectable() {
    return tExtendsSelectable;
  }

  public CodeBlock valAssignment() {
    return CodeBlock.builder()
        .addStatement("this.$L = $T.of($L)", valName(), Naming.InsertValue, valName())
        .build();
  }

  public FieldSpec valField() {
    return FieldSpec.builder(ParameterizedTypeName.get(Naming.InsertValue, t), valName())
        .addModifiers(Modifier.FINAL)
        .build();
  }

  public String valName() {
    return "val" + level;
  }

  public ParameterSpec valParameter() {
    return ParameterSpec.builder(t, valName()).build();
  }

  public String valueName() {
    return "value" + level;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy