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

br.com.objectos.auto.ToFunctionStatic Maven / Gradle / Ivy

The 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.auto;

import java.util.List;

import javax.lang.model.element.Modifier;

import com.google.common.base.Optional;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class ToFunctionStatic extends AbstractHasToFunction {

  ToFunctionStatic(ToFunction function) {
    super(function);
  }

  public static ToFunctionStatic wrap(ToFunction function) {
    List parameterSpecList = function.parameterSpecList;
    return parameterSpecList.isEmpty()
        ? new ParameterLess(function)
        : new WithParameter(function);
  }

  public abstract Optional getField();

  public abstract MethodSpec getMethod();

  void addFieldTo(TypeSpec.Builder type) {
    Optional maybeField = getField();
    if (maybeField.isPresent()) {
      FieldSpec field = maybeField.get();
      type.addField(field);
    }
  }

  void addMethodTo(TypeSpec.Builder type) {
    MethodSpec method = getMethod();
    type.addMethod(method);
  }

  private static class ParameterLess extends ToFunctionStatic {

    private ParameterLess(ToFunction function) {
      super(function);
    }

    @Override
    public Optional getField() {
      FieldSpec field = field();
      return Optional.of(field);
    }

    @Override
    public MethodSpec getMethod() {
      return MethodSpec.methodBuilder("get")
          .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
          .returns(functionTypeName())
          .addStatement("return INSTANCE")
          .build();
    }

    private FieldSpec field() {
      return FieldSpec.builder(functionTypeName(), "INSTANCE")
          .addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
          .initializer("new $T()", className())
          .build();
    }

  }

  private static class WithParameter extends ToFunctionStatic {

    private WithParameter(ToFunction function) {
      super(function);
    }

    @Override
    public Optional getField() {
      return Optional.absent();
    }

    @Override
    public MethodSpec getMethod() {
      MethodSpec.Builder b = MethodSpec.methodBuilder("get")
          .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
          .returns(functionTypeName());

      for (ParameterSpec parameterSpec : parameterSpecList()) {
        b.addParameter(parameterSpec);
      }

      CodeBlock returnStatement = methodInfo().statementWriter()
          .add("return new $T(")
          .add("$L").forEachParameter(", ")
          .add(")")
          .set(className())
          .setParameterName()
          .write();

      return b.addCode(returnStatement)
          .build();
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy