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

br.com.objectos.code.pojo.CollectionNaming 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.code.pojo;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.code.TypeParameterInfo;
import br.com.objectos.core.auto.AutoPojo;
import br.com.objectos.core.testing.Testable;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;

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

  abstract ClassName collectionClassName();
  abstract TypeName collectionTypeName();
  abstract TypeName elementTypeName();
  abstract boolean wildcard();

  CollectionNaming() {
  }

  public static CollectionNamingBuilder builder() {
    return new CollectionNamingBuilderPojo();
  }

  public static CollectionNaming of(SimpleTypeInfo returnTypeInfo) {
    TypeParameterInfo typeParameterInfo = returnTypeInfo.getTypeParameterInfoStream()
        .findFirst()
        .get();
    boolean wildcard = typeParameterInfo.isWildcard();
    TypeName elementTypeName = typeParameterInfo.typeNameBound();
    return returnTypeInfo.isInfoOf(List.class)
        ? list(wildcard, elementTypeName)
        : returnTypeInfo.isInfoOf(Set.class)
            ? set(wildcard, elementTypeName)
            : null;
  }

  private static CollectionNaming list(boolean wildcard, TypeName elementTypeName) {
    return of(wildcard, List.class, ArrayList.class, elementTypeName);
  }

  private static CollectionNaming of(
      boolean wildcard, Class collection, Class implementation, TypeName elementTypeName) {
    ClassName collectionClassName = ClassName.get(collection);
    TypeName collectionTypeName = TypeName.get(implementation);
    return CollectionNaming.builder()
        .collectionClassName(collectionClassName)
        .collectionTypeName(collectionTypeName)
        .elementTypeName(elementTypeName)
        .wildcard(wildcard)
        .build();
  }

  private static CollectionNaming set(boolean wildcard, TypeName elementTypeName) {
    return of(wildcard, Set.class, LinkedHashSet.class, elementTypeName);
  }

  TypeName collectionTypeNameBound() {
    return ParameterizedTypeName.get(collectionClassName(), elementTypeName());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy