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

br.com.objectos.way.pojo.plugin.AbstractCollectionPlugin 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.Collections;

import br.com.objectos.way.code.TypeParameterInfo;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ArrayTypeName;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.WildcardTypeName;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class AbstractCollectionPlugin extends AbstractPlugin implements BuilderPropertyAction {

  static final AnnotationSpec SUPPRESS_UNCHECKED = AnnotationSpec.builder(SuppressWarnings.class)
      .addMember("value", "$S", "unchecked")
      .build();

  @Override
  protected final void configure() {
    when(property(instanceOf(collectionInterface()))).execute(this);
  }

  @Override
  public final BuilderProperty execute(Property property) {
    return BuilderProperty.of(
        property.standardBuilderProperty(),
        varargs(property));
  }

  abstract Class collectionImplementation();

  abstract Class collectionInterface();

  abstract String unmodifiableMethodName();

  boolean safeVarargs(TypeName elementTypeName) {
    return elementTypeName instanceof ParameterizedTypeName || elementTypeName instanceof WildcardTypeName;
  }

  private BuilderProperty varargs(Property property) {
    return varargs0(
        property,
        property.typeParameterInfoStream()
            .map(TypeParameterInfo::typeNameBound)
            .findFirst()
            .get(),
        "elements");
  };

  private BuilderProperty varargs0(Property property, TypeName elementTypeName, String parameterName) {
    return BuilderProperty.methodBuilder(property)
        .addAnnotationIf(SUPPRESS_UNCHECKED, safeVarargs(elementTypeName))
        .name()
        .addParameter(ArrayTypeName.of(elementTypeName), parameterName)
        .varargs()
        .nullCheck(parameterName)
        .statement(
            "$T $L = new $T<>($L.length)",
            ParameterizedTypeName.get(ClassName.get(collectionInterface()), elementTypeName),
            property.name(),
            collectionImplementation(),
            parameterName)
        .beginControlFlow("for (int i = 0; i < elements.length; i++)")
        .statement("$T e = $L[i]", elementTypeName, parameterName)
        .nullCheck("e")
        .statement("$L.add(e)", property.name())
        .endControlFlow()
        .assignment("$T.$L($L)", Collections.class, unmodifiableMethodName(), property.name())
        .build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy