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

br.com.objectos.code.pojo.Naming 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.code.pojo;

import java.util.List;

import br.com.objectos.code.Code;
import br.com.objectos.code.TypeInfo;
import br.com.objectos.core.auto.AutoPojo;
import br.com.objectos.core.testing.Testable;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;

/**
 * @author [email protected] (Marcio Endo)
 */
@AutoPojo
public abstract class Naming implements Testable {

  abstract ClassName superClass();
  abstract ClassName pojo();
  abstract ClassName builderInterface();
  abstract ClassName builderClass();
  abstract List typeVariableNameList();
  abstract List typeVariableNameRawList();
  abstract List typeVariableNameUnboundedList();

  Naming() {
  }

  static NamingBuilder builder() {
    return new NamingBuilderPojo();
  }

  static Naming of(TypeInfo typeInfo) {
    return Naming.builder()
        .superClass(typeInfo.className())
        .pojo(typeInfo.classNameSuffix("Pojo"))
        .builderInterface(typeInfo.classNameSuffix("Builder"))
        .builderClass(typeInfo.classNameSuffix("BuilderPojo"))
        .typeVariableNameList(typeInfo.typeVariableNameList())
        .typeVariableNameRawList(typeInfo.typeVariableNameRawList())
        .typeVariableNameUnboundedList(typeInfo.typeVariableNameUnboundedList())
        .build();
  }

  public ClassName builderClassName() {
    return builderClass();
  }

  public String builderClassSimpleName() {
    return builderClass().simpleName();
  }

  public TypeName builderClassTypeName() {
    return typeName(builderClass(), typeVariableNameList());
  }

  public String builderInnerSimpleName(AttributeMethod method) {
    String suffix = Code.upperCaseFirstChar(method.fieldName());
    return builderInterface().simpleName() + suffix;
  }

  public TypeName builderInnerTypeName(AttributeMethod method) {
    String innerSimpleName = builderInnerSimpleName(method);
    ClassName innerClassName = builderInterface().nestedClass(innerSimpleName);
    return typeNameUnbounded(innerClassName);
  }

  public String builderInterfaceSimpleName() {
    return builderInterface().simpleName();
  }

  public TypeName builderInterfaceTypeNameUnbounded() {
    return typeNameUnbounded(builderInterface());
  }

  public ClassName lazyClassName(String fieldName) {
    return pojo().nestedClass("Lazy" + Code.upperCaseFirstChar(fieldName));
  }

  public String packageName() {
    return superClass().packageName();
  }

  public String pojoSimpleName() {
    return pojo().simpleName();
  }

  public TypeName pojoUnboundedTypeName() {
    return typeNameUnbounded(pojo());
  }

  public TypeName superClassTypeName() {
    return typeName(superClass(), typeVariableNameList());
  }

  public TypeName superClassTypeNameRaw() {
    return typeNameRaw(superClass());
  }

  public TypeName superClassTypeNameUnbounded() {
    return typeNameUnbounded(superClass());
  }

  public void typeVariableNameListTo(TypeSpec.Builder type) {
    for (TypeVariableName typeVariableName : typeVariableNameList()) {
      type.addTypeVariable(typeVariableName);
    }
  }

  public TypeName typeVariableNameRawListTo(ClassName className) {
    return typeNameRaw(className);
  }

  public void typeVariableNameUnboundedListTo(TypeSpec.Builder type) {
    for (TypeVariableName typeVariableName : typeVariableNameUnboundedList()) {
      type.addTypeVariable(typeVariableName);
    }
  }

  private TypeName typeName(ClassName className, List typeVariableNameList) {
    TypeName typeName = className;

    if (!typeVariableNameList.isEmpty()) {
      TypeVariableName[] typeArguments = typeVariableNameList.toArray(new TypeVariableName[] {});
      typeName = ParameterizedTypeName.get(className, typeArguments);
    }

    return typeName;
  }

  private TypeName typeNameRaw(ClassName className) {
    return typeName(className, typeVariableNameRawList());
  }

  private TypeName typeNameUnbounded(ClassName className) {
    return typeName(className, typeVariableNameUnboundedList());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy