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

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

/*
 * Copyright 2016 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.util.ArrayList;
import java.util.List;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.code.TypeParameterInfo;
import br.com.objectos.pojo.plugin.Property;

import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.FieldSpec.Builder;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class FlatContainerProperty {

  private final Property property;
  private final FlatRecordInfo recordInfo;

  FlatContainerProperty(Property property, FlatRecordInfo recordInfo) {
    this.property = property;
    this.recordInfo = recordInfo;
  }

  public static FlatContainerProperty of(Property property) {
    SimpleTypeInfo returnTypeInfo = property.returnTypeInfo();
    return returnTypeInfo.isInfoOf(List.class)
        ? ListProperty.of(property, returnTypeInfo)
        : SimpleProperty.of(property, returnTypeInfo);
  }

  public void constructor(MethodSpec.Builder constructor) {
    constructor
        .addParameter(property.returnTypeInfo().typeName(), propertyName())
        .addStatement("this.$1L = $1L", propertyName());
  }

  public void flatReaderVisitor(TypeSpec.Builder visitor) {
    visitor.addField(flatReaderVisitorFieldSpec().build());
  }

  public abstract void visitLineMethod(MethodSpec.Builder method);

  public abstract void writeTo(MethodSpec.Builder method);

  FieldSpec.Builder flatReaderVisitorFieldSpec() {
    SimpleTypeInfo returnTypeInfo = property.returnTypeInfo();
    return FieldSpec.builder(returnTypeInfo.typeName(), property.name(), Modifier.PRIVATE);
  }

  String propertyName() {
    return property.name();
  }

  FlatRecordInfo recordInfo() {
    return recordInfo;
  }

  private static class ListProperty extends FlatContainerProperty {

    public ListProperty(Property property, FlatRecordInfo recordInfo) {
      super(property, recordInfo);
    }

    public static ListProperty of(Property property, SimpleTypeInfo returnTypeInfo) {
      FlatRecordInfo recordInfo = returnTypeInfo.getTypeParameterInfoStream()
          .findFirst()
          .map(TypeParameterInfo::simpleTypeInfo)
          .map(FlatRecordInfo::of)
          .get();
      return new ListProperty(property, recordInfo);
    }

    @Override
    public void visitLineMethod(MethodSpec.Builder method) {
      method.addStatement("$L.add(new $T(reader))", propertyName(), recordInfo().pojoTypeName());
      method.addStatement("break");
    }

    @Override
    public void writeTo(MethodSpec.Builder method) {
      method.beginControlFlow("for ($T e : $L)", recordInfo().typeName(), propertyName())
          .addStatement("e.writeTo(writer)")
          .endControlFlow();
    }

    @Override
    Builder flatReaderVisitorFieldSpec() {
      return super.flatReaderVisitorFieldSpec().initializer("new $T<>()", ArrayList.class);
    }

  }

  private static class SimpleProperty extends FlatContainerProperty {

    public SimpleProperty(Property property, FlatRecordInfo recordInfo) {
      super(property, recordInfo);
    }

    public static SimpleProperty of(Property property, SimpleTypeInfo returnTypeInfo) {
      FlatRecordInfo recordInfo = FlatRecordInfo.of(returnTypeInfo);
      return new SimpleProperty(property, recordInfo);
    }

    @Override
    public void visitLineMethod(MethodSpec.Builder method) {
      method.addStatement("$L = new $T(reader)", propertyName(), recordInfo().pojoTypeName());
      method.addStatement("break");
    }

    @Override
    public void writeTo(MethodSpec.Builder method) {
      method.addStatement("$L.writeTo(writer)", propertyName());
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy