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

br.com.objectos.way.code.PackageInfo Maven / Gradle / Ivy

The 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.code;

import java.util.Objects;
import java.util.stream.Stream;

import br.com.objectos.way.testable.Equality;
import br.com.objectos.way.testable.Testable;
import br.com.objectos.way.testable.Tester;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;

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

  static final PackageInfo JAVA_LANG = new CorePackageInfo("java.lang");

  abstract String name();

  PackageInfo() {
  }

  public ClassName className(String simpleName) {
    return ClassName.get(name(), simpleName);
  }

  public ClassName className(String simpleName, String... simpleNames) {
    return ClassName.get(name(), simpleName, simpleNames);
  }

  public abstract Stream declaredTypeInfoStream();

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

  @Override
  public final int hashCode() {
    return Objects.hash(name());
  }

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

  @Override
  public Equality isEqualTo(Object that) {
    return Tester.of(PackageInfo.class)
        .add("name", o -> o.name())
        .test(this, that);
  }

  public boolean isJavaLang() {
    return "java.lang".equals(name());
  }

  public PackageInfo parentPackageInfo() {
    return new CorePackageInfo(parentName());
  }

  public JavaFile toJavaFile(TypeSpec typeSpec) {
    return JavaFile.builder(name(), typeSpec)
        .skipJavaLangImports(true)
        .build();
  }

  public String toQualifiedName(NameInfo nameInfo) {
    return String.format("%s.%s", this, nameInfo);
  }

  public String toQualifiedName(String name) {
    return name() + "." + name;
  }

  @Override
  public String toString() {
    return name();
  }

  String parentName() {
    int lastIndexOf = name().lastIndexOf('.');
    return lastIndexOf == -1
        ? ""
        : name().substring(0, lastIndexOf);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy