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

br.com.objectos.way.code.ParameterInfo 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.List;

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.ParameterSpec;
import com.squareup.javapoet.TypeName;

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

  public abstract Index index();
  @Override
  public abstract List annotationInfoList();
  public abstract SimpleTypeInfo simpleTypeInfo();
  public abstract String name();

  ParameterInfo() {
  }

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

  public boolean isInfoOf(Class type) {
    return simpleTypeInfo().isInfoOf(type);
  }

  public FieldInfo toFieldInfo() {
    return CoreFieldInfo.of(simpleTypeInfo(), name());
  }

  public IndexedParameterInfo toIndexedParameterInfo(int index, int total) {
    return new IndexedParameterInfoBuilder(index, total).build();
  }

  @Override
  public String toString() {
    return simpleTypeInfo().getDeclaredName() + " " + name();
  }

  @Override
  public void compilationError(String message) {
  }

  public ParameterInfoFieldWriter fieldWriter() {
    return new ParameterInfoFieldWriter(this);
  }

  public ParameterSpec parameterSpec() {
    TypeName type = simpleTypeInfo().typeName();
    return ParameterSpec.builder(type, name())
        .build();
  }

  void addName(List parameterList) {
    parameterList.add(name());
  }

  void addTypeNameAndName(List parameterList) {
    TypeName typeName = simpleTypeInfo().typeName();
    parameterList.add(typeName);
    addName(parameterList);
  }

  private class IndexedParameterInfoBuilder implements IndexedParameterInfo.Builder {

    private final int index;
    private final int total;

    public IndexedParameterInfoBuilder(int index, int total) {
      this.index = index;
      this.total = total;
    }

    @Override
    public IndexedParameterInfo build() {
      return new IndexedParameterInfoPojo(this);
    }

    @Override
    public ParameterInfo getParameterInfo() {
      return ParameterInfo.this;
    }

    @Override
    public boolean isLast() {
      return index + 1 == total;
    }

  }

  void signature(StringBuilder sig) {
    sig.append(simpleTypeInfo().qualifiedName());
  }

  void toString(StringBuilder toString) {
    signature(toString);
    toString.append(" ");
    toString.append(name());
  }

}