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

br.com.objectos.schema.info.ColumnInfoMethodInfo Maven / Gradle / Ivy

There is a newer version: 0.3.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.schema.info;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.core.util.ImmutableList;
import br.com.objectos.core.util.ImmutableMap;
import br.com.objectos.lazy.Lazy;
import br.com.objectos.schema.annotation.Name;
import br.com.objectos.schema.annotation.NotNull;
import br.com.objectos.schema.annotation.PrimaryKey;
import br.com.objectos.schema.type.BigIntColumn;
import br.com.objectos.schema.type.BitColumn;
import br.com.objectos.schema.type.BooleanColumn;
import br.com.objectos.schema.type.CharColumn;
import br.com.objectos.schema.type.DateColumn;
import br.com.objectos.schema.type.DateTimeColumn;
import br.com.objectos.schema.type.DoubleColumn;
import br.com.objectos.schema.type.FloatColumn;
import br.com.objectos.schema.type.IntColumn;
import br.com.objectos.schema.type.MediumIntColumn;
import br.com.objectos.schema.type.SmallIntColumn;
import br.com.objectos.schema.type.TimestampColumn;
import br.com.objectos.schema.type.TinyIntColumn;
import br.com.objectos.schema.type.VarcharColumn;
import br.com.objectos.testable.Equality;
import br.com.objectos.testable.Tester;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class ColumnInfoMethodInfo implements ColumnInfo {

  private static final Tester TESTER = Tester.of(ColumnInfo.class)
      .add("tableName", o -> o.tableName())
      .add("simpleName", o -> o.simpleName())
      .add("nullable", o -> o.nullable())
      .build();

  private static final Map SUPPLIERS = ImmutableMap
      . builder()
      .put(BigIntColumn.class.getName(), (t, m) -> new LongColumnInfoMethodInfo(t, m, LongColumnKind.BIGINT))
      .put(BitColumn.class.getName(), (t, m) -> new BooleanColumnInfoMethodInfo(t, m, BooleanColumnKind.BIT))
      .put(BooleanColumn.class.getName(), (t, m) -> new BooleanColumnInfoMethodInfo(t, m, BooleanColumnKind.BOOLEAN))
      .put(CharColumn.class.getName(), (t, m) -> new StringColumnInfoMethodInfo(t, m, StringColumnKind.CHAR))
      .put(DateColumn.class.getName(), LocalDateColumnInfoMethodInfo::new)
      .put(DateTimeColumn.class.getName(), (t, m) -> new LocalDateTimeColumnInfoMethodInfo(t, m, LocalDateTimeColumnKind.DATETIME))
      .put(DoubleColumn.class.getName(), DoubleColumnInfoMethodInfo::new)
      .put(FloatColumn.class.getName(), FloatColumnInfoMethodInfo::new)
      .put(MediumIntColumn.class.getName(), (t, m) -> new IntColumnInfoMethodInfo(t, m, IntColumnKind.MEDIUMINT))
      .put(IntColumn.class.getName(), (t, m) -> new IntColumnInfoMethodInfo(t, m, IntColumnKind.INT))
      .put(SmallIntColumn.class.getName(), (t, m) -> new IntColumnInfoMethodInfo(t, m, IntColumnKind.SMALLINT))
      .put(TimestampColumn.class.getName(), (t, m) -> new LocalDateTimeColumnInfoMethodInfo(t, m, LocalDateTimeColumnKind.TIMESTAMP))
      .put(TinyIntColumn.class.getName(), (t, m) -> new IntColumnInfoMethodInfo(t, m, IntColumnKind.TINYINT))
      .put(VarcharColumn.class.getName(), (t, m) -> new StringColumnInfoMethodInfo(t, m, StringColumnKind.VARCHAR))
      .build();

  private final TableNameTypeInfo tableName;
  private final MethodInfo methodInfo;

  private final Lazy simpleName = new LazySimpleName();

  ColumnInfoMethodInfo(TableNameTypeInfo tableName, MethodInfo methodInfo) {
    this.tableName = tableName;
    this.methodInfo = methodInfo;
  }

  public static ColumnInfoMethodInfo of(TableNameTypeInfo tableName, MethodInfo methodInfo) {
    SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
    String qualifiedName = returnTypeInfo.qualifiedName();

    if (!SUPPLIERS.containsKey(qualifiedName)) {
      throw new IllegalArgumentException(qualifiedName);
    }

    ColumnInfoMethodInfoSupplier supplier = SUPPLIERS.get(qualifiedName);
    return supplier.get(tableName, methodInfo);
  }

  public TypeName annotationTypeName() {
    ClassName tableClassName = tableName.className();
    return tableClassName.nestedClass(methodInfo.name());
  }

  public Optional annotationInfo(Class type) {
    return methodInfo.annotationInfo(type);
  }

  public void compilationError(String message) {
    methodInfo.compilationError(message);
  }

  @Override
  public ThisGenerationInfo generationInfo() {
    return ThisNoGenerationInfo.get();
  }

  public boolean hasMethodName(Set nameSet) {
    return nameSet.contains(methodInfo.name());
  }

  @Override
  public Equality isEqualTo(Object o) {
    return TESTER.test(this, o);
  }

  public boolean primaryKey() {
    return methodInfo.hasAnnotation(PrimaryKey.class);
  }

  @Override
  public boolean nullable() {
    return !methodInfo.hasAnnotation(NotNull.class);
  }

  @Override
  public String simpleName() {
    return simpleName.get();
  }

  @Override
  public TableNameTypeInfo tableName() {
    return tableName;
  }

  public ForeignKeyPartMethodInfo toForeignKeyPart(Map nameMap) {
    ColumnInfoMethodInfo columnInfo = nameMap.get(simpleName());
    return new ForeignKeyPartMethodInfo(columnInfo, methodInfo);
  }

  public KeyPartColumnInfoMethodInfo toKeyPart() {
    return new KeyPartColumnInfoMethodInfo(this);
  }

  public MigrationColumnMethod toMigrationColumnMethod() {
    ColumnName columnName = tableName.migrationColumnName(methodInfo);
    return new MigrationColumnMethod(this, columnName);
  }

  public SchemaColumnMethod toSchemaColumnMethod(TableInfoTypeInfo tableInfo) {
    ColumnName columnName = tableName.schemaColumnName(methodInfo);
    List referencedColumnInfoList = tableInfo.referencedColumnInfoList(methodInfo.name());
    return new SchemaColumnMethod(this, columnName, referencedColumnInfoList);
  }

  public List valueMethodList(ClassName className) {
    return ImmutableList.of(withValueMethod(className));
  }

  abstract Class comparisonOperatorClass();

  abstract Enum comparisonOperatorDefaultValue();

  MethodInfo methodInfo() {
    return methodInfo;
  }

  MethodSpec nullValueMethod(ClassName innerClassName) {
    return MethodSpec.methodBuilder("nullValue")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .returns(innerClassName)
        .addStatement("return new $T()", innerClassName)
        .build();
  }

  abstract TypeName valueTypeName();

  MethodSpec withValueMethod(ClassName innerClassName) {
    return MethodSpec.methodBuilder("withValue")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .addParameter(valueTypeName(), "value")
        .returns(innerClassName)
        .addStatement("return new $T(value)", innerClassName)
        .build();
  }

  private class LazySimpleName extends Lazy {
    @Override
    protected String compute() {
      return methodInfo.annotationInfo(Name.class)
          .flatMap(annotationInfo -> annotationInfo.stringValue("value"))
          .orElse(methodInfo.name());
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy