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

br.com.objectos.way.pojo.plugin.PojoInfo Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show 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.way.pojo.plugin;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import br.com.objectos.core.util.MoreCollectors;
import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.CanGenerateCompilationError;
import br.com.objectos.way.code.ConstructorInfo;
import br.com.objectos.way.code.HasAnnotationInfoList;
import br.com.objectos.way.code.InterfaceInfo;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.TypeInfo;

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

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

  private final Mode mode;
  private final TypeInfo typeInfo;
  private final Naming naming;

  private Predicate propertyFilter = (t) -> true;

  private PojoInfo(Mode mode, TypeInfo typeInfo, Naming naming) {
    this.mode = mode;
    this.typeInfo = typeInfo;
    this.naming = naming;
  }

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

  public static PojoInfo of(TypeInfo typeInfo) {
    return of(Mode.STANDARD, typeInfo);
  }

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

  private static PojoInfo of0(Mode mode, TypeInfo typeInfo) {
    return new PojoInfo(
        mode,
        typeInfo,
        Naming.of(typeInfo));
  }

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

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

  public Stream constructorInfoStream() {
    return typeInfo.constructorInfoStream();
  }

  @Override
  public final boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof PojoInfo)) {
      return false;
    }
    PojoInfo that = (PojoInfo) obj;
    return Objects.equals(typeInfo, that.typeInfo);
  }

  public Optional getInterface(Class type) {
    return typeInfo.getInterface(type);
  }

  @Override
  public final int hashCode() {
    return typeInfo.hashCode();
  }

  public Stream ignoredPropertyStream() {
    return Property.of(mode, typeInfo)
        .stream()
        .filter(propertyFilter.negate());
  }

  public boolean instanceOf(Class type) {
    return typeInfo.isSubType(type);
  }

  public Stream methodInfoStream() {
    return typeInfo.methodInfoStream();
  }

  public Naming naming() {
    return naming;
  }

  public List propertyList() {
    return propertyStream().collect(MoreCollectors.toImmutableList());
  }

  public Stream propertyStream() {
    return Property.of(mode, typeInfo)
        .stream()
        .filter(propertyFilter);
  }

  void addPropertyCondition(PropertyPredicate condition) {
    propertyFilter = propertyFilter.and(condition.negate());
  }

  boolean hasInterface(Class type) {
    return instanceOf(type);
  }

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

  List pojoConstructorList() {
    return typeInfo.constructorInfoStream()
        .map(info -> new PojoConstructor(mode, naming, info))
        .collect(Collectors.toList());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy