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

br.com.objectos.io.compiler.Record 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.io.compiler;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import br.com.objectos.code.TypeInfo;
import br.com.objectos.code.pojo.Pojo;
import br.com.objectos.code.pojo.PojoInfo;
import br.com.objectos.io.flat.IsRecord;
import br.com.objectos.io.flat.RecordParser;
import br.com.objectos.io.flat.annotation.FieldAnnotation;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
class Record extends Pojo {

  final ClassName classNameParser;

  final TypeName typeNameMatcherInterface;
  final TypeName typeNameParserInterface;

  final List fieldMethodList;

  private Record(PojoInfo pojoInfo,
                 ClassName classNameParser,
                 TypeName typeNameMatcherInterface,
                 TypeName typeNameParserInterface,
                 List fieldMethodList) {
    super(pojoInfo);
    this.classNameParser = classNameParser;
    this.typeNameMatcherInterface = typeNameMatcherInterface;
    this.typeNameParserInterface = typeNameParserInterface;
    this.fieldMethodList = fieldMethodList;
  }

  public static Record code(TypeInfo typeInfo) {
    ClassName className = typeInfo.className();
    ClassName classNameParser = typeInfo.classNameSuffix("Parser");
    TypeName typeNameMatcherInterface = ClassName.get(br.com.objectos.io.flat.RecordMatcher.class);
    TypeName typeNameParserInterface = ParameterizedTypeName.get(ClassName.get(RecordParser.class), className);

    List fieldMethodList;
    fieldMethodList = FieldMethod.fieldMethodListOf(typeInfo);

    return new Record(
        PojoInfo.of(typeInfo, m -> true, m -> m.annotationInfoAnnotatedWith(FieldAnnotation.class)
            .get()
            .annotationInfo(FieldAnnotation.class)
            .get()
            .annotationValueInfo("builder")
            .get()
            .booleanValue()),
        classNameParser,
        typeNameMatcherInterface,
        typeNameParserInterface,
        fieldMethodList);
  }

  @Override
  public Stream generate() {
    Stream pojoStream = super.generate();
    Stream parserStream = parserType().generate();
    return Stream.concat(pojoStream, parserStream);
  }

  @Override
  protected AnnotationSpec annotationSpec() {
    return super.annotationSpec();
  }

  protected ClassName classNameParser() {
    return classNameParser;
  }

  @Override
  protected void onField(TypeSpec.Builder type, List fieldList) {
    pojoField().addTo(type);
  }

  @Override
  protected void onMethod(TypeSpec.Builder type, List methodList) {
    pojoToString().addTo(type);
    pojoEmit().addTo(type);

    super.onMethod(type, methodList);
  }

  @Override
  protected void onType(TypeSpec.Builder type) {
    super.onType(type);

    type.addSuperinterface(IsRecord.class);
  }

  @Override
  protected String processorClassName() {
    return RecordPojoProcessor.class.getName();
  }

  Optional matcher() {
    RecordMatcher matcher = null;

    for (FieldMethod fieldMethod : fieldMethodList) {
      if (fieldMethod.prefixMethod()) {
        matcher = new RecordMatcher(this, fieldMethod);
        break;
      }
    }

    return Optional.ofNullable(matcher);
  }

  RecordMatcher matcherOrVoid() {
    return matcher().orElse(RecordMatcherVoid.get());
  }

  RecordParserConstructor parserConstructor() {
    return new RecordParserConstructor(this);
  }

  RecordParserField parserField() {
    return new RecordParserField(this);
  }

  RecordParserParse parserParse() {
    return new RecordParserParse(this);
  }

  RecordParserType parserType() {
    return new RecordParserType(this);
  }

  RecordPojoField pojoField() {
    return new RecordPojoField(this);
  }

  RecordPojoEmit pojoEmit() {
    return new RecordPojoEmit(this);
  }

  RecordPojoToString pojoToString() {
    return new RecordPojoToString(this);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy