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

br.com.objectos.way.auto.optional.BuilderInterface 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.optional;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newArrayListWithCapacity;

import java.util.List;

import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.code.WayCode;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.core.tmpl.mustache.Mustaches;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;

/**
 * @author [email protected] (Marcio Endo)
 */
class BuilderInterface implements IsMustacheSerializable {

  private final MethodWrapper method;
  private final Optional next;

  private BuilderInterface(MethodWrapper method, Optional next) {
    this.method = method;
    this.next = next;
  }

  public static List listOf(List methodList) {
    int size = methodList.size();
    List list = newArrayListWithCapacity(size);

    BuilderInterface next = null;
    List reversed = Lists.reverse(methodList);
    for (MethodWrapper method : reversed) {
      Optional maybeNext = Optional.fromNullable(next);
      BuilderInterface iface = new BuilderInterface(method, maybeNext);
      list.add(iface);
      next = iface;
    }

    return Lists.reverse(list);
  }

  @Override
  public MustacheObject toMustache() {
    List declarations = newArrayList();
    declarations.add(new MethodDeclaration(method));

    BuilderInterface iter = this;
    while (iter != null && iter.isOptional()) {
      Declaration declaration = new BuildDeclaration();

      Optional maybeNext = iter.next;
      if (maybeNext.isPresent()) {
        BuilderInterface next = maybeNext.get();
        declaration = new MethodDeclaration(next.method);
      }

      declarations.add(declaration);
      iter = maybeNext.orNull();
    }

    return method.toMustacheHelper()
        .add("declarations", declarations)
        .toMustache();
  }

  private boolean isOptional() {
    return method.isOptional();
  }

  private static class BuildDeclaration extends Declaration {

    @Override
    String text() {
      return " build()";
    }

  }

  private static class MethodDeclaration extends Declaration {

    private final MethodWrapper method;

    public MethodDeclaration(MethodWrapper method) {
      this.method = method;
    }

    @Override
    String text() {
      MethodInfo info = method.methodInfo();
      String fieldName = info.getFieldName();
      String fieldToClassName = WayCode.upperCaseFirstChar(fieldName);
      SimpleTypeInfo returnType = method.unboxedType();
      return String.format("Builder%s %s(%s %s)", fieldToClassName, fieldName, returnType.getDeclaredName(), fieldName);
    }

  }

  abstract static class Declaration implements IsMustacheSerializable {

    @Override
    public final MustacheObject toMustache() {
      return Mustaches.toMustacheHelper()
          .add("text", text())
          .toMustache();
    }

    abstract String text();

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy