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

br.com.objectos.io.compiler.FlatFile 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.stream.Collectors;
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.FlatFileParser;
import br.com.objectos.io.flat.WritableFlatFile;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
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 FlatFile extends Pojo {

  final ClassName parseExceptionClassName;

  final ClassName parserClassName;
  final TypeName parserInterfaceName;

  final List recordMethodList;
  final List exceptionFieldList;

  final boolean writable;

  private FlatFile(PojoInfo pojoInfo,
                   ClassName parseExceptionClassName,
                   ClassName parserClassName,
                   TypeName parserInterfaceName,
                   List recordMethodList,
                   boolean writable) {
    super(pojoInfo);
    this.parseExceptionClassName = parseExceptionClassName;
    this.parserClassName = parserClassName;
    this.parserInterfaceName = parserInterfaceName;
    this.recordMethodList = recordMethodList;
    this.writable = writable;
    exceptionFieldList = ExceptionField.listOf(recordMethodList);
  }

  public static FlatFile code(TypeInfo typeInfo) {
    PojoInfo pojoInfo = PojoInfo.of(typeInfo);

    ClassName className = typeInfo.className();
    ClassName parseExceptionClassName = typeInfo.classNameSuffix("ParseException");
    ClassName parserClassName = typeInfo.classNameSuffix("Parser");

    return new FlatFile(
        pojoInfo,
        parseExceptionClassName,
        parserClassName,
        ParameterizedTypeName.get(ClassName.get(FlatFileParser.class), className, parseExceptionClassName),
        Pojo.methodInfoList(typeInfo).stream()
            .map(RecordMethod::code)
            .collect(Collectors.toList()),
        typeInfo.getInterface(WritableFlatFile.class).isPresent());
  }

  @Override
  public Stream generate() {
    Stream pojoStream = super.generate();
    JavaFile parser = parserType().generate();
    JavaFile exception = exceptionType().generate();
    Stream more = Stream.of(parser, exception);
    return Stream.concat(pojoStream, more);
  }

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

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

    super.onMethod(type, methodList);

    pojoWriteTo().addTo(type);
  }

  protected ClassName parserClassName() {
    return parserClassName;
  }

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

  FlatFileExceptionConstructor exceptionConstructor() {
    return new FlatFileExceptionConstructor(this);
  }

  FlatFileExceptionField exceptionField() {
    return new FlatFileExceptionField(this);
  }

  FlatFileExceptionMethod exceptionMethod() {
    return new FlatFileExceptionMethod(this);
  }

  FlatFileExceptionStaticFactory exceptionStaticFactory() {
    return new FlatFileExceptionStaticFactory(this);
  }

  FlatFileExceptionType exceptionType() {
    return new FlatFileExceptionType(this);
  }

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

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

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

  FlatFilePojoWritable pojoWritable() {
    return new FlatFilePojoWritable(this);
  }

  FlatFilePojoWriteTo pojoWriteTo() {
    return new FlatFilePojoWriteTo(this);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy