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

br.com.objectos.flat.FlatRecordPlugin Maven / Gradle / Ivy

There is a newer version: 0.5.2
Show newest version
/*
 * Copyright 2014-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 javax.lang.model.element.Modifier;

import br.com.objectos.code.Artifact;
import br.com.objectos.metainf.Services;
import br.com.objectos.pojo.plugin.AbstractPlugin;
import br.com.objectos.pojo.plugin.ArtifactAction;
import br.com.objectos.pojo.plugin.BuilderProperty;
import br.com.objectos.pojo.plugin.BuilderPropertyAction;
import br.com.objectos.pojo.plugin.Contribution;
import br.com.objectos.pojo.plugin.Plugin;
import br.com.objectos.pojo.plugin.PojoAction;
import br.com.objectos.pojo.plugin.PojoInfo;
import br.com.objectos.pojo.plugin.PojoProperty;
import br.com.objectos.pojo.plugin.PojoPropertyAction;
import br.com.objectos.pojo.plugin.Property;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
@Services(Plugin.class)
public class FlatRecordPlugin extends AbstractPlugin implements PojoAction {

  @Override
  protected void configure() {
    executeWhen(pojo(instanceOf(FlatRecord.class)));

    execute(this);
    execute(ThisBuilderPropertyAction.INSTANCE);
    execute(Parser.INSTANCE);
  }

  @Override
  public Contribution execute(PojoInfo pojoInfo) {
    return Contribution.builder()
        .addPojoPropertyAction(ThisPojoPropertyAction.INSTANCE)
        .addMethod(emit(pojoInfo))
        .addMethod(toString0())
        .build();
  }

  private MethodSpec emit(PojoInfo pojoInfo) {
    return MethodSpec.methodBuilder("emit")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .addParameter(ParameterSpec.builder(FlatFileWriter.class, "file").build())
        .addCode(emitBody(pojoInfo))
        .build();
  }

  private CodeBlock emitBody(PojoInfo pojoInfo) {
    CodeBlock.Builder body = CodeBlock.builder()
        .add("file.recordWriter()\n");

    pojoInfo.propertyStream()
        .map(FieldMethod::code)
        .forEach(f -> f.recordWriterCode(body));

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

  private MethodSpec toString0() {
    return MethodSpec.methodBuilder("toString")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .returns(String.class)
        .addStatement("$T builder = new $T()", StringBuilder.class, StringBuilder.class)
        .addStatement("$T file = new $T(builder)", FlatFileWriter.class, FlatFileWriter.class)
        .addStatement("emit(file)")
        .addStatement("return builder.toString()")
        .build();
  }

  private static enum ThisBuilderPropertyAction implements BuilderPropertyAction {

    INSTANCE;

    @Override
    public BuilderProperty execute(Property property) {
      return FieldMethod.code(property).builderProperty();
    }

  }

  private static enum Parser implements ArtifactAction {

    INSTANCE;

    @Override
    public Artifact execute(PojoInfo pojoInfo) {
      RecordInfo record = RecordInfo.code(pojoInfo);
      RecordParserType type = new RecordParserType(record);
      return pojoInfo.naming().toArtifact(type.get());
    }

  }

  private static enum ThisPojoPropertyAction implements PojoPropertyAction {

    INSTANCE;

    @Override
    public PojoProperty execute(Property property) {
      return FieldMethod.code(property).pojoProperty();
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy