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

br.com.objectos.io.compiler.FlatFileParserParse 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.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.io.flat.FlatFileReader;

import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

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

  public FlatFileParserParse(FlatFile file) {
    super(file);
  }

  public List get() {
    return ImmutableList. builder()
        .add(parseReader())
        .add(parseString())
        .add(___parse___())
        .build();
  }

  public void addTo(TypeSpec.Builder type) {
    type.addMethods(get());
  }

  private MethodSpec parseReader() {
    return MethodSpec.methodBuilder("parse")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .addParameter(Reader.class, "reader")
        .returns(naming().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(naming().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(naming().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.methodInfoFieldName());
      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