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

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

There is a newer version: 0.5.2
Show 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.flat;

import java.util.List;
import java.util.Optional;

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

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

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

  final TypeName superClassTypeName;

  final ClassName builderClassName;
  final ClassName classNameParser;

  final TypeName typeNameMatcherInterface;
  final TypeName typeNameParserInterface;

  final List fieldMethodList;

  private RecordInfo(TypeName superClassTypeName,
                 ClassName builderClassName,
                 ClassName classNameParser,
                 TypeName typeNameMatcherInterface,
                 TypeName typeNameParserInterface,
                 List fieldMethodList) {
    this.superClassTypeName = superClassTypeName;
    this.builderClassName = builderClassName;
    this.classNameParser = classNameParser;
    this.typeNameMatcherInterface = typeNameMatcherInterface;
    this.typeNameParserInterface = typeNameParserInterface;
    this.fieldMethodList = fieldMethodList;
  }

  public static RecordInfo code(PojoInfo pojoInfo) {
    Naming naming = pojoInfo.naming();
    ClassName className = naming.superClass();
    ClassName classNameParser = className.peerClass(className.simpleName() + "Parser");
    TypeName typeNameMatcherInterface = ClassName.get(br.com.objectos.flat.RecordMatcher.class);
    TypeName typeNameParserInterface = ParameterizedTypeName.get(ClassName.get(RecordParser.class), className);

    List fieldMethodList;
    fieldMethodList = FieldMethod.fieldMethodListOf(pojoInfo);

    return new RecordInfo(
        pojoInfo.naming().superClassTypeName(),
        pojoInfo.naming().builderClassName(),
        classNameParser,
        typeNameMatcherInterface,
        typeNameParserInterface,
        fieldMethodList);
  }

  Optional matcher() {
    RecordMatcherInfo matcher = null;

    for (FieldMethod fieldMethod : fieldMethodList) {
      if (fieldMethod.prefixMethod()) {
        matcher = new RecordMatcherInfo(this, fieldMethod);
        break;
      }
    }

    return Optional.ofNullable(matcher);
  }

  RecordMatcherInfo matcherOrVoid() {
    return matcher().orElse(RecordMatcherVoid.get());
  }

  RecordParserConstructor parserConstructor() {
    return new RecordParserConstructor(this);
  }

  RecordParserField parserField() {
    return new RecordParserField(this);
  }

  RecordParserParse parserParse() {
    return new RecordParserParse(this);
  }

  RecordParserType parserType() {
    return new RecordParserType(this);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy