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

br.com.objectos.way.pojo.plugin.Naming Maven / Gradle / Ivy

There is a newer version: 0.6.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.way.pojo.plugin;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import br.com.objectos.testable.Equality;
import br.com.objectos.testable.Testable;
import br.com.objectos.testable.Tester;
import br.com.objectos.way.code.Artifact;
import br.com.objectos.way.code.Code;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.TypeInfo;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
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)
 */
public class Naming implements Testable {

  private static final Map CACHE = new ConcurrentHashMap<>();

  private static final Tester TESTER = Tester.of(Naming.class)
      .add("superClass", o -> o.superClass)
      .add("pojo", o -> o.pojo)
      .add("builderInterface", o -> o.builderInterface)
      .add("builderClass", o -> o.builderClass)
      .add("typeVariableNameList", o -> o.typeVariableNameList)
      .add("typeVariableNameRawList", o -> o.typeVariableNameRawList)
      .add("typeVariableNameUnboundedList", o -> o.typeVariableNameUnboundedList)
      .build();

  private final ClassName superClass;
  private final ClassName pojo;
  private final ClassName builderInterface;
  private final ClassName builderClass;

  private final List typeVariableNameList;
  private final List typeVariableNameRawList;
  private final List typeVariableNameUnboundedList;

  private Naming(ClassName superClass,
                 ClassName pojo,
                 ClassName builderInterface,
                 ClassName builderClass,
                 List typeVariableNameList,
                 List typeVariableNameRawList,
                 List typeVariableNameUnboundedList) {
    this.superClass = superClass;
    this.pojo = pojo;
    this.builderInterface = builderInterface;
    this.builderClass = builderClass;
    this.typeVariableNameList = typeVariableNameList;
    this.typeVariableNameRawList = typeVariableNameRawList;
    this.typeVariableNameUnboundedList = typeVariableNameUnboundedList;
  }
  public static void invalidate() {
    CACHE.clear();
  }

  public static Naming of(TypeInfo typeInfo) {
    return CACHE.computeIfAbsent(typeInfo, Naming::of0);
  }

  private static Naming of0(TypeInfo typeInfo) {
    return new Naming(
        typeInfo.className(),
        typeInfo.classNameSuffix("Pojo"),
        typeInfo.classNameSuffix("Builder"),
        typeInfo.classNameSuffix("BuilderPojo"),
        typeInfo.typeVariableNameList(),
        typeInfo.typeVariableNameRawList(),
        typeInfo.typeVariableNameUnboundedList());
  }

  public ClassName builderClass() {
    return builderClass;
  }

  public ClassName builderClassName() {
    return builderClass;
  }

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

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

  public ClassName builderInterface() {
    return builderInterface;
  }

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

  public TypeName builderInnerTypeName(MethodInfo 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);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof Naming)) {
      return false;
    }
    Naming that = (Naming) o;
    return superClass.equals(that.superClass);
  }

  @Override
  public int hashCode() {
    return superClass.hashCode();
  }

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

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

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

  public ClassName pojo() {
    return pojo;
  }

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

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

  public ClassName superClass() {
    return superClass;
  }

  public ClassName superClassSuffix(String suffix) {
    String simpleName = superClass.simpleName();
    return superClass.peerClass(simpleName + suffix);
  }

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

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

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

  public Artifact toArtifact(TypeSpec type) {
    JavaFile file = JavaFile.builder(packageName(), type)
        .skipJavaLangImports(true)
        .build();
    return Artifact.of(file);
  }

  public Artifact toArtifact(TypeSpec.Builder type) {
    return toArtifact(type.build());
  }

  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