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

br.com.objectos.way.sql.compiler.LoaderMethodInfo 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.sql.compiler;

import java.util.Set;

import br.com.objectos.way.code.ImportInfo;
import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.sql.Generated;

import com.google.common.collect.ImmutableSet;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

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

  private static final Set sqlTypeSet = ImmutableSet. builder()
      .add(Integer.class.getName())
      .add(Long.class.getName())
      .add(String.class.getName())

      .add(LocalDate.class.getName())
      .add(LocalDateTime.class.getName())
      .build();

  final MethodInfo methodInfo;
  final int index;
  final SimpleTypeInfo returnTypeBoxed;

  LoaderMethodInfo(MethodInfo methodInfo, int index, SimpleTypeInfo returnTypeBoxed) {
    this.methodInfo = methodInfo;
    this.index = index;
    this.returnTypeBoxed = returnTypeBoxed;
  }

  public static LoaderMethodInfo wrap(MethodInfo methodInfo, int index) {
    SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
    SimpleTypeInfo returnTypeBoxed = returnTypeInfo.autobox();

    String qualifiedName = returnTypeBoxed.toString();
    if (sqlTypeSet.contains(qualifiedName)) {
      return new LoaderMethodInfoSqlType(methodInfo, index, returnTypeBoxed);
    }

    if (qualifiedName.equals(Generated.class.getName())) {
      return new LoaderMethodInfoGeneratedType(methodInfo, index, returnTypeBoxed);
    }

    return new LoaderMethodInfoLoaderType(methodInfo, index, returnTypeBoxed);
  }

  public void toImportInfo(ImportInfoSet importInfoSet) {
    Set set = methodInfo.toImportInfoSet();
    importInfoSet.addAll(set);
  }

  @Override
  public MustacheObject toMustache() {
    return methodInfo.toMustacheHelper()
        .add("index", index)
        .add("getter", getter())
        .add("loaderAssignment", loaderAssignment())
        .add("loaderDeclaration", loaderDeclaration())
        .add("loaderInstance", loaderInstance())
        .add("loaderInstanceDeclaration", loaderInstanceDeclaration())
        .add("loaderVarName", loaderVarName())
        .toMustache();
  }

  @Override
  public String toString() {
    return methodInfo.toString();
  }

  abstract String getter();

  String loaderAssignment() {
    String varName = returnTypeBoxed.getVarName();
    return String.format("this.%sLoader = %sLoader;", varName, varName);
  }

  String loaderDeclaration() {
    return String.format("ResultFunction<%s> %sLoader", returnTypeBoxed.getRawName(), returnTypeBoxed.getVarName());
  }

  String loaderInstance() {
    return returnTypeBoxed.getVarName();
  }

  String loaderInstanceDeclaration() {
    return returnTypeBoxed.getRawName() + " " + returnTypeBoxed.getVarName();
  }

  String loaderVarName() {
    return returnTypeBoxed.getVarName() + "Loader";
  }

  boolean sqlType() {
    return true;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy