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

br.com.objectos.pojo.plugin.Property Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-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.pojo.plugin;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.CanGenerateCompilationError;
import br.com.objectos.code.HasAnnotationInfoList;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.ModifierInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.code.SimpleTypePrimitives;
import br.com.objectos.code.TypeInfo;
import br.com.objectos.code.TypeParameterInfo;
import br.com.objectos.core.util.MoreCollectors;
import br.com.objectos.pojo.Invalidate;
import br.com.objectos.testable.Equality;
import br.com.objectos.testable.Testable;
import br.com.objectos.testable.Tester;

import com.squareup.javapoet.TypeName;

/**
 * @author [email protected] (Marcio Endo)
 */
public class Property
    implements
    CanGenerateCompilationError,
    HasAnnotationInfoList,
    Testable {

  private static final Map> CACHE = new ConcurrentHashMap<>();

  private static final Tester TESTER = Tester.of(Property.class)
      .add("naming", o -> o.naming)
      .add("methodInfo", o -> o.methodInfo)
      .build();

  private final Mode mode;
  private final Naming naming;
  private final MethodInfo methodInfo;

  Property(Mode mode, Naming naming, MethodInfo methodInfo) {
    this.mode = mode;
    this.naming = naming;
    this.methodInfo = methodInfo;
  }

  public static void invalidate() {
    CACHE.clear();
  }

  static List of(Mode mode, TypeInfo typeInfo) {
    return CACHE.computeIfAbsent(typeInfo, t -> of0(mode, t));
  }

  private static List of0(Mode mode, TypeInfo typeInfo) {
    Naming naming = Naming.of(typeInfo);
    return typeInfo.methodInfoStream()
        .filter(m -> m.hasModifierInfo(ModifierInfo.ABSTRACT))
        .filter(m -> !m.hasReturnTypeInfo(SimpleTypePrimitives.VOID))
        .filter(m -> m.hasParameterInfoListSize(0))
        .filter(m -> !m.hasAnnotation(Invalidate.class))
        .map(m -> new Property(mode, naming, m))
        .collect(MoreCollectors.toImmutableList());
  }

  public String accessorName() {
    return methodInfo.name();
  }

  @Override
  public List annotationInfoList() {
    return methodInfo.annotationInfoList();
  }

  @Override
  public void compilationError(String message) {
    methodInfo.compilationError(message);
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (!(obj instanceof Property)) {
      return false;
    }
    Property that = (Property) obj;
    return naming.equals(that.naming)
        && methodInfo.equals(that.methodInfo);
  }

  @Override
  public int hashCode() {
    return Objects.hash(naming, methodInfo);
  }

  @Override
  public Equality isEqualTo(Object that) {
    return TESTER.test(this, that);
  }

  public String name() {
    return methodInfo.fieldName();
  }

  public SimpleTypeInfo returnTypeInfo() {
    return methodInfo.returnTypeInfo();
  }

  public BuilderProperty standardBuilderField() {
    return new FieldBuilderProperty(this);
  }

  public BuilderProperty standardBuilderGetter() {
    return new GetterBuilderProperty(this);
  }

  public BuilderProperty standardBuilderMethod() {
    return BuilderProperty.methodBuilder(this)
        .name()
        .addParameter()
        .nullCheckIfNecessary()
        .assignment()
        .build();
  }

  public BuilderProperty standardBuilderProperty() {
    return BuilderProperty.of(
        new SuperinterfaceBuilderProperty(this),
        standardBuilderField(),
        standardBuilderMethod(),
        standardBuilderGetter());
  }

  public PojoProperty standardPojoConstructorStatement() {
    return mode.standardPojoConstructorStatement(this);
  }

  public PojoProperty standardPojoField() {
    return PojoProperty.fieldBuilder(this)
        .modifiers(Modifier.PRIVATE, Modifier.FINAL)
        .build();
  }

  public PojoProperty standardPojoMethod() {
    return PojoProperty.overridingMethodBuilder(this)
        .statement("return $L", name())
        .build();
  }

  public PojoProperty standardPojoProperty() {
    return PojoProperty.of(standardPojoField(), standardPojoConstructorStatement(), standardPojoMethod());
  }

  @Override
  public String toString() {
    return methodInfo.toString();
  }

  public Stream typeParameterInfoStream() {
    return methodInfo.returnTypeInfo().getTypeParameterInfoStream();
  }

  boolean hasName(String name) {
    return name().equals(name);
  }

  boolean instanceOf(Class type) {
    SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
    return returnTypeInfo.isSubType(type);
  }

  MethodInfo methodInfo() {
    return methodInfo;
  }

  Naming naming() {
    return naming;
  }

  TypeName typeName() {
    SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
    return returnTypeInfo.typeName();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy