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

br.com.objectos.code.pojo.Configuration 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.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

import javax.annotation.Generated;

import br.com.objectos.core.util.MoreCollectors;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class Configuration {

  private final PojoInfo pojoInfo;
  private final List customFieldList = new ArrayList<>();

  public Configuration(PojoInfo pojoInfo) {
    this.pojoInfo = pojoInfo;
  }

  public synchronized Stream generate() {
    resetAndConfigure();

    AnnotationSpec annotationSpec = annotationSpec();
    return Stream.of(
        pojoInfo.toPojoClass().generate(this),
        pojoInfo.toBuilderInterface().generate(annotationSpec),
        pojoInfo.toBuilderClass().generate(this));
  }

  protected final void addCustomField(Type type, String name) {
    Objects.requireNonNull(type);
    Objects.requireNonNull(name);

    TypeName typeName = TypeName.get(type);
    customFieldList.add(new CustomField(typeName, name));
  }

  protected final void addCustomField(TypeName typeName, String name) {
    Objects.requireNonNull(typeName);
    Objects.requireNonNull(name);
    customFieldList.add(new CustomField(typeName, name));
  }

  protected AnnotationSpec annotationSpec() {
    return AnnotationSpec.builder(Generated.class)
        .addMember("value", "$S", processorClassName())
        .build();
  }

  protected void configure() {
  }

  protected List customFieldList() {
    return pojoClassFieldStream().collect(MoreCollectors.toImmutableList());
  }

  protected void onConstructor(TypeSpec.Builder type, List constructorList) {
    type.addMethods(constructorList);
  }

  protected void onField(TypeSpec.Builder type, List fieldList) {
    type.addFields(fieldList);
  }

  protected void onInvalidate(TypeSpec.Builder type, InvalidateMethod invalidate) {
    List lazyMethodList = pojoInfo.lazyMethodList();
    invalidate.addAll(lazyMethodList);

    Optional maybeMethod = invalidate.method();
    if (maybeMethod.isPresent()) {
      type.addMethod(maybeMethod.get());
    }
  }

  protected void onLazyField(TypeSpec.Builder type, List fieldList) {
    type.addFields(fieldList);
  }

  protected void onLazyInnerClass(TypeSpec.Builder type, List innerClassList) {
    type.addTypes(innerClassList);
  }

  protected void onLazyMethod(TypeSpec.Builder type, List methodList) {
    type.addMethods(methodList);
  }

  protected void onMethod(TypeSpec.Builder type, List methodList) {
    type.addMethods(methodList);
  }

  protected void onTestable(TypeSpec.Builder type, IsTestable testable) {
    List attributeMethodList = pojoInfo.attributeMethodList();
    Optional maybeTestable = testable.get(attributeMethodList);
    if (maybeTestable.isPresent()) {
      type.addMethod(maybeTestable.get());
    }
  }

  protected void onType(TypeSpec.Builder type) {
  }

  protected PojoInfo pojoInfo() {
    return pojoInfo;
  }

  protected abstract String processorClassName();

  Stream builderClassField() {
    return customFieldList.stream().map(CustomField::builderClassField);
  }

  List parameterList() {
    return customFieldList.stream()
        .map(CustomField::pojoClassConstructorParam)
        .collect(MoreCollectors.toImmutableList());
  }

  Stream pojoClassConstructorBodyStream() {
    return customFieldList.stream().map(CustomField::pojoClassConstructorBody);
  }

  List pojoClassFieldList() {
    return pojoClassFieldStream().collect(MoreCollectors.toImmutableList());
  }

  Stream pojoClassFieldStream() {
    return customFieldList.stream().map(CustomField::pojoClassField);
  }

  void resetAndConfigure() {
    customFieldList.clear();
    pojoInfo.invalidate().clear();
    configure();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy