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

br.com.objectos.way.auto.functional.IsPredicateCanvas Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright 2014 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.auto.functional;

import java.util.List;

import br.com.objectos.way.code.AccessInfo;
import br.com.objectos.way.code.CodeCanvasArtifact;
import br.com.objectos.way.code.CodeCanvasWriter;
import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.code.SimpleTypePrimitives;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.code.mustache.Writers;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.core.tmpl.mustache.Mustaches;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;

/**
 * @author [email protected] (Patricia Nascimento)
 */
public class IsPredicateCanvas {

  public static final IsPredicateCanvas INSTANCE = new IsPredicateCanvas();

  private IsPredicateCanvas() {
  }

  public static List toArtifactList(TypeInfo typeInfo, MethodInfo methodInfo) {
    return INSTANCE.code(typeInfo, methodInfo);
  }

  private List code(TypeInfo typeInfo, MethodInfo methodInfo) {
    if (methodInfo.hasAccessInfo(AccessInfo.PRIVATE)) {
      return ImmutableList.of();
    }

    if (!methodInfo.hasReturnTypeInfo(SimpleTypePrimitives.BOOLEAN)) {
      return ImmutableList.of();
    }

    CodeCanvasArtifact artifact = codeMethod(typeInfo, methodInfo);
    return ImmutableList.of(artifact);
  }

  private CodeCanvasArtifact codeMethod(TypeInfo typeInfo, MethodInfo methodInfo) {
    String suffix = methodInfo.getClassName();
    return CodeCanvasWriter.forTemplate("/way-auto-functional/IsPredicate.mustache")
        .namedAfter(typeInfo, suffix)
        .toCodeCanvasArtifact(Writers.INSTANCE, mustache(typeInfo, methodInfo));
  }

  private MustacheObject mustache(TypeInfo typeInfo, MethodInfo methodInfo) {
    ImportInfoSet importInfoSet = ImportInfoSet.setOf();
    importInfoSet.add(Predicate.class);

    SimpleTypeInfo fromClass = typeInfo.toSimpleTypeInfo();

    importInfoSet.addAll(methodInfo.toImportInfoSet());

    return Mustaches.toMustacheHelper()
        .add("fromClass", fromClass)
        .add("method", methodInfo)
        .add("imports", importInfoSet.toImportInfoList(typeInfo))
        .toMustache();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy