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

br.com.objectos.ui.html.Child 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.ui.html;

import java.util.List;
import java.util.stream.Collectors;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.core.auto.AutoPojo;
import br.com.objectos.core.testing.Testable;
import br.com.objectos.ui.html.annotation.Elements;

import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
@AutoPojo
abstract class Child implements Testable {

  abstract ClassName parentClassName();
  abstract String tagName();
  abstract ClassName childClassName();
  abstract ClassName innerClassName();

  Child() {
  }

  public static ChildBuilder builder() {
    return new ChildBuilderPojo();
  }

  public static List listOf(ClassName className, MethodInfo methodInfo) {
    return methodInfo.annotationInfo(Elements.class)
        .flatMap(annotationInfo -> annotationInfo.simpleTypeInfoArrayValue("value"))
        .orElse(ImmutableList. of())
        .stream()
        .map(childTypeInfo -> {
          ClassName protoClassName = childTypeInfo.className();
          ClassName childClassName = protoClassName.peerClass(protoClassName.simpleName().replace("Proto", ""));
          return Child.builder()
              .parentClassName(className)
              .tagName(childTypeInfo.typeInfo()
                  .flatMap(typeInfo -> typeInfo.annotationInfo(TagName.class))
                  .flatMap(annotationInfo -> annotationInfo.stringValue("value"))
                  .get())
              .childClassName(childClassName)
              .innerClassName(className.nestedClass(childClassName.simpleName() + "Child"))
              .build();
        })
        .collect(Collectors.toList());
  }

  public TypeSpec innerClass() {
    return TypeSpec.classBuilder(innerClassName().simpleName())
        .addModifiers(Modifier.PRIVATE)
        .superclass(childClassName())
        .addMethod(MethodSpec.methodBuilder("end")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC)
            .returns(parentClassName())
            .addStatement("return $T.this", parentClassName())
            .build())
        .build();
  }

  public List method() {
    return ImmutableList. builder()
        .add(method0())
        .add(method1())
        .build();
  }

  private MethodSpec method0() {
    return MethodSpec.methodBuilder("add")
        .addModifiers(Modifier.PUBLIC)
        .returns(parentClassName())
        .addParameter(childClassName(), tagName())
        .addStatement("return addElement($L)", tagName())
        .build();
  }

  private MethodSpec method1() {
    return MethodSpec.methodBuilder(tagName())
        .addModifiers(Modifier.PUBLIC)
        .returns(childClassName())
        .addStatement("$T child = new $T()", childClassName(), innerClassName())
        .addStatement("addElement(child)")
        .addStatement("return child")
        .build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy