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

br.com.objectos.way.auto.functional.ToFunctionCanvas 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.Function;
import com.google.common.collect.ImmutableList;

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

  private static ToFunctionCanvas INSTANCE = new ToFunctionCanvas();

  private ToFunctionCanvas() {
  }

  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();
    }

    if (methodInfo.hasReturnTypeInfo(SimpleTypePrimitives.VOID)) {
      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/ToFunction.mustache")
        .namedAfter(typeInfo, suffix)
        .toCodeCanvasArtifact(Writers.INSTANCE, mustache(typeInfo, methodInfo));
  }

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

    SimpleTypeInfo fromClass = typeInfo.toSimpleTypeInfo();
    SimpleTypeInfo toClass = method.returnTypeInfo();

    importInfoSet.addAll(toClass.toImportInfo());
    importInfoSet.addAll(method.toImportInfoSet());

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy