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

br.com.objectos.way.sql.compiler.SqlParameter 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.Map;
import java.util.Set;

import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.sql.Generated;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;

/**
 * @author [email protected] (Marcio Endo)
 */
enum SqlParameter {

  GENERATED(Generated.class.getName()) {
    @Override
    String toString(SimpleTypeInfo returnTypeInfo) {
      SimpleTypeInfo actualType = returnTypeInfo
          .getTypeParameterInfoIterable()
          .first()
          .get()
          .toSimpleTypeInfo();
      return SqlParameter.mustacheString(actualType);
    }
  },

  INTEGER("int", "java.lang.Integer") {
    @Override
    String toString(SimpleTypeInfo returnTypeInfo) {
      return "Parameter.integer()";
    }
  },

  LOCAL_DATE("org.joda.time.LocalDate") {
    @Override
    String toString(SimpleTypeInfo returnTypeInfo) {
      return "Parameter.localDate()";
    }
  },

  LONG("long", "java.lang.Long") {
    @Override
    String toString(SimpleTypeInfo returnTypeInfo) {
      return "Parameter.longParameter()";
    }
  },

  STRING("java.lang.String") {
    @Override
    String toString(SimpleTypeInfo returnTypeInfo) {
      return "Parameter.string()";
    }
  };

  private static final Map typeMap;

  static {
    typeMap = Maps.newHashMap();
    for (SqlParameter parameter : SqlParameter.values()) {
      for (String name : parameter.nameSet) {
        typeMap.put(name, parameter);
      }
    }
  }

  private final Set nameSet;

  private SqlParameter(String... names) {
    nameSet = ImmutableSet.copyOf(names);
  }

  public static String mustacheString(SimpleTypeInfo returnTypeInfo) {
    SqlParameter parameter = SqlParameter.of(returnTypeInfo);
    return parameter != null ? parameter.toString(returnTypeInfo) : null;
  }

  private static SqlParameter of(SimpleTypeInfo typeInfo) {
    String name = typeInfo.toString();
    return typeMap.get(name);
  }

  abstract String toString(SimpleTypeInfo returnTypeInfo);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy