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

br.com.objectos.way.flat.RepoInfoHelper Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/*
 * 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.way.flat;

import java.util.List;

import javax.lang.model.element.Modifier;

import br.com.objectos.io.File;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.flat.FlatFile;
import br.com.objectos.way.flat.FlatMapper;
import br.com.objectos.way.pojo.plugin.PojoInfo;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;

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

  private final TypeInfo pojoTypeInfo;

  private final List fieldList;
  private final List parameterSpecList;
  private final CodeBlock parameterAssignment;
  private final String parameterNames;

  public RepoInfoHelper(TypeInfo pojoTypeInfo,
                        List fieldList,
                        List parameterSpecList,
                        CodeBlock parameterAssignment,
                        String parameterNames) {
    this.pojoTypeInfo = pojoTypeInfo;

    this.fieldList = fieldList;
    this.parameterSpecList = parameterSpecList;
    this.parameterAssignment = parameterAssignment;
    this.parameterNames = parameterNames;
  }

  public MethodSpec constructor() {
    return MethodSpec.constructorBuilder()
        .addModifiers(Modifier.PROTECTED)
        .addParameters(parameterSpecList)
        .addParameter(FlatFile.class, "file")
        .addStatement("super(file)")
        .addCode(parameterAssignment)
        .build();
  }

  public List fieldList() {
    return fieldList;
  }

  public MethodSpec mapperMethodSpec() {
    return MethodSpec.methodBuilder("mapper")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PROTECTED)
        .returns(ParameterizedTypeName.get(ClassName.get(FlatMapper.class), pojoTypeInfo.className()))
        .addStatement("return reader -> new $T($Lreader)", pojoTypeInfo.classNameSuffix("Pojo"), parameterNames)
        .build();
  }

  public MethodSpec ofFileMethodSpec(TypeInfo typeInfo) {
    return MethodSpec.methodBuilder("of")
        .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
        .addParameters(parameterSpecList)
        .addParameter(File.class, "file")
        .returns(typeInfo.className())
        .addStatement("$1T flatFile = $1T.of(file)", FlatFile.class)
        .addStatement("return new $T($LflatFile)", typeInfo.classNameSuffix("Repo"), parameterNames)
        .build();
  }

  public MethodSpec recordSizeMethodSpec() {
    return MethodSpec.methodBuilder("recordSize")
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PROTECTED)
        .returns(TypeName.INT)
        .addStatement("return $L", recordSize())
        .build();
  }

  private int recordSize() {
    return PojoInfo.of(pojoTypeInfo)
        .propertyStream()
        .map(FieldMethod::code)
        .mapToInt(FieldMethod::length)
        .sum();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy