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

br.com.objectos.flat.FlatFilePojo 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 java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import javax.annotation.Generated;

import br.com.objectos.code.Artifact;
import br.com.objectos.pojo.plugin.Naming;
import br.com.objectos.pojo.plugin.PojoInfo;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

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

  private static final Map CACHE = new ConcurrentHashMap<>();

  public final Naming naming;
  public final ClassName parseExceptionClassName;
  public final ClassName parserClassName;
  public final TypeName parserInterfaceName;
  public final List recordMethodList;
  public final List exceptionFieldList;

  private FlatFilePojo(Naming naming,
                       ClassName parseExceptionClassName,
                       ClassName parserClassName,
                       TypeName parserInterfaceName,
                       List recordMethodList,
                       List exceptionFieldList) {
    this.naming = naming;
    this.parseExceptionClassName = parseExceptionClassName;
    this.parserClassName = parserClassName;
    this.parserInterfaceName = parserInterfaceName;
    this.recordMethodList = recordMethodList;
    this.exceptionFieldList = exceptionFieldList;
  }

  public static void invalidate() {
    CACHE.clear();
  }

  public static FlatFilePojo of(PojoInfo pojoInfo) {
    return CACHE.computeIfAbsent(pojoInfo, FlatFilePojo::of0);
  }

  private static FlatFilePojo of0(PojoInfo pojoInfo) {
    Naming naming = pojoInfo.naming();
    ClassName className = naming.superClass();
    ClassName parseExceptionClassName = naming.superClassSuffix("ParseException");

    List recordMethodList = pojoInfo.propertyStream()
        .map(RecordMethod::of)
        .collect(Collectors.toList());

    return new FlatFilePojo(
        naming,
        parseExceptionClassName,
        naming.superClassSuffix("Parser"),
        ParameterizedTypeName.get(ClassName.get(FlatFileParser.class), className, parseExceptionClassName),
        recordMethodList,
        ExceptionField.listOf(recordMethodList));
  }

  public AnnotationSpec annotationSpec(Class type) {
    return AnnotationSpec.builder(Generated.class)
        .addMember("value", "$S", type.getName())
        .build();
  }

  public Artifact toArtifact(TypeSpec type) {
    return naming.toArtifact(type);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy