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

br.com.objectos.way.pojo.plugin.StandardBuilderConfiguration Maven / Gradle / Ivy

There is a newer version: 0.6.0
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.way.pojo.plugin;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import br.com.objectos.way.code.AccessInfo;
import br.com.objectos.way.code.Artifact;
import br.com.objectos.way.code.ConstructorInfo;
import br.com.objectos.way.code.TypeInfo;

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

/**
 * @author [email protected] (Marcio Endo)
 */
class StandardBuilderConfiguration implements BuilderConfiguration {

  private final TypeInfo typeInfo;
  private final Naming naming;

  private final GeneratedBy generatedBy = GeneratedBy.empty();
  private final List builderPropertyPluginList = new ArrayList<>();
  private final List customFieldList = new ArrayList<>();
  private Predicate propertyFilter = (t) -> true;

  private StandardBuilderConfiguration(TypeInfo typeInfo,
                                       Naming naming) {
    this.typeInfo = typeInfo;
    this.naming = naming;
  }

  public static StandardBuilderConfiguration of(TypeInfo typeInfo) {
    Naming naming = Naming.of(typeInfo);
    return new StandardBuilderConfiguration(typeInfo, naming);
  }

  @Override
  public void accept(Artifact.Builder artifactList) {
    artifactList.addArtifact(builderInterface().execute());
    artifactList.addArtifact(builderClass().execute());
  }

  @Override
  public void addCustomField(BuilderCustomField customField) {
    customFieldList.add(customField);
  }

  @Override
  public void addPlugin(BuilderPropertyPlugin action) {
    builderPropertyPluginList.add(action);
  }

  @Override
  public void addPropertyCondition(PropertyPredicate condition) {
    propertyFilter = propertyFilter.and(condition.negate());
  }

  @Override
  public void onExecute() {
    builderPropertyPluginList.add(BuilderPropertyPlugin.STANDARD);
  }

  @Override
  public void generatedBy(Class generator) {
    generatedBy.add(generator);
  }

  Artifact artifact(TypeSpec.Builder type) {
    generatedBy.accept(type);
    return naming.toArtifact(type);
  }

  List builderPropertyPluginList() {
    return builderPropertyPluginList;
  }

  List constructorList() {
    List constructorInfoList = typeInfo.constructorInfoStream()
        .filter(info -> !info.hasAccessInfo(AccessInfo.PRIVATE))
        .collect(Collectors.toList());
    int size = constructorInfoList.size();
    return constructorInfoList.stream()
        .map(constructor -> new BuilderClassConstructor(constructor, size, customFieldList))
        .collect(Collectors.toList());
  }

  List customFieldSpecList() {
    return customFieldList.stream()
        .map(BuilderCustomField::builderField)
        .collect(Collectors.toList());
  }

  Naming naming() {
    return naming;
  }

  List propertyList() {
    return Property.of(Mode.STANDARD, typeInfo)
        .stream()
        .filter(propertyFilter)
        .collect(Collectors.toList());
  }

  TypeInfo typeInfo() {
    return typeInfo;
  }

  private BuilderClass builderClass() {
    return BuilderClass.of(this);
  }

  private BuilderInterface builderInterface() {
    return BuilderInterface.of(this);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy