/*
* 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 static com.google.common.collect.Lists.transform;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;
import br.com.objectos.way.base.Testables;
import br.com.objectos.way.base.WayIterables;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
abstract class TypeInfoPojo implements TypeInfo {
final PackageInfo packageInfo;
final String name;
final List<SimpleTypeInfo> genericTypeInfoList;
final List<InterfaceInfo> interfaceInfoList;
final List<MethodInfo> methodInfoList;
public TypeInfoPojo(Builder builder) {
packageInfo = builder.getPackageInfo();
name = builder.getName();
genericTypeInfoList = builder.getGenericTypeInfoList();
interfaceInfoList = builder.getInterfaceInfoList();
methodInfoList = setMethodInfoList(builder.getMethodInfoList());
}
@Override
public Optional<SimpleTypeInfo> getFirstGenericType() {
return FluentIterable.from(genericTypeInfoList)
.first();
}
@Override
public Optional<InterfaceInfo> getFirstInterfaceInfo() {
return FluentIterable.from(interfaceInfoList)
.first();
}
@Override
public boolean isEqual(TypeInfo o) {
TypeInfoPojo that = o.toPojo();
return Testables.isEqualHelper()
.equal(this.getClass(), that.getClass())
.equal(this.packageInfo, that.packageInfo)
.equal(this.name, that.name)
.allEqual(this.genericTypeInfoList, that.genericTypeInfoList)
.allEqual(toTypeInfoList(this.interfaceInfoList), toTypeInfoList(that.interfaceInfoList))
.allEqual(this.methodInfoList, that.methodInfoList)
.result();
}
@Override
public TypeInfoPojo toPojo() {
return this;
}
@Override
public String toString() {
return name;
}
@Override
public Type toType(AST ast) {
SimpleName name = ast.newSimpleName(this.name);
return ast.newSimpleType(name);
}
@Override
public SingleVariableDeclaration toSingleVariableDeclaration(AST ast) {
SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
declaration.setType(toType(ast));
String variableName = WayCode.lowerCaseFirstChar(name);
declaration.setName(ast.newSimpleName(variableName));
return declaration;
}
@Override
public SimpleName toSimpleNameSuffix(AST ast, String suffix) {
return ast.newSimpleName(name + suffix);
}
@Override
public void visitPublicGetters(GetterInfoVisitor visitor) {
List<GetterInfo> getterInfoList = getPublicGetterInfoList();
for (GetterInfo getterInfo : getterInfoList) {
visitor.visit(getterInfo);
}
}
@Override
public void visitPublicMethods(MethodInfoVisitor visitor) {
List<MethodInfo> methodInfoList = getPublicMethodInfoList();
for (MethodInfo methodInfo : methodInfoList) {
visitor.visit(methodInfo);
}
}
@Override
public PackageInfo writePackage(CompilationUnit compilationUnit) {
packageInfo.writeTo(compilationUnit);
return packageInfo;
}
List<MethodInfo> getMethodInfoList() {
return methodInfoList;
}
List<GetterInfo> getPublicGetterInfoList() {
List<MethodInfo> publicMethodInfoList = getPublicMethodInfoList();
List<Optional<GetterInfo>> optionalList = transform(publicMethodInfoList, MethodInfo.TO_GETTER_INFO);
Iterable<GetterInfo> presentLIst = Optional.presentInstances(optionalList);
return ImmutableList.copyOf(presentLIst);
}
List<MethodInfo> getPublicMethodInfoList() {
return WayIterables.from(interfaceInfoList)
.transformAndConcat(TypeInfo.TO_METHOD_INFO_LIST)
.addAll(methodInfoList)
.toImmutableList();
}
List<MethodInfo> setMethodInfoList(List<MethodInfo> methodInfoList) {
return methodInfoList;
}
private List<TypeInfo> toTypeInfoList(List<InterfaceInfo> list) {
return ImmutableList.<TypeInfo> builder()
.addAll(list)
.build();
}
}