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

br.com.objectos.flat.FlatFileParserArtifact 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.flat;

import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Optional;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.Artifact;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
class FlatFileParserArtifact extends AbstractHasFlatFile {

  private FlatFileParserArtifact(FlatFilePojo pojo) {
    super(pojo);
  }

  public static FlatFileParserArtifact of(FlatFilePojo pojo) {
    return new FlatFileParserArtifact(pojo);
  }

  public Artifact execute() {
    return toArtifact(get());
  }

  public TypeSpec get() {
    TypeSpec.Builder type = TypeSpec.classBuilder(parserClassName().simpleName())
        .addAnnotation(annotationSpec(FlatFileParserArtifactAction.class))
        .addModifiers(Modifier.FINAL)
        .addSuperinterface(parserInterfaceName());

    type.addField(staticField());
    type.addMethod(constructor());
    type.addMethod(staticMethod());
    type.addMethod(parseReader());
    type.addMethod(parseString());
    type.addMethod(___parse___());

    return type.build();
  }

  private MethodSpec constructor() {
    return MethodSpec.constructorBuilder()
        .addModifiers(Modifier.PRIVATE)
        .build();
  }

  private FieldSpec staticField() {
    return FieldSpec.builder(parserClassName(), "INSTANCE")
        .addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
        .initializer("new $T()", parserClassName())
        .build();
  }

  private MethodSpec staticMethod() {
    return MethodSpec.methodBuilder("get")
        .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
        .returns(parserInterfaceName())
        .addStatement("return INSTANCE")
        .build();
  }

  private MethodSpec parseReader() {
    return MethodSpec.methodBuilder("parse")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .addParameter(Reader.class, "reader")
        .returns(superClassTypeName())
        .addStatement("return ___parse___(new $T(reader))", LineNumberReader.class)
        .addException(parseExceptionClassName())
        .build();
  }

  private MethodSpec parseString() {
    return MethodSpec.methodBuilder("parse")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .addParameter(String.class, "input")
        .returns(superClassTypeName())
        .addStatement("$T reader = new $T(input)", StringReader.class, StringReader.class)
        .addStatement("return parse(reader)")
        .addException(parseExceptionClassName())
        .build();
  }

  private MethodSpec ___parse___() {
    return MethodSpec.methodBuilder("___parse___")
        .addModifiers(Modifier.PRIVATE)
        .addParameter(LineNumberReader.class, "reader")
        .returns(superClassTypeName())
        .addCode(body())
        .addException(parseExceptionClassName())
        .build();
  }

  private CodeBlock body() {
    CodeBlock.Builder body = CodeBlock.builder();
    body.addStatement("$T file = $T.get(reader)", FlatFileReader.class, FlatFileReader.class);
    body.add("\n");

    List recordMethodList = recordMethodList();
    for (RecordMethod recordMethod : recordMethodList) {
      recordMethod.parserParseStatementTo(body);
    }

    ClassName pecn = parseExceptionClassName();
    body.add("\n");
    body.add("$T<$T> maybeParseException = $T.of(", Optional.class, pecn, pecn);
    int index = 0;
    int size = recordMethodList.size();
    for (RecordMethod recordMethod : recordMethodList) {
      body.add("$L", recordMethod.propertyFieldName());
      if (++index < size) {
        body.add(", ");
      }
    }
    body.addStatement(")");

    body.beginControlFlow("if (maybeParseException.isPresent())");
    body.addStatement("throw maybeParseException.get()");
    body.endControlFlow();

    body.add("\n");
    body.add("return new $T()\n", classNameBuilderPojo());

    for (RecordMethod recordMethod : recordMethodList) {
      recordMethod.parserParseBuilder(body);
    }

    return body
        .addStatement("    .build()")
        .build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy